Skip to content

Commit b650c3c

Browse files
committed
Implement --setup-show cli flag
to also be able to see fixture setup with normal test execution.
1 parent f7b5bb2 commit b650c3c

File tree

5 files changed

+42
-11
lines changed

5 files changed

+42
-11
lines changed

CHANGELOG.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,9 @@
8787
* New cli flags: (1) ``--setup-plan`` performs normal collection and reports
8888
the potential setup and teardown, does not execute any fixtures and tests (2)
8989
``--setup-only`` performs normal collection, executes setup and teardown of
90-
fixtures and reports them. Thanks `@d6e`_, `@kvas-it`_, `@sallner`_
91-
and `@omarkohl`_ for the PR.
90+
fixtures and reports them. (3) ``--setup-show`` performs normal test
91+
execution and additionally shows the setup and teardown of fixtures.
92+
Thanks `@d6e`_, `@kvas-it`_, `@sallner`_ and `@omarkohl`_ for the PRs.
9293

9394
* Added two new hooks: ``pytest_fixture_setup`` which executes the fixture
9495
setup and ``pytest_fixture_post_finalizer`` which is called after the fixture's

_pytest/runner.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,9 @@ def runtestprotocol(item, log=True, nextitem=None):
7373
rep = call_and_report(item, "setup", log)
7474
reports = [rep]
7575
if rep.passed:
76-
if item.config.option.setuponly or item.config.option.setupplan:
76+
if item.config.option.setupshow:
7777
show_test_item(item)
78-
else:
78+
if not item.config.option.setuponly:
7979
reports.append(call_and_report(item, "call", log))
8080
reports.append(call_and_report(item, "teardown", log,
8181
nextitem=nextitem))

_pytest/setuponly.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ def pytest_addoption(parser):
55
group = parser.getgroup("debugconfig")
66
group.addoption('--setuponly', '--setup-only', action="store_true",
77
help="only setup fixtures, don't execute the tests.")
8+
group.addoption('--setupshow', '--setup-show', action="store_true",
9+
help="show setup fixtures while executing the tests.")
810

911
@pytest.hookimpl(hookwrapper=True)
1012
def pytest_fixture_setup(fixturedef, request):
1113
yield
1214
config = request.config
13-
if config.option.setuponly:
15+
if config.option.setupshow:
1416
if hasattr(request, 'param'):
1517
# Save the fixture parameter so ._show_fixture_action() can
1618
# display it now and during the teardown (in .finish()).
@@ -26,7 +28,7 @@ def pytest_fixture_setup(fixturedef, request):
2628
def pytest_fixture_post_finalizer(fixturedef):
2729
if hasattr(fixturedef, "cached_result"):
2830
config = fixturedef._fixturemanager.config
29-
if config.option.setuponly:
31+
if config.option.setupshow:
3032
_show_fixture_action(fixturedef, 'TEARDOWN')
3133
if hasattr(fixturedef, "cached_param"):
3234
del fixturedef.cached_param
@@ -57,3 +59,8 @@ def _show_fixture_action(fixturedef, msg):
5759
capman.resumecapture()
5860
sys.stdout.write(out)
5961
sys.stderr.write(err)
62+
63+
@pytest.hookimpl(tryfirst=True)
64+
def pytest_cmdline_main(config):
65+
if config.option.setuponly:
66+
config.option.setupshow = True

_pytest/setupplan.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ def pytest_fixture_setup(fixturedef, request):
1717
def pytest_cmdline_main(config):
1818
if config.option.setupplan:
1919
config.option.setuponly = True
20+
config.option.setupshow = True

testing/python/setup_only.py

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import pytest
22

33

4-
@pytest.fixture(params=['--setup-only', '--setup-plan'], scope='module')
4+
@pytest.fixture(params=['--setup-only', '--setup-plan', '--setup-show'],
5+
scope='module')
56
def mode(request):
67
return request.param
78

@@ -24,7 +25,7 @@ def test_arg1(arg1):
2425

2526
result.stdout.fnmatch_lines([
2627
'*SETUP F arg1*',
27-
'*test_arg1 (fixtures used: arg1)',
28+
'*test_arg1 (fixtures used: arg1)*',
2829
'*TEARDOWN F arg1*',
2930
])
3031
assert "_arg0" not in result.stdout.str()
@@ -49,7 +50,7 @@ def test_arg1(arg_session, arg_function):
4950
result.stdout.fnmatch_lines([
5051
'SETUP S arg_session*',
5152
'*SETUP F arg_function*',
52-
'*test_arg1 (fixtures used: arg_function, arg_session)',
53+
'*test_arg1 (fixtures used: arg_function, arg_session)*',
5354
'*TEARDOWN F arg_function*',
5455
'TEARDOWN S arg_session*',
5556
])
@@ -77,7 +78,7 @@ def test_arg1(arg_same):
7778
result.stdout.fnmatch_lines([
7879
'SETUP S arg_same*',
7980
'*SETUP F arg_same (fixtures used: arg_same)*',
80-
'*test_arg1 (fixtures used: arg_same)',
81+
'*test_arg1 (fixtures used: arg_same)*',
8182
'*TEARDOWN F arg_same*',
8283
'TEARDOWN S arg_same*',
8384
])
@@ -102,7 +103,7 @@ def test_arg1(arg_function):
102103
result.stdout.fnmatch_lines([
103104
'SETUP S arg_session*',
104105
'*SETUP F arg_function*',
105-
'*test_arg1 (fixtures used: arg_function, arg_session)',
106+
'*test_arg1 (fixtures used: arg_function, arg_session)*',
106107
])
107108

108109

@@ -219,3 +220,24 @@ def test_capturing(two):
219220
'this should be captured',
220221
'this should also be captured'
221222
])
223+
224+
225+
def test_show_fixtures_and_execute_test(testdir):
226+
""" Verifies that setups are shown and tests are executed. """
227+
p = testdir.makepyfile('''
228+
import pytest
229+
@pytest.fixture
230+
def arg():
231+
assert True
232+
def test_arg(arg):
233+
assert False
234+
''')
235+
236+
result = testdir.runpytest("--setup-show", p)
237+
assert result.ret == 1
238+
239+
result.stdout.fnmatch_lines([
240+
'*SETUP F arg*',
241+
'*test_arg (fixtures used: arg)F',
242+
'*TEARDOWN F arg*',
243+
])

0 commit comments

Comments
 (0)