Merge pull request #1695 from sallner/feature-setup-show

Feature setup show
This commit is contained in:
Bruno Oliveira 2016-07-05 21:29:01 -03:00 committed by GitHub
commit f31c31a73c
5 changed files with 55 additions and 15 deletions

View File

@ -87,8 +87,9 @@
* New cli flags: (1) ``--setup-plan`` performs normal collection and reports * New cli flags: (1) ``--setup-plan`` performs normal collection and reports
the potential setup and teardown, does not execute any fixtures and tests (2) the potential setup and teardown, does not execute any fixtures and tests (2)
``--setup-only`` performs normal collection, executes setup and teardown of ``--setup-only`` performs normal collection, executes setup and teardown of
fixtures and reports them. Thanks `@d6e`_, `@kvas-it`_, `@sallner`_ fixtures and reports them. (3) ``--setup-show`` performs normal test
and `@omarkohl`_ for the PR. 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 * Added two new hooks: ``pytest_fixture_setup`` which executes the fixture
setup and ``pytest_fixture_post_finalizer`` which is called after the fixture's setup and ``pytest_fixture_post_finalizer`` which is called after the fixture's

View File

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

View File

@ -1,16 +1,20 @@
import pytest import pytest
import sys import sys
def pytest_addoption(parser): def pytest_addoption(parser):
group = parser.getgroup("debugconfig") group = parser.getgroup("debugconfig")
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('--setupshow', '--setup-show', action="store_true",
help="show setup fixtures while executing the tests.")
@pytest.hookimpl(hookwrapper=True) @pytest.hookimpl(hookwrapper=True)
def pytest_fixture_setup(fixturedef, request): def pytest_fixture_setup(fixturedef, request):
yield yield
config = request.config config = request.config
if config.option.setuponly: if config.option.setupshow:
if hasattr(request, 'param'): if hasattr(request, 'param'):
# Save the fixture parameter so ._show_fixture_action() can # Save the fixture parameter so ._show_fixture_action() can
# display it now and during the teardown (in .finish()). # display it now and during the teardown (in .finish()).
@ -18,19 +22,22 @@ def pytest_fixture_setup(fixturedef, request):
if callable(fixturedef.ids): if callable(fixturedef.ids):
fixturedef.cached_param = fixturedef.ids(request.param) fixturedef.cached_param = fixturedef.ids(request.param)
else: else:
fixturedef.cached_param = fixturedef.ids[request.param_index] fixturedef.cached_param = fixturedef.ids[
request.param_index]
else: else:
fixturedef.cached_param = request.param fixturedef.cached_param = request.param
_show_fixture_action(fixturedef, 'SETUP') _show_fixture_action(fixturedef, 'SETUP')
def pytest_fixture_post_finalizer(fixturedef): def pytest_fixture_post_finalizer(fixturedef):
if hasattr(fixturedef, "cached_result"): if hasattr(fixturedef, "cached_result"):
config = fixturedef._fixturemanager.config config = fixturedef._fixturemanager.config
if config.option.setuponly: if config.option.setupshow:
_show_fixture_action(fixturedef, 'TEARDOWN') _show_fixture_action(fixturedef, 'TEARDOWN')
if hasattr(fixturedef, "cached_param"): if hasattr(fixturedef, "cached_param"):
del fixturedef.cached_param del fixturedef.cached_param
def _show_fixture_action(fixturedef, msg): def _show_fixture_action(fixturedef, msg):
config = fixturedef._fixturemanager.config config = fixturedef._fixturemanager.config
capman = config.pluginmanager.getplugin('capturemanager') capman = config.pluginmanager.getplugin('capturemanager')
@ -57,3 +64,9 @@ def _show_fixture_action(fixturedef, msg):
capman.resumecapture() capman.resumecapture()
sys.stdout.write(out) sys.stdout.write(out)
sys.stderr.write(err) sys.stderr.write(err)
@pytest.hookimpl(tryfirst=True)
def pytest_cmdline_main(config):
if config.option.setuponly:
config.option.setupshow = True

View File

@ -1,10 +1,12 @@
import pytest import pytest
def pytest_addoption(parser): def pytest_addoption(parser):
group = parser.getgroup("debugconfig") group = parser.getgroup("debugconfig")
group.addoption('--setupplan', '--setup-plan', action="store_true", group.addoption('--setupplan', '--setup-plan', action="store_true",
help="show what fixtures and tests would be executed but don't" help="show what fixtures and tests would be executed but "
" execute anything.") "don't execute anything.")
@pytest.hookimpl(tryfirst=True) @pytest.hookimpl(tryfirst=True)
def pytest_fixture_setup(fixturedef, request): def pytest_fixture_setup(fixturedef, request):
@ -13,7 +15,9 @@ def pytest_fixture_setup(fixturedef, request):
fixturedef.cached_result = (None, None, None) fixturedef.cached_result = (None, None, None)
return fixturedef.cached_result return fixturedef.cached_result
@pytest.hookimpl(tryfirst=True) @pytest.hookimpl(tryfirst=True)
def pytest_cmdline_main(config): def pytest_cmdline_main(config):
if config.option.setupplan: if config.option.setupplan:
config.option.setuponly = True config.option.setuponly = True
config.option.setupshow = True

View File

@ -1,7 +1,8 @@
import pytest 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): def mode(request):
return request.param return request.param
@ -24,7 +25,7 @@ def test_show_only_active_fixtures(testdir, mode):
result.stdout.fnmatch_lines([ result.stdout.fnmatch_lines([
'*SETUP F arg1*', '*SETUP F arg1*',
'*test_arg1 (fixtures used: arg1)', '*test_arg1 (fixtures used: arg1)*',
'*TEARDOWN F arg1*', '*TEARDOWN F arg1*',
]) ])
assert "_arg0" not in result.stdout.str() assert "_arg0" not in result.stdout.str()
@ -49,7 +50,7 @@ def test_show_different_scopes(testdir, mode):
result.stdout.fnmatch_lines([ result.stdout.fnmatch_lines([
'SETUP S arg_session*', 'SETUP S arg_session*',
'*SETUP F arg_function*', '*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 F arg_function*',
'TEARDOWN S arg_session*', 'TEARDOWN S arg_session*',
]) ])
@ -77,7 +78,7 @@ def test_show_nested_fixtures(testdir, mode):
result.stdout.fnmatch_lines([ result.stdout.fnmatch_lines([
'SETUP S arg_same*', 'SETUP S arg_same*',
'*SETUP F arg_same (fixtures used: 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 F arg_same*',
'TEARDOWN S arg_same*', 'TEARDOWN S arg_same*',
]) ])
@ -102,7 +103,7 @@ def test_show_fixtures_with_autouse(testdir, mode):
result.stdout.fnmatch_lines([ result.stdout.fnmatch_lines([
'SETUP S arg_session*', 'SETUP S arg_session*',
'*SETUP F arg_function*', '*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 be captured',
'this should also 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*',
])