pytest
pytest copied to clipboard
Test failure when /tmp/setup.py exists
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 https://github.com/pytest-dev/pytest/blob/c97a40140b2eca3ea45fcb1645d77da8cae3ee32/src/_pytest/nodes.py#L601
Following _pytest.config._initini to determine_setup to this loop https://github.com/pytest-dev/pytest/blob/c97a40140b2eca3ea45fcb1645d77da8cae3ee32/src/_pytest/config/findpaths.py#L222-L225
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