Implement --setup-show cli flag

to also be able to see fixture setup with normal test execution.
This commit is contained in:
Steffen Allner 2016-07-03 22:30:51 +02:00
parent f7b5bb2f97
commit b650c3c118
5 changed files with 42 additions and 11 deletions

View File

@ -87,8 +87,9 @@
* New cli flags: (1) ``--setup-plan`` performs normal collection and reports * New cli flags: (1) ``--setup-plan`` performs normal collection and reports
the potential setup and teardown, does not execute any fixtures and tests (2) the potential setup and teardown, does not execute any fixtures and tests (2)
``--setup-only`` performs normal collection, executes setup and teardown of ``--setup-only`` performs normal collection, executes setup and teardown of
fixtures and reports them. Thanks `@d6e`_, `@kvas-it`_, `@sallner`_ fixtures and reports them. (3) ``--setup-show`` performs normal test
and `@omarkohl`_ for the PR. execution and additionally shows the setup and teardown of fixtures.
Thanks `@d6e`_, `@kvas-it`_, `@sallner`_ and `@omarkohl`_ for the PRs.
* Added two new hooks: ``pytest_fixture_setup`` which executes the fixture * Added two new hooks: ``pytest_fixture_setup`` which executes the fixture
setup and ``pytest_fixture_post_finalizer`` which is called after the fixture's setup and ``pytest_fixture_post_finalizer`` which is called after the fixture's

View File

@ -73,9 +73,9 @@ def runtestprotocol(item, log=True, nextitem=None):
rep = call_and_report(item, "setup", log) rep = call_and_report(item, "setup", log)
reports = [rep] reports = [rep]
if rep.passed: if rep.passed:
if item.config.option.setuponly or item.config.option.setupplan: if item.config.option.setupshow:
show_test_item(item) show_test_item(item)
else: if not item.config.option.setuponly:
reports.append(call_and_report(item, "call", log)) reports.append(call_and_report(item, "call", log))
reports.append(call_and_report(item, "teardown", log, reports.append(call_and_report(item, "teardown", log,
nextitem=nextitem)) nextitem=nextitem))

View File

@ -5,12 +5,14 @@ def pytest_addoption(parser):
group = parser.getgroup("debugconfig") group = parser.getgroup("debugconfig")
group.addoption('--setuponly', '--setup-only', action="store_true", group.addoption('--setuponly', '--setup-only', action="store_true",
help="only setup fixtures, don't execute the tests.") help="only setup fixtures, don't execute the tests.")
group.addoption('--setupshow', '--setup-show', action="store_true",
help="show setup fixtures while executing the tests.")
@pytest.hookimpl(hookwrapper=True) @pytest.hookimpl(hookwrapper=True)
def pytest_fixture_setup(fixturedef, request): def pytest_fixture_setup(fixturedef, request):
yield yield
config = request.config config = request.config
if config.option.setuponly: if config.option.setupshow:
if hasattr(request, 'param'): if hasattr(request, 'param'):
# Save the fixture parameter so ._show_fixture_action() can # Save the fixture parameter so ._show_fixture_action() can
# display it now and during the teardown (in .finish()). # display it now and during the teardown (in .finish()).
@ -26,7 +28,7 @@ def pytest_fixture_setup(fixturedef, request):
def pytest_fixture_post_finalizer(fixturedef): def pytest_fixture_post_finalizer(fixturedef):
if hasattr(fixturedef, "cached_result"): if hasattr(fixturedef, "cached_result"):
config = fixturedef._fixturemanager.config config = fixturedef._fixturemanager.config
if config.option.setuponly: if config.option.setupshow:
_show_fixture_action(fixturedef, 'TEARDOWN') _show_fixture_action(fixturedef, 'TEARDOWN')
if hasattr(fixturedef, "cached_param"): if hasattr(fixturedef, "cached_param"):
del fixturedef.cached_param del fixturedef.cached_param
@ -57,3 +59,8 @@ def _show_fixture_action(fixturedef, msg):
capman.resumecapture() capman.resumecapture()
sys.stdout.write(out) sys.stdout.write(out)
sys.stderr.write(err) sys.stderr.write(err)
@pytest.hookimpl(tryfirst=True)
def pytest_cmdline_main(config):
if config.option.setuponly:
config.option.setupshow = True

