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.