From 61992b4e22c73bf0131b1298f20743bb0bb8dbf2 Mon Sep 17 00:00:00 2001 From: Vasily Kuznetsov Date: Wed, 22 Jun 2016 16:45:36 +0200 Subject: [PATCH] Implement --setup-plan option --- _pytest/main.py | 3 +++ _pytest/python.py | 15 ++++++++++----- _pytest/runner.py | 2 +- testing/python/setup_only.py | 32 ++++++++++++++++++++------------ testing/python/setup_plan.py | 18 ++++++++++++++++++ 5 files changed, 52 insertions(+), 18 deletions(-) create mode 100644 testing/python/setup_plan.py diff --git a/_pytest/main.py b/_pytest/main.py index ce0bb4918..ee99fa1e7 100644 --- a/_pytest/main.py +++ b/_pytest/main.py @@ -50,6 +50,9 @@ def pytest_addoption(parser): help="load configuration from `file` instead of trying to locate one of the implicit configuration files.") group.addoption('--setuponly', '--setup-only', action="store_true", help="only setup fixtures, don't execute the tests.") + group.addoption('--setupplan', '--setup-plan', action="store_true", + help="show what fixtures and tests would be executed but don't" + " execute anything.") group = parser.getgroup("collect", "collection") group.addoption('--collectonly', '--collect-only', action="store_true", diff --git a/_pytest/python.py b/_pytest/python.py index 6c7be14de..4c5614318 100644 --- a/_pytest/python.py +++ b/_pytest/python.py @@ -2459,7 +2459,8 @@ class FixtureDef: # even if finalization fails, we invalidate # the cached fixture value if hasattr(self, "cached_result"): - if self._fixturemanager.config.option.setuponly: + config = self._fixturemanager.config + if config.option.setuponly or config.option.setupplan: self._log_fixture_stack('TEARDOWN') if hasattr(self, "cached_param"): del self.cached_param @@ -2507,10 +2508,14 @@ class FixtureDef: fixturefunc = fixturefunc.__get__(request.instance) try: - result = call_fixture_func(fixturefunc, request, kwargs) - # We want to access the params of ids if they exist also in during - # the finish() method. - if self._fixturemanager.config.option.setuponly: + config = request.config + if config.option.setupplan: + result = None + else: + result = call_fixture_func(fixturefunc, request, kwargs) + if config.option.setuponly or config.option.setupplan: + # We want to access the params of ids if they exist also in during + # the finish() method. if hasattr(request, 'param'): if self.ids: ind = self.params.index(request.param) diff --git a/_pytest/runner.py b/_pytest/runner.py index d385027ad..efcb28108 100644 --- a/_pytest/runner.py +++ b/_pytest/runner.py @@ -73,7 +73,7 @@ def runtestprotocol(item, log=True, nextitem=None): rep = call_and_report(item, "setup", log) reports = [rep] if rep.passed: - if item.config.option.setuponly: + if item.config.option.setuponly or item.config.option.setupplan: show_test_item(item) else: reports.append(call_and_report(item, "call", log)) diff --git a/testing/python/setup_only.py b/testing/python/setup_only.py index fcb2b9a7d..2508ca74e 100644 --- a/testing/python/setup_only.py +++ b/testing/python/setup_only.py @@ -1,4 +1,12 @@ -def test_show_only_active_fixtures(testdir): +import pytest + + +@pytest.fixture(params=['--setup-only', '--setup-plan'], scope='module') +def mode(request): + return request.param + + +def test_show_only_active_fixtures(testdir, mode): p = testdir.makepyfile(''' import pytest @pytest.fixture @@ -11,7 +19,7 @@ def test_show_only_active_fixtures(testdir): pass ''') - result = testdir.runpytest("--setup-only", p) + result = testdir.runpytest(mode, p) assert result.ret == 0 result.stdout.fnmatch_lines([ @@ -22,7 +30,7 @@ def test_show_only_active_fixtures(testdir): assert "_arg0" not in result.stdout.str() -def test_show_different_scopes(testdir): +def test_show_different_scopes(testdir, mode): p = testdir.makepyfile(''' import pytest @pytest.fixture @@ -35,7 +43,7 @@ def test_show_different_scopes(testdir): pass ''') - result = testdir.runpytest("--setup-only", p) + result = testdir.runpytest(mode, p) assert result.ret == 0 result.stdout.fnmatch_lines([ @@ -47,7 +55,7 @@ def test_show_different_scopes(testdir): ]) -def test_show_nested_fixtures(testdir): +def test_show_nested_fixtures(testdir, mode): testdir.makeconftest(''' import pytest @pytest.fixture(scope='session') @@ -63,7 +71,7 @@ def test_show_nested_fixtures(testdir): pass ''') - result = testdir.runpytest("--setup-only", p) + result = testdir.runpytest(mode, p) assert result.ret == 0 result.stdout.fnmatch_lines([ @@ -75,7 +83,7 @@ def test_show_nested_fixtures(testdir): ]) -def test_show_fixtures_with_autouse(testdir): +def test_show_fixtures_with_autouse(testdir, mode): p = testdir.makepyfile(''' import pytest @pytest.fixture @@ -88,7 +96,7 @@ def test_show_fixtures_with_autouse(testdir): pass ''') - result = testdir.runpytest("--setup-only", p) + result = testdir.runpytest(mode, p) assert result.ret == 0 result.stdout.fnmatch_lines([ @@ -98,7 +106,7 @@ def test_show_fixtures_with_autouse(testdir): ]) -def test_show_fixtures_with_parameters(testdir): +def test_show_fixtures_with_parameters(testdir, mode): testdir.makeconftest(''' import pytest @pytest.fixture(scope='session', params=['foo', 'bar']) @@ -114,7 +122,7 @@ def test_show_fixtures_with_parameters(testdir): pass ''') - result = testdir.runpytest("--setup-only", p) + result = testdir.runpytest(mode, p) assert result.ret == 0 result.stdout.fnmatch_lines([ @@ -125,7 +133,7 @@ def test_show_fixtures_with_parameters(testdir): ]) -def test_show_fixtures_with_parameter_ids(testdir): +def test_show_fixtures_with_parameter_ids(testdir, mode): testdir.makeconftest(''' import pytest @pytest.fixture( @@ -142,7 +150,7 @@ def test_show_fixtures_with_parameter_ids(testdir): pass ''') - result = testdir.runpytest("--setup-only", p) + result = testdir.runpytest(mode, p) assert result.ret == 0 result.stdout.fnmatch_lines([ diff --git a/testing/python/setup_plan.py b/testing/python/setup_plan.py new file mode 100644 index 000000000..1618fa7b3 --- /dev/null +++ b/testing/python/setup_plan.py @@ -0,0 +1,18 @@ +def test_show_fixtures_and_test(testdir): + p = testdir.makepyfile(''' + import pytest + @pytest.fixture + def arg(): + assert False + def test_arg(arg): + assert False + ''') + + result = testdir.runpytest("--setup-plan", p) + assert result.ret == 0 + + result.stdout.fnmatch_lines([ + '*SETUP F arg*', + '*test_arg (fixtures used: arg)', + '*TEARDOWN F arg*', + ])