From b650c3c1181a7f496a02afeab1d82c1404b03584 Mon Sep 17 00:00:00 2001 From: Steffen Allner Date: Sun, 3 Jul 2016 22:30:51 +0200 Subject: [PATCH] Implement --setup-show cli flag to also be able to see fixture setup with normal test execution. --- CHANGELOG.rst | 5 +++-- _pytest/runner.py | 4 ++-- _pytest/setuponly.py | 11 +++++++++-- _pytest/setupplan.py | 1 + testing/python/setup_only.py | 32 +++++++++++++++++++++++++++----- 5 files changed, 42 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index e67cffc70..239650e5a 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -87,8 +87,9 @@ * New cli flags: (1) ``--setup-plan`` performs normal collection and reports the potential setup and teardown, does not execute any fixtures and tests (2) ``--setup-only`` performs normal collection, executes setup and teardown of - fixtures and reports them. Thanks `@d6e`_, `@kvas-it`_, `@sallner`_ - and `@omarkohl`_ for the PR. + fixtures and reports them. (3) ``--setup-show`` performs normal test + 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 setup and ``pytest_fixture_post_finalizer`` which is called after the fixture's diff --git a/_pytest/runner.py b/_pytest/runner.py index dff321a75..2ec24cd53 100644 --- a/_pytest/runner.py +++ b/_pytest/runner.py @@ -73,9 +73,9 @@ def runtestprotocol(item, log=True, nextitem=None): rep = call_and_report(item, "setup", log) reports = [rep] if rep.passed: - if item.config.option.setuponly or item.config.option.setupplan: + if item.config.option.setupshow: 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, "teardown", log, nextitem=nextitem)) diff --git a/_pytest/setuponly.py b/_pytest/setuponly.py index abb578da7..4fc472087 100644 --- a/_pytest/setuponly.py +++ b/_pytest/setuponly.py @@ -5,12 +5,14 @@ def pytest_addoption(parser): group = parser.getgroup("debugconfig") group.addoption('--setuponly', '--setup-only', action="store_true", 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) def pytest_fixture_setup(fixturedef, request): yield config = request.config - if config.option.setuponly: + if config.option.setupshow: if hasattr(request, 'param'): # Save the fixture parameter so ._show_fixture_action() can # 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): if hasattr(fixturedef, "cached_result"): config = fixturedef._fixturemanager.config - if config.option.setuponly: + if config.option.setupshow: _show_fixture_action(fixturedef, 'TEARDOWN') if hasattr(fixturedef, "cached_param"): del fixturedef.cached_param @@ -57,3 +59,8 @@ def _show_fixture_action(fixturedef, msg): capman.resumecapture() sys.stdout.write(out) sys.stderr.write(err) + +@pytest.hookimpl(tryfirst=True) +def pytest_cmdline_main(config): + if config.option.setuponly: + config.option.setupshow = True diff --git a/_pytest/setupplan.py b/_pytest/setupplan.py index c7c8ff60d..8d2d859ca 100644 --- a/_pytest/setupplan.py +++ b/_pytest/setupplan.py @@ -17,3 +17,4 @@ def pytest_fixture_setup(fixturedef, request): def pytest_cmdline_main(config): if config.option.setupplan: config.option.setuponly = True + config.option.setupshow = True diff --git a/testing/python/setup_only.py b/testing/python/setup_only.py index e7403420b..c780b197e 100644 --- a/testing/python/setup_only.py +++ b/testing/python/setup_only.py @@ -1,7 +1,8 @@ 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): return request.param @@ -24,7 +25,7 @@ def test_show_only_active_fixtures(testdir, mode): result.stdout.fnmatch_lines([ '*SETUP F arg1*', - '*test_arg1 (fixtures used: arg1)', + '*test_arg1 (fixtures used: arg1)*', '*TEARDOWN F arg1*', ]) assert "_arg0" not in result.stdout.str() @@ -49,7 +50,7 @@ def test_show_different_scopes(testdir, mode): result.stdout.fnmatch_lines([ 'SETUP S arg_session*', '*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 S arg_session*', ]) @@ -77,7 +78,7 @@ def test_show_nested_fixtures(testdir, mode): result.stdout.fnmatch_lines([ 'SETUP S 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 S arg_same*', ]) @@ -102,7 +103,7 @@ def test_show_fixtures_with_autouse(testdir, mode): result.stdout.fnmatch_lines([ 'SETUP S arg_session*', '*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 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*', + ])