pytest-dev / pytest

The pytest framework makes it easy to write small tests, yet scales to support complex functional testing
https://pytest.org
MIT License
11.84k stars 2.64k forks source link

rootdir is not considered as expected #11186

Open roeniss opened 1 year ago

roeniss commented 1 year ago

note

I made a repository to reproduce the bug: https://github.com/roeniss/pytest-bug-rootdir

issue

> tree 
.
├── settings
│   └── pytest.ini
└── test
    └── test_hello.py

> pytest -c settings/pytest.ini
=== test session starts === 
platform darwin -- Python 3.11.4, pytest-7.4.0, pluggy-1.2.0
rootdir: /Users/roeniss/tmp/pytest-error-reproduce/settings
configfile: pytest.ini
collected 1 item

settings/test/test_hello.py .                                                                                                                       [100%

=== 1 passed in 0.00s ===  

As we can see above, settings/test/test_hello.py doesn't exist. I came up with some idea:

  1. rootdir is just a string placeholder for readability. It is set as the configuration's directory by default and does not affect any actual test run. So this is the expected output.
  2. rootdir is something that is considered as source root or test root, meaning that this is a real bug. What should happen here is that no test has run because settings directory has no test.
  3. rootdir is worked but somehow it fallbacks to root path because there is no test in settings or any reason. Then it starts finding test and find it, run, but sadly the output shows wrong directory. So the log-side should be fixed.

I brought this bug here because I'm not sure what rootdir actually does. Is there any correct assumption on my thought?

versions

> pip list
Package    Version
---------- -------
iniconfig  2.0.0
packaging  23.1
pip        23.1.2
pluggy     1.2.0
pytest     7.4.0
setuptools 67.6.1

> pytest --version
pytest 7.4.0

> neofetch
                    'c.          roeniss@Roenissui-MacBookAir-2.local
                 ,xNMM.          ------------------------------------
               .OMMMMo           OS: macOS 13.2.1 22D68 arm64
               OMMM0,            Host: Mac14,2
     .;loddo:' loolloddol;.      Kernel: 22.3.0
   cKMMMMMMMMMMNWMMMMMMMMMM0:    Uptime: 5 days, 15 hours, 30 mins
 .KMMMMMMMMMMMMMMMMMMMMMMMWd.    Packages: 278 (brew)
 XMMMMMMMMMMMMMMMMMMMMMMMX.      Shell: zsh 5.8.1
;MMMMMMMMMMMMMMMMMMMMMMMM:       Resolution: 2048x1152
:MMMMMMMMMMMMMMMMMMMMMMMM:       DE: Aqua
.MMMMMMMMMMMMMMMMMMMMMMMMX.      WM: Rectangle
 kMMMMMMMMMMMMMMMMMMMMMMMMWd.    Terminal: iTerm2
 .XMMMMMMMMMMMMMMMMMMMMMMMMMMk   Terminal Font: JetBrainsMono-Regular 11
  .XMMMMMMMMMMMMMMMMMMMMMMMMK.   CPU: Apple M2
    kMMMMMMMMMMMMMMMMMMMMMMd     GPU: Apple M2
     ;KMMMMMMMWXXWMMMMMMMk.      Memory: 1487MiB / 8192MiB
       .cooc,.    .,coo:.
RonnyPfannschmidt commented 1 year ago

At first glance this Looks like an combination of 2 and 3

Rootdir is used as anchor for testing ids

bluetech commented 1 year ago

Definitely a bug, would be interesting to understand what goes on here. Also interesting to think about what it should show...

I brought this bug here because I'm not sure what rootdir actually does. Is there any correct assumption on my thought?

In practice, putting pytest.ini in a subdir is pretty unusual, I would recommend putting it directly in your root dir then you'd be walking the beaten path. But if you want to keep -c settings/pytest.ini, I recommend also setting --rootdir=. because pytest derives the default rootdir from the location of the settings file, but in this case you want it to be /Users/roeniss/tmp/pytest-error-reproduce not /Users/roeniss/tmp/pytest-error-reproduce/settings.

roeniss commented 1 year ago

In practice, putting pytest.ini in a subdir is pretty unusual

I agree on your suggestion. This is a kind of experiment, which is inspired by this: https://dotmeta.org/ . IMHO, pytest.ini is not that important to reside in root path.

I recommend also setting --rootdir=.

Indeed, this resolves the problem I saw. This issue is about "why it works even if I didn't set --rootdir. Please check out my third idea please.