Skip to content

Commit f31c31a

Browse files
authored
Merge pull request #1695 from sallner/feature-setup-show
Feature setup show
2 parents 7927dff + 6438895 commit f31c31a

File tree

5 files changed

+55
-15
lines changed

5 files changed

+55
-15
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: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,43 @@
11
import pytest
22
import sys
33

4+
45
def pytest_addoption(parser):
56
group = parser.getgroup("debugconfig")
67
group.addoption('--setuponly', '--setup-only', action="store_true",
7-
help="only setup fixtures, don't execute the tests.")
8+
help="only setup fixtures, don't execute the tests.")
9+
group.addoption('--setupshow', '--setup-show', action="store_true",
10+
help="show setup fixtures while executing the tests.")
11+
812

913
@pytest.hookimpl(hookwrapper=True)
1014
def pytest_fixture_setup(fixturedef, request):
1115
yield
1216
config = request.config
13-
if config.option.setuponly:
17+
if config.option.setupshow:
1418
if hasattr(request, 'param'):
1519
# Save the fixture parameter so ._show_fixture_action() can
1620
# display it now and during the teardown (in .finish()).
1721
if fixturedef.ids:
1822
if callable(fixturedef.ids):
1923
fixturedef.cached_param = fixturedef.ids(request.param)
2024
else:
21-
fixturedef.cached_param = fixturedef.ids[request.param_index]
25+
fixturedef.cached_param = fixturedef.ids[
26+
request.param_index]
2227
else:
2328
fixturedef.cached_param = request.param
2429
_show_fixture_action(fixturedef, 'SETUP')
2530

31+
2632
def pytest_fixture_post_finalizer(fixturedef):
2733
if hasattr(fixturedef, "cached_result"):
2834
config = fixturedef._fixturemanager.config
29-
if config.option.setuponly:
35+
if config.option.setupshow:
3036
_show_fixture_action(fixturedef, 'TEARDOWN')
3137
if hasattr(fixturedef, "cached_param"):
3238
del fixturedef.cached_param
3339

40+
3441
def _show_fixture_action(fixturedef, msg):
3542
config = fixturedef._fixturemanager.config
3643
capman = config.pluginmanager.getplugin('capturemanager')
@@ -57,3 +64,9 @@ def _show_fixture_action(fixturedef, msg):
5764
capman.resumecapture()
5865
sys.stdout.write(out)
5966
sys.stderr.write(err)
67+
68+
69+
@pytest.hookimpl(tryfirst=True)
70+
def pytest_cmdline_main(config):
71+
if config.option.setuponly:
72+
config.option.setupshow = True

_pytest/setupplan.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import pytest
22

3+
34
def pytest_addoption(parser):
45
group = parser.getgroup("debugconfig")
56
group.addoption('--setupplan', '--setup-plan', action="store_true",
6-
help="show what fixtures and tests would be executed but don't"
7-
" execute anything.")
7+
help="show what fixtures and tests would be executed but "
8+
"don't execute anything.")
9+
810

911
@pytest.hookimpl(tryfirst=True)
1012
def pytest_fixture_setup(fixturedef, request):
@@ -13,7 +15,9 @@ def pytest_fixture_setup(fixturedef, request):
1315
fixturedef.cached_result = (None, None, None)
1416
return fixturedef.cached_result
1517

18+
1619
@pytest.hookimpl(tryfirst=True)
1720
def pytest_cmdline_main(config):
1821
if config.option.setupplan:
1922
config.option.setuponly = True
23+
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)