-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Description
I was debugging multiple test failures which I could reduce to a failure showing already in testing/test_warnings.py
pytest -s testing/test_warnings.py -k test_warning_recorded_hook
============ FAILURES ==================
_______________ test_warning_recorded_hook ____________________
pytester = <Pytester PosixPath('/tmp/pytest-of-s3248973/pytest-12/test_warning_recorded_hook0')>
> ???
E AssertionError: [('config warning', 'config', '', ('/tmp/pytest-of-s3248973/pytest-12/test_warning_recorded_hook0/conftest.py', 2, 'pytest_configure')), ('collect warning', 'collect', '', None), ('setup warning', 'runtest', 'pytest-of-s3248973/pytest-12/test_warning_recorded_hook0/test_warning_recorded_hook.py::test_func', None), ('call warning', 'runtest', 'pytest-of-s3248973/pytest-12/test_warning_recorded_hook0/test_warning_recorded_hook.py::test_func', None), ('teardown warning', 'runtest', 'pytest-of-s3248973/pytest-12/test_warning_recorded_hook0/test_warning_recorded_hook.py::test_func', None)]
E assert 'pytest-of-s3...py::test_func' == 'test_warning...py::test_func'
E - test_warning_recorded_hook.py::test_func
E + pytest-of-s3248973/pytest-12/test_warning_recorded_hook0/test_warning_recorded_hook.py::test_func
I.e. for the recording of the warning location it uses the path under /tmp instead of only the filename.
Tracing further the outmost Collector
has a nodeid of pytest-of-s3248973/pytest-12/test_warning_recorded_hook0
instead of test_warning_recorded_hook.py
because session.config.rootpath
is determined as /tmp
, see
Line 601 in c97a401
nodeid = str(self.path.relative_to(session.config.rootpath)) |
Following _pytest.config._initini
to determine_setup
to this loop
pytest/src/_pytest/config/findpaths.py
Lines 222 to 225 in c97a401
for possible_rootdir in (ancestor, *ancestor.parents): | |
if (possible_rootdir / "setup.py").is_file(): | |
rootdir = possible_rootdir | |
break |
It starts from the test folder ancestor
where the test is invoked from upwards until it finds /tmp/setup.py
and then treats /tmp
as the rootdir
This ultimately causes the failure.
I see 2 solutions: Ignore tempfile.gettempdir()/setup.py
and/or make the tests resilient by either passing --rootdir
and/or matching against path suffixes instead of full paths in the test assertions
I'm using pytest 7.2.2 w/ Python 3.9 but it should affect (almost) any version