Skip to content

Wrong module reported when collecting tests in a package #4024

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

Closed
CaselIT opened this issue Sep 23, 2018 · 11 comments
Closed

Wrong module reported when collecting tests in a package #4024

CaselIT opened this issue Sep 23, 2018 · 11 comments
Labels
type: bug problem that needs to be addressed type: regression indicates a problem that was introduced in a release which was working previously

Comments

@CaselIT
Copy link

CaselIT commented Sep 23, 2018

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

@RonnyPfannschmidt RonnyPfannschmidt added type: bug problem that needs to be addressed type: regression indicates a problem that was introduced in a release which was working previously labels Sep 23, 2018
@RonnyPfannschmidt
Copy link
Member

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

@CaselIT
Copy link
Author

CaselIT commented Sep 23, 2018

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'>
      ...

@RonnyPfannschmidt
Copy link
Member

@CaselIT thanks for the feedback - we will add a acceptance test in future to sort this out

jonozzz pushed a commit to jonozzz/pytest that referenced this issue Sep 26, 2018
…directory when creating modules that are part of a package.
@jonozzz
Copy link

jonozzz commented Sep 26, 2018

Sorry, I just got back from vacation and just saw this.
Can you try this change and see if it fixes your issue?
jonozzz@58f7b17#diff-adadbb411b38936fe8a465a285a5dd0f

Why are you looking at the module's name attribute anyway? That should informative only, external code should not rely on that.

@CaselIT
Copy link
Author

CaselIT commented Sep 26, 2018

@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
image

i just noticed that sometimes it was gone and found that the problem was this

@RonnyPfannschmidt
Copy link
Member

@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

@CaselIT
Copy link
Author

CaselIT commented Sep 26, 2018

I did microsoft/vscode-python#2663

@CaselIT
Copy link
Author

CaselIT commented Sep 26, 2018

@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'>
    ...

@nicoddemus
Copy link
Member

Thanks @CaselIT for testing it!

@jonozzz could you open a PR with the fix when you have the time? Thanks! 🙇

@symonk
Copy link
Member

symonk commented Feb 25, 2021

Is this fixed already?

<Package tests>
  <Module foo_test.py>
    <Function test_it>
<Module tests/subdir/bar_test.py>
  <Function test_it>

is the output today as of head of master, unsure if thats correct or not but it looks OK to me?

@CaselIT
Copy link
Author

CaselIT commented Feb 25, 2021

I had honestly forgot about this issue.
I no longer face the problem mentioned in the op. Looking at the output I guess it's fixed, since the path does appear in the module name without init.

@CaselIT CaselIT closed this as completed Feb 25, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug problem that needs to be addressed type: regression indicates a problem that was introduced in a release which was working previously
Projects
None yet
Development

No branches or pull requests

5 participants