From 7bdfba3578455b58589e5834fea3feaf932018bd Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Sat, 28 Sep 2019 11:52:09 -0300 Subject: [PATCH] Fix --setup-only and --setup-show for custom pytest items Fix #5884 --- changelog/5884.bugfix.rst | 1 + src/_pytest/runner.py | 4 ++-- testing/conftest.py | 27 +++++++++++++++++++++++++++ testing/python/setup_only.py | 6 +++--- testing/python/setup_plan.py | 6 +++--- 5 files changed, 36 insertions(+), 8 deletions(-) create mode 100644 changelog/5884.bugfix.rst diff --git a/changelog/5884.bugfix.rst b/changelog/5884.bugfix.rst new file mode 100644 index 000000000..42d207ce7 --- /dev/null +++ b/changelog/5884.bugfix.rst @@ -0,0 +1 @@ +Fix ``--setup-only`` and ``--setup-show`` for custom pytest items. diff --git a/src/_pytest/runner.py b/src/_pytest/runner.py index 7d8b74a80..fce4c1e3f 100644 --- a/src/_pytest/runner.py +++ b/src/_pytest/runner.py @@ -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))) diff --git a/testing/conftest.py b/testing/conftest.py index d7f94ce45..a03efb0cf 100644 --- a/testing/conftest.py +++ b/testing/conftest.py @@ -88,3 +88,30 @@ def tw_mock(): 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="") diff --git a/testing/python/setup_only.py b/testing/python/setup_only.py index 4ae24b15a..7c871a9ee 100644 --- a/testing/python/setup_only.py +++ b/testing/python/setup_only.py @@ -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 @@ -21,7 +21,7 @@ def test_show_only_active_fixtures(testdir, mode): ''' ) - result = testdir.runpytest(mode, p) + result = testdir.runpytest(mode) assert result.ret == 0 result.stdout.fnmatch_lines( diff --git a/testing/python/setup_plan.py b/testing/python/setup_plan.py index 0321939a8..e323ba240 100644 --- a/testing/python/setup_plan.py +++ b/testing/python/setup_plan.py @@ -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 @@ -11,7 +11,7 @@ def test_show_fixtures_and_test(testdir): """ ) - result = testdir.runpytest("--setup-plan", p) + result = testdir.runpytest("--setup-plan") assert result.ret == 0 result.stdout.fnmatch_lines(