View File

@ -17,3 +17,4 @@ def pytest_fixture_setup(fixturedef, request):
def pytest_cmdline_main(config): def pytest_cmdline_main(config):
if config.option.setupplan: if config.option.setupplan:
config.option.setuponly = True config.option.setuponly = True
config.option.setupshow = True

View File

@ -1,7 +1,8 @@
import pytest import pytest
@pytest.fixture(params=['--setup-only', '--setup-plan'], scope='module') @pytest.fixture(params=['--setup-only', '--setup-plan', '--setup-show'],
scope='module')
def mode(request): def mode(request):
return request.param return request.param
@ -24,7 +25,7 @@ def test_show_only_active_fixtures(testdir, mode):
result.stdout.fnmatch_lines([ result.stdout.fnmatch_lines([
'*SETUP F arg1*', '*SETUP F arg1*',
'*test_arg1 (fixtures used: arg1)', '*test_arg1 (fixtures used: arg1)*',
'*TEARDOWN F arg1*', '*TEARDOWN F arg1*',
]) ])
assert "_arg0" not in result.stdout.str() assert "_arg0" not in result.stdout.str()
@ -49,7 +50,7 @@ def test_show_different_scopes(testdir, mode):
result.stdout.fnmatch_lines([ result.stdout.fnmatch_lines([
'SETUP S arg_session*', 'SETUP S arg_session*',
'*SETUP F arg_function*', '*SETUP F arg_function*',
'*test_arg1 (fixtures used: arg_function, arg_session)', '*test_arg1 (fixtures used: arg_function, arg_session)*',
'*TEARDOWN F arg_function*', '*TEARDOWN F arg_function*',
'TEARDOWN S arg_session*', 'TEARDOWN S arg_session*',
]) ])
@ -77,7 +78,7 @@ def test_show_nested_fixtures(testdir, mode):
result.stdout.fnmatch_lines([ result.stdout.fnmatch_lines([
'SETUP S arg_same*', 'SETUP S arg_same*',
'*SETUP F arg_same (fixtures used: arg_same)*', '*SETUP F arg_same (fixtures used: arg_same)*',
'*test_arg1 (fixtures used: arg_same)', '*test_arg1 (fixtures used: arg_same)*',
'*TEARDOWN F arg_same*', '*TEARDOWN F arg_same*',
'TEARDOWN S arg_same*', 'TEARDOWN S arg_same*',
]) ])
@ -102,7 +103,7 @@ def test_show_fixtures_with_autouse(testdir, mode):
result.stdout.fnmatch_lines([ result.stdout.fnmatch_lines([
'SETUP S arg_session*', 'SETUP S arg_session*',
'*SETUP F arg_function*', '*SETUP F arg_function*',
'*test_arg1 (fixtures used: arg_function, arg_session)', '*test_arg1 (fixtures used: arg_function, arg_session)*',
]) ])
@ -219,3 +220,24 @@ def test_capturing(testdir):
'this should be captured', 'this should be captured',
'this should also be captured' 'this should also be captured'
]) ])
def test_show_fixtures_and_execute_test(testdir):
""" Verifies that setups are shown and tests are executed. """
p = testdir.makepyfile('''
import pytest
@pytest.fixture
def arg():
assert True
def test_arg(arg):
assert False
''')
result = testdir.runpytest("--setup-show", p)
assert result.ret == 1
result.stdout.fnmatch_lines([
'*SETUP F arg*',
'*test_arg (fixtures used: arg)F',
'*TEARDOWN F arg*',
])