Skip to content

Commit 7bdfba3

Browse files
committed
Fix --setup-only and --setup-show for custom pytest items
Fix #5884
1 parent 6bfd30d commit 7bdfba3

File tree

5 files changed

+36
-8
lines changed

5 files changed

+36
-8
lines changed

changelog/5884.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix ``--setup-only`` and ``--setup-show`` for custom pytest items.

src/_pytest/runner.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,8 @@ def show_test_item(item):
107107
tw = item.config.get_terminal_writer()
108108
tw.line()
109109
tw.write(" " * 8)
110-
tw.write(item._nodeid)
111-
used_fixtures = sorted(item._fixtureinfo.name2fixturedefs.keys())
110+
tw.write(item.nodeid)
111+
used_fixtures = sorted(getattr(item, "fixturenames", []))
112112
if used_fixtures:
113113
tw.write(" (fixtures used: {})".format(", ".join(used_fixtures)))
114114

testing/conftest.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,30 @@ def get_write_msg(self, idx):
8888
fullwidth = 80
8989

9090
return TWMock()
91+
92+
93+
@pytest.fixture
94+
def dummy_yaml_custom_test(testdir):
95+
"""Writes a conftest file that collects and executes a dummy yaml test.
96+
97+
Taken from the docs, but stripped down to the bare minimum, useful for
98+
tests which needs custom items collected.
99+
"""
100+
testdir.makeconftest(
101+
"""
102+
import pytest
103+
104+
def pytest_collect_file(parent, path):
105+
if path.ext == ".yaml" and path.basename.startswith("test"):
106+
return YamlFile(path, parent)
107+
108+
class YamlFile(pytest.File):
109+
def collect(self):
110+
yield YamlItem(self.fspath.basename, self)
111+
112+
class YamlItem(pytest.Item):
113+
def runtest(self):
114+
pass
115+
"""
116+
)
117+
testdir.makefile(".yaml", test1="")

testing/python/setup_only.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ def mode(request):
66
return request.param
77

88

9-
def test_show_only_active_fixtures(testdir, mode):
10-
p = testdir.makepyfile(
9+
def test_show_only_active_fixtures(testdir, mode, dummy_yaml_custom_test):
10+
testdir.makepyfile(
1111
'''
1212
import pytest
1313
@pytest.fixture
@@ -21,7 +21,7 @@ def test_arg1(arg1):
2121
'''
2222
)
2323

24-
result = testdir.runpytest(mode, p)
24+
result = testdir.runpytest(mode)
2525
assert result.ret == 0
2626

2727
result.stdout.fnmatch_lines(

testing/python/setup_plan.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
def test_show_fixtures_and_test(testdir):
1+
def test_show_fixtures_and_test(testdir, dummy_yaml_custom_test):
22
""" Verifies that fixtures are not executed. """
3-
p = testdir.makepyfile(
3+
testdir.makepyfile(
44
"""
55
import pytest
66
@pytest.fixture
@@ -11,7 +11,7 @@ def test_arg(arg):
1111
"""
1212
)
1313

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

1717
result.stdout.fnmatch_lines(

0 commit comments

Comments
 (0)