When collecting tests from a directory is a package with (has a __init__.py file), the wrong module is reported if the files are in a subfolder
With a file structure like
- tests
|- __init__.py
|- bar_test.py
|- subfolder
|- foo_test.py
The reported output of pytest is
<Package 'C:\\path\\to\\pytest-discovery-bug\\tests_package'>
<Module 'bar_test.py'>
...
<Module 'foo_test.py'>
...
There is no indication that foo_test.py is inside the folder subfolder
I've created a minimal example: https://github.com/CaselIT/pytest-discovery-bug
This issue prevent the function of the python extension for vscode. https://github.com/Microsoft/vscode-python
this is an structural mistake when handling non-package folders within packages that contain tests
if the folder for a package mismatches, we should have a slash in the module "name" to denote it
CC @jonozzz
Indeed adding __init__.py into subfolder changes the output to the correct one
<Package 'C:\\path\\to\\pytest-discovery-bug\\tests_package'>
<Module 'bar_test.py'>
...
<Package 'C:\\path\\to\\pytest-discovery-bug\\tests_package\\subfolder'>
<Module 'foo_test.py'>
...
@CaselIT thanks for the feedback - we will add a acceptance test in future to sort this out
Sorry, I just got back from vacation and just saw this.
Can you try this change and see if it fixes your issue?
https://github.com/jonozzz/pytest/commit/58f7b174d8e27d88a2c11e4604e7ed36c9d18ec3#diff-adadbb411b38936fe8a465a285a5dd0f
Why are you looking at the module's name attribute anyway? That should informative only, external code should not rely on that.
@jonozzz I will try and report back.
I'm not directly looking at it. The vscode extension for python parses the collect output to provide the code lens functionality on the test so they can be ran or debugged individually

i just noticed that sometimes it was gone and found that the problem was this
@CaselIT please also report a bug against them, they should run collect-only against a plugin getting the actual objects instead of parsing messy output
@jonozzz yes it seems to fix the issue and does not break the parser of vscode. Thanks!
The reported output is now
<Package ''C:\\path\\to\\pytest-discovery-bug\\tests_package'>
<Module 'bar_test.py'>
...
<Module 'subfolder/foo_test.py'>
...
Thanks @CaselIT for testing it!
@jonozzz could you open a PR with the fix when you have the time? Thanks! 馃檱
Most helpful comment
I did https://github.com/Microsoft/vscode-python/issues/2663