Pass metafunc into the pytest generator

This removes a lot of code from the calling test (in this case
test_gabbits_pytest) but one problem still remains: test_pytest
still needs to exist in the test module, otherwise no tests are
created.
This commit is contained in:
Chris Dent 2016-11-01 16:16:35 +00:00
parent 6dd40c4290
commit 439706c464
2 changed files with 30 additions and 27 deletions

View File

@ -129,7 +129,8 @@ def build_tests(path, loader, host=None, port=8001, intercept=None,
def py_test_generator(test_dir, host=None, port=8001, intercept=None,
prefix=None, test_loader_name=None,
fixture_module=None, response_handlers=None,
content_handlers=None, require_ssl=False, url=None):
content_handlers=None, require_ssl=False, url=None,
metafunc=None):
"""Generate tests cases for py.test
This uses build_tests to create TestCases and then yields them in
@ -156,16 +157,33 @@ def py_test_generator(test_dir, host=None, port=8001, intercept=None,
if hasattr(test, '_tests'):
# Establish fixtures as if they were tests. These will
# be cleaned up by the pytester plugin.
test_list.append(('start_%s' % test._tests[0].__class__.__name__, test.start, result))
test_list.append(('start_%s' % test._tests[0].__class__.__name__,
test.start, result))
for subtest in test:
test_list.append(('%s' % subtest.__class__.__name__, subtest, result))
test_list.append(('stop_%s' % test._tests[0].__class__.__name__, test.stop))
test_list.append(('%s' % subtest.__class__.__name__,
subtest, result))
test_list.append(('stop_%s' % test._tests[0].__class__.__name__,
test.stop))
return test_list
if metafunc:
if metafunc.function == test_pytest:
ids = []
args = []
for test in test_list:
if len(test) >=3:
name, method, arg = test
else:
name, method = test
arg = None
ids.append(name)
args.append((method, arg))
metafunc.parametrize("test, result", argvalues=args, ids=ids)
else:
# preserve backwards compatibility with old calling style
return test_list
# temporarily here for testing, there has to be some kind of test
# that is being parameterized.
def test_pytest(test, result):
try:
test(result)

View File

@ -19,9 +19,9 @@ without duplication.
import os
from gabbi import driver
# this test_* needs to be imported bare or things do not work
# TODO: this test_* needs to be imported bare or things do not work
from gabbi.driver import test_pytest
from gabbi import driver
from gabbi.tests import simple_wsgi
from gabbi.tests import test_intercept
@ -29,25 +29,10 @@ TESTS_DIR = 'gabbits_intercept'
def pytest_generate_tests(metafunc):
os.environ['GABBI_TEST_URL'] = 'takingnames'
test_dir = os.path.join(os.path.dirname(__file__), TESTS_DIR)
test_generator = driver.py_test_generator(
driver.py_test_generator(
test_dir, intercept=simple_wsgi.SimpleWsgi,
fixture_module=test_intercept,
response_handlers=[test_intercept.TestResponseHandler])
if metafunc.function == test_pytest:
ids = []
args = []
for test in test_generator:
if len(test) >=3:
name, method, arg = test
else:
name, method = test
arg = None
ids.append(name)
args.append((method, arg))
metafunc.parametrize("test, result", argvalues=args, ids=ids)
response_handlers=[test_intercept.TestResponseHandler],
metafunc=metafunc)