Implement --setup-plan option

This commit is contained in:
Vasily Kuznetsov 2016-06-22 16:45:36 +02:00
parent 5e0d78f4f1
commit 61992b4e22
5 changed files with 52 additions and 18 deletions

View File

@ -50,6 +50,9 @@ def pytest_addoption(parser):
help="load configuration from `file` instead of trying to locate one of the implicit configuration files.") help="load configuration from `file` instead of trying to locate one of the implicit configuration files.")
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('--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 = parser.getgroup("collect", "collection")
group.addoption('--collectonly', '--collect-only', action="store_true", group.addoption('--collectonly', '--collect-only', action="store_true",

View File

@ -2459,7 +2459,8 @@ class FixtureDef:
# even if finalization fails, we invalidate # even if finalization fails, we invalidate
# the cached fixture value # the cached fixture value
if hasattr(self, "cached_result"): 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') self._log_fixture_stack('TEARDOWN')
if hasattr(self, "cached_param"): if hasattr(self, "cached_param"):
del self.cached_param del self.cached_param
@ -2507,10 +2508,14 @@ class FixtureDef:
fixturefunc = fixturefunc.__get__(request.instance) fixturefunc = fixturefunc.__get__(request.instance)
try: try:
result = call_fixture_func(fixturefunc, request, kwargs) config = request.config
# We want to access the params of ids if they exist also in during if config.option.setupplan:
# the finish() method. result = None
if self._fixturemanager.config.option.setuponly: 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 hasattr(request, 'param'):
if self.ids: if self.ids:
ind = self.params.index(request.param) ind = self.params.index(request.param)

View File

@ -73,7 +73,7 @@ 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: if item.config.option.setuponly or item.config.option.setupplan:
show_test_item(item) show_test_item(item)
else: else:
reports.append(call_and_report(item, "call", log)) reports.append(call_and_report(item, "call", log))

View File

@ -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(''' p = testdir.makepyfile('''
import pytest import pytest
@pytest.fixture @pytest.fixture
@ -11,7 +19,7 @@ def test_show_only_active_fixtures(testdir):
pass pass
''') ''')
result = testdir.runpytest("--setup-only", p) result = testdir.runpytest(mode, p)
assert result.ret == 0 assert result.ret == 0
result.stdout.fnmatch_lines([ result.stdout.fnmatch_lines([
@ -22,7 +30,7 @@ def test_show_only_active_fixtures(testdir):
assert "_arg0" not in result.stdout.str() assert "_arg0" not in result.stdout.str()
def test_show_different_scopes(testdir): def test_show_different_scopes(testdir, mode):
p = testdir.makepyfile(''' p = testdir.makepyfile('''
import pytest import pytest
@pytest.fixture @pytest.fixture
@ -35,7 +43,7 @@ def test_show_different_scopes(testdir):
pass pass
''') ''')
result = testdir.runpytest("--setup-only", p) result = testdir.runpytest(mode, p)
assert result.ret == 0 assert result.ret == 0
result.stdout.fnmatch_lines([ 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(''' testdir.makeconftest('''
import pytest import pytest
@pytest.fixture(scope='session') @pytest.fixture(scope='session')
@ -63,7 +71,7 @@ def test_show_nested_fixtures(testdir):
pass pass
''') ''')
result = testdir.runpytest("--setup-only", p) result = testdir.runpytest(mode, p)
assert result.ret == 0 assert result.ret == 0
result.stdout.fnmatch_lines([ 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(''' p = testdir.makepyfile('''
import pytest import pytest
@pytest.fixture @pytest.fixture
@ -88,7 +96,7 @@ def test_show_fixtures_with_autouse(testdir):
pass pass
''') ''')
result = testdir.runpytest("--setup-only", p) result = testdir.runpytest(mode, p)
assert result.ret == 0 assert result.ret == 0
result.stdout.fnmatch_lines([ 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(''' testdir.makeconftest('''
import pytest import pytest
@pytest.fixture(scope='session', params=['foo', 'bar']) @pytest.fixture(scope='session', params=['foo', 'bar'])
@ -114,7 +122,7 @@ def test_show_fixtures_with_parameters(testdir):
pass pass
''') ''')
result = testdir.runpytest("--setup-only", p) result = testdir.runpytest(mode, p)
assert result.ret == 0 assert result.ret == 0
result.stdout.fnmatch_lines([ 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(''' testdir.makeconftest('''
import pytest import pytest
@pytest.fixture( @pytest.fixture(
@ -142,7 +150,7 @@ def test_show_fixtures_with_parameter_ids(testdir):
pass pass
''') ''')
result = testdir.runpytest("--setup-only", p) result = testdir.runpytest(mode, p)
assert result.ret == 0 assert result.ret == 0
result.stdout.fnmatch_lines([ result.stdout.fnmatch_lines([

View File

@ -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*',
])