Skip to content

Test failure when /tmp/setup.py exists #13822

@Flamefire

Description

@Flamefire

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

nodeid = str(self.path.relative_to(session.config.rootpath))

Following _pytest.config._initini to determine_setup to this loop

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions