Skip to content

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

Closed

Description

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.

Activity

  1. eleanorjboyd commented on Nov 27, 2023

    @eleanorjboyd
    Member

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

  2. eleanorjboyd commented on Nov 27, 2023

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

  3. bhavyaus commented on Nov 29, 2023

    @bhavyaus

    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

    Eleanor Boyd (@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.

  4. joyceerhl commented on Nov 29, 2023

    @joyceerhl

    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

  5. eleanorjboyd commented on Nov 29, 2023

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

    Eleanor Boyd (@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?

  6. locked as resolved and limited conversation to collaborators on Dec 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions