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

Test suite discovery error on imports that use shared filenames with the pythonFiles>unittestadapter directory #22453

Closed
shanesaravia opened this issue Nov 9, 2023 · 5 comments
Assignees
Labels
area-testing verification-needed Verification of issue is requested verified Verification succeeded
Milestone

Comments

@shanesaravia
Copy link

Due to PATH ordering, shared directory/file names in a users codebase with the pythonFiles > unittestadapter directory will fail.

Error: ModuleNotFoundError: No module named 'utils.<name>; 'utils' is not a package

Problem:

  • When running the test suite, we run discovery.py which adds unittestadapter to path at index 0.
  • In some tests that import a file from utils, it will first search within the unittestadapter directory due to PATH ordering
  • The discovery will now fail because it detects utils as a file and not a module since its reading the utils.py file from the unittestadapter directory.

Solution:

  • Once starting to run discovery, add the projects root path to PATH at index 0 so that any further imports will use the projects root directory and not reference the incorrect directory.
  • Since the test suite only allows the start_dir to be one directory deep, we can conclude that if the start_dir is not "." or contains a "/", that we need to add that start_dir's parent to PATH. Otherwise, we simply add the start_dir to PATH.
@github-actions github-actions bot added the triage-needed Needs assignment to the proper sub-team label Nov 9, 2023
@karthiknadig karthiknadig self-assigned this Nov 9, 2023
@eleanorjboyd
Copy link
Member

thanks again for contributing to the repo and putting in this fix!

@eleanorjboyd eleanorjboyd added this to the November 2023 milestone Nov 27, 2023
@eleanorjboyd eleanorjboyd removed the triage-needed Needs assignment to the proper sub-team label Nov 27, 2023
paulacamargo25 pushed a commit to paulacamargo25/vscode-python that referenced this issue Nov 27, 2023
…osoft#22454)

[Issue microsoft#22453](microsoft#22453)

- Once starting to run discovery, add the projects root path to PATH at
index 0 so that any further imports will use the projects root directory
and not reference the incorrect directory.
- Since the test suite only allows the start_dir to be one directory
deep, we can conclude that if the start_dir is not "." or contains a
"/", that we need to add that start_dir's parent to PATH. Otherwise, we
simply add the start_dir to PATH.
@eleanorjboyd
Copy link
Member

should be able to verify with the following set up:

project structure with my_utils containing some_function

my_project/
│
├── unittestadapter/
│   └── utils.py
│
├── utils/
│   ├── __init__.py
│   └── my_util.py
│
└── tests/
    └── test_my_util.py

test which should now work:

import unittest
from utils.my_util import some_function

class TestMyUtil(unittest.TestCase):
    def test_some_function(self):
        self.assertTrue(some_function())

made a zip of a project that should replicate this:
my_project.zip

@eleanorjboyd eleanorjboyd self-assigned this Nov 27, 2023
@eleanorjboyd eleanorjboyd added the verification-needed Verification of issue is requested label Nov 27, 2023
@bhavyaus bhavyaus added verified Verification succeeded and removed verification-needed Verification of issue is requested labels Nov 29, 2023
@bhavyaus
Copy link

bhavyaus commented Nov 29, 2023

should be able to verify with the following set up:

project structure with my_utils containing some_function

my_project/
│
├── unittestadapter/
│   └── utils.py
│
├── utils/
│   ├── __init__.py
│   └── my_util.py
│
└── tests/
    └── test_my_util.py

test which should now work:

import unittest
from utils.my_util import some_function

class TestMyUtil(unittest.TestCase):
    def test_some_function(self):
        self.assertTrue(some_function())

made a zip of a project that should replicate this: my_project.zip

@eleanorjboyd I'm still seeing the import error mentioned in the description:

Image

I might be missing a few setup steps but would like to learn.

@bhavyaus bhavyaus added verification-needed Verification of issue is requested and removed verified Verification succeeded labels Nov 29, 2023
@joyceerhl joyceerhl added the verified Verification succeeded label Nov 29, 2023
@joyceerhl
Copy link

This worked for me after I ran python3 -m pip install pytest, reloaded the window, and reran test discovery, selecting unittest as the test framework. Why does unittest test discovery depend on pytest, and can we help the user out with installing the package?
image

@eleanorjboyd
Copy link
Member

should be able to verify with the following set up:
project structure with my_utils containing some_function

my_project/
│
├── unittestadapter/
│   └── utils.py
│
├── utils/
│   ├── __init__.py
│   └── my_util.py
│
└── tests/
    └── test_my_util.py

test which should now work:

import unittest
from utils.my_util import some_function

class TestMyUtil(unittest.TestCase):
    def test_some_function(self):
        self.assertTrue(some_function())

made a zip of a project that should replicate this: my_project.zip

@eleanorjboyd I'm still seeing the import error mentioned in the description:

Image

I might be missing a few setup steps but would like to learn.

What do you have as your settings for your unittest args?

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 30, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-testing verification-needed Verification of issue is requested verified Verification succeeded
Projects
None yet
Development

No branches or pull requests

5 participants