Skip to content

Commit

Permalink
Fix --setup-only and --setup-show for custom pytest items
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoddemus committed Sep 28, 2019
1 parent 6bfd30d commit 7bdfba3
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 8 deletions.
1 change: 1 addition & 0 deletions changelog/5884.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix ``--setup-only`` and ``--setup-show`` for custom pytest items.
4 changes: 2 additions & 2 deletions src/_pytest/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ def show_test_item(item):
tw = item.config.get_terminal_writer()
tw.line()
tw.write(" " * 8)
tw.write(item._nodeid)
used_fixtures = sorted(item._fixtureinfo.name2fixturedefs.keys())
tw.write(item.nodeid)
used_fixtures = sorted(getattr(item, "fixturenames", []))
if used_fixtures:
tw.write(" (fixtures used: {})".format(", ".join(used_fixtures)))

Expand Down
27 changes: 27 additions & 0 deletions testing/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,30 @@ def get_write_msg(self, idx):
fullwidth = 80

return TWMock()


@pytest.fixture
def dummy_yaml_custom_test(testdir):
"""Writes a conftest file that collects and executes a dummy yaml test.
Taken from the docs, but stripped down to the bare minimum, useful for
tests which needs custom items collected.
"""
testdir.makeconftest(
"""
import pytest
def pytest_collect_file(parent, path):
if path.ext == ".yaml" and path.basename.startswith("test"):
return YamlFile(path, parent)
class YamlFile(pytest.File):
def collect(self):
yield YamlItem(self.fspath.basename, self)
class YamlItem(pytest.Item):
def runtest(self):
pass
"""
)
testdir.makefile(".yaml", test1="")
6 changes: 3 additions & 3 deletions testing/python/setup_only.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ def mode(request):
return request.param


def test_show_only_active_fixtures(testdir, mode):
p = testdir.makepyfile(
def test_show_only_active_fixtures(testdir, mode, dummy_yaml_custom_test):
testdir.makepyfile(
'''
import pytest
@pytest.fixture
Expand All @@ -21,7 +21,7 @@ def test_arg1(arg1):
'''
)

result = testdir.runpytest(mode, p)
result = testdir.runpytest(mode)
assert result.ret == 0

result.stdout.fnmatch_lines(
Expand Down
6 changes: 3 additions & 3 deletions testing/python/setup_plan.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
def test_show_fixtures_and_test(testdir):
def test_show_fixtures_and_test(testdir, dummy_yaml_custom_test):
""" Verifies that fixtures are not executed. """
p = testdir.makepyfile(
testdir.makepyfile(
"""
import pytest
@pytest.fixture
Expand All @@ -11,7 +11,7 @@ def test_arg(arg):
"""
)

result = testdir.runpytest("--setup-plan", p)
result = testdir.runpytest("--setup-plan")
assert result.ret == 0

result.stdout.fnmatch_lines(
Expand Down

0 comments on commit 7bdfba3

Please sign in to comment.