From e1ad1a14af483af8df6979fbd61e842198c8996d Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Tue, 31 Jul 2018 17:50:55 -0300 Subject: [PATCH 1/2] Add example script and failure for #3742 --- .../example_scripts/fixtures/custom_item/conftest.py | 10 ++++++++++ .../fixtures/custom_item/foo/__init__.py | 0 .../fixtures/custom_item/foo/test_foo.py | 2 ++ testing/python/fixture.py | 5 +++++ 4 files changed, 17 insertions(+) create mode 100644 testing/example_scripts/fixtures/custom_item/conftest.py create mode 100644 testing/example_scripts/fixtures/custom_item/foo/__init__.py create mode 100644 testing/example_scripts/fixtures/custom_item/foo/test_foo.py diff --git a/testing/example_scripts/fixtures/custom_item/conftest.py b/testing/example_scripts/fixtures/custom_item/conftest.py new file mode 100644 index 00000000000..25299d72690 --- /dev/null +++ b/testing/example_scripts/fixtures/custom_item/conftest.py @@ -0,0 +1,10 @@ +import pytest + + +class CustomItem(pytest.Item, pytest.File): + def runtest(self): + pass + + +def pytest_collect_file(path, parent): + return CustomItem(path, parent) diff --git a/testing/example_scripts/fixtures/custom_item/foo/__init__.py b/testing/example_scripts/fixtures/custom_item/foo/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/testing/example_scripts/fixtures/custom_item/foo/test_foo.py b/testing/example_scripts/fixtures/custom_item/foo/test_foo.py new file mode 100644 index 00000000000..f174823854e --- /dev/null +++ b/testing/example_scripts/fixtures/custom_item/foo/test_foo.py @@ -0,0 +1,2 @@ +def test(): + pass diff --git a/testing/python/fixture.py b/testing/python/fixture.py index 70d79ab7132..240d7a08fa4 100644 --- a/testing/python/fixture.py +++ b/testing/python/fixture.py @@ -1619,6 +1619,11 @@ def test_package(one): reprec = testdir.inline_run() reprec.assertoutcome(passed=2) + def test_collect_custom_items(self, testdir): + testdir.copy_example("fixtures/custom_item") + result = testdir.runpytest("foo") + result.stdout.fnmatch_lines("*passed*") + class TestAutouseDiscovery(object): @pytest.fixture From 8c9efd86087c36dda54cbe5284c1f804688bd443 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Tue, 31 Jul 2018 19:06:30 -0300 Subject: [PATCH 2/2] Only call _collectfile on package instances As discussed in #3751, this feels like a hack, pushing it only so we can see how it fares on CI and if there are better solutions out there --- src/_pytest/main.py | 4 +++- testing/test_collection.py | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/_pytest/main.py b/src/_pytest/main.py index 9599fa16175..105891e4686 100644 --- a/src/_pytest/main.py +++ b/src/_pytest/main.py @@ -482,6 +482,8 @@ def collect(self): self.trace.root.indent -= 1 def _collect(self, arg): + from _pytest.python import Package + names = self._parsearg(arg) argpath = names.pop(0) paths = [] @@ -503,7 +505,7 @@ def _collect(self, arg): root = self._node_cache[pkginit] else: col = root._collectfile(pkginit) - if col: + if col and isinstance(col, Package): root = col[0] self._node_cache[root.fspath] = root diff --git a/testing/test_collection.py b/testing/test_collection.py index 6480cc85d42..23d82cb141b 100644 --- a/testing/test_collection.py +++ b/testing/test_collection.py @@ -647,7 +647,7 @@ def test_pkgfile(self, testdir): col = testdir.getnode(config, x) assert isinstance(col, pytest.Module) assert col.name == "x.py" - assert col.parent.parent.parent is None + assert col.parent.parent is None for col in col.listchain(): assert col.config is config