Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix --setup-only and --setup-show for custom pytest items #5886

Merged
merged 1 commit into from
Sep 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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