Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect paths to tests in output when using multiple test locations and --rootdir/pytest.ini #13254

Open
ugomancz opened this issue Feb 27, 2025 · 2 comments
Labels
topic: collection related to the collection phase type: bug problem that needs to be addressed

Comments

@ugomancz
Copy link

Description

When running pytest with paths to tests in multiple directories while setting rootdir (or having a pytest.ini file) to one of the test directories, paths to tests from other directories in the output are wrong.

Consider the following file structure:

pytest_rootdir_issue
└─tests
  ├─a
  │ └─aa
  │    └─test_a.py
  │
  └─b
    └─bb
       └─test_b.py

When running pytest inside tests/a/ and giving it paths to both tests (./aa ../b/bb) while having a pytest.ini in tests/a/aa (or setting --rootdir to ./aa), path to the test_b.py in the output is invalid. The only valid path to the test_b.py is after the <- arrow sign, but that can cause quite confusing and invalid-looking output, especially with multiple test locations, long paths, and many tests.

Output with the incorrect path:

================================= test session starts =================================
platform win32 -- Python 3.12.7, pytest-8.3.4, pluggy-1.5.0 -- C:\pytest_issue\.venv\Scripts\python.exe
cachedir: .pytest_cache
rootdir: C:\pytest_issue\tests\a\aa
collected 2 items

aa\test_a.py::test_a PASSED                                                      [ 50%]
aa\test_b.py::test_b <- ..\..\b\bb\test_b.py PASSED                              [100%]

================================== 2 passed in 0.01s ==================================

The aa\test_b.py::test_b test path is invalid.

pip list

Package   Version
--------- -------
colorama  0.4.6
iniconfig 2.0.0
packaging 24.2
pip       24.2
pluggy    1.5.0
pytest    8.3.4

pytest and operating system versions

pytest 8.3.4, Windows 11 Business 24H2 (build 26100.3194)

Minimal example

Minimal example is prepared in this repository: https://github.com/ugomancz/pytest_rootdir_issue

@ugomancz
Copy link
Author

Upon further investigation, I think I have identified the issue.

During the collection of the tests, when nodeid is being calculated, if the path to the test is outside of the config.rootpath, then paths to the tests' locations given as CLI argument are used for nodeid calculation.
This happens here

However, later, when the terminal output is being created (here), nodeid is joined with config.rootpath, resulting in the invalid path seen in the output.

@RonnyPfannschmidt RonnyPfannschmidt added type: bug problem that needs to be addressed topic: collection related to the collection phase labels Feb 27, 2025
@andrewshkovskii
Copy link

This is related to #13246

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: collection related to the collection phase type: bug problem that needs to be addressed
Projects
None yet
Development

No branches or pull requests

3 participants