From c61cff4c7765586d1ff5eb89be4dcdc9891d556d Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Mon, 28 Oct 2019 09:38:30 +0100 Subject: [PATCH 1/2] [bug] pytest_collect_file gets called multiple times testing/test_collection.py::test_collect_init_tests triggers the assert. --- src/_pytest/main.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/_pytest/main.py b/src/_pytest/main.py index ad65ed29929..98e69ae28b5 100644 --- a/src/_pytest/main.py +++ b/src/_pytest/main.py @@ -386,6 +386,8 @@ def __init__(self, config): self.config.pluginmanager.register(self, name="session") + self._collected = set() + def __repr__(self): return "<%s %s exitstatus=%r testsfailed=%d testscollected=%d>" % ( self.__class__.__name__, @@ -575,6 +577,10 @@ def _collect(self, arg): yield from m def _collectfile(self, path, handle_dupes=True): + key = (path, handle_dupes) + assert key not in self._collected, key + self._collected.add(key) + assert ( path.isfile() ), "{!r} is not a file (isdir={!r}, exists={!r}, islink={!r})".format( From aefd3beaa5e58e4d793ea9d7d17ba4bac55d68d9 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Mon, 28 Oct 2019 09:43:58 +0100 Subject: [PATCH 2/2] RFC: harden test, but would be better to have this in general [ci skip] --- .../collect/collect_init_tests/conftest.py | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 testing/example_scripts/collect/collect_init_tests/conftest.py diff --git a/testing/example_scripts/collect/collect_init_tests/conftest.py b/testing/example_scripts/collect/collect_init_tests/conftest.py new file mode 100644 index 00000000000..365906d14a5 --- /dev/null +++ b/testing/example_scripts/collect/collect_init_tests/conftest.py @@ -0,0 +1,11 @@ +from typing import Set +from typing import Tuple + +collected_already = set() # type: Set[Tuple] + + +def pytest_collect_file(path, parent): + # Ensure that the hook is only called once per path. + key = (path, parent) + assert key not in collected_already, key + collected_already.add(key)