VS Code Python unittest AssertionError: Path must be within the project or Importerror

Question:

So, once again I have run into problems with python unittest within VS Code. My directory is structured as follows:

workdir
 __init__.py
 - package
   __init__.py

   - submoduleA
    __init__.py

   - submoduleB
     __init__.py

   ...

   - tests
     __init__.py
     test_A.py
     test_B.py
     ...

so basically, my package is in my workdir, has a bunch of submodules and a tests folder. everything has an __init__.pyfile and runs well when I import it in a script and run it.

Until this morning I had my settings.json as follows and everything went fine:

"python.testing.unittestArgs": [
        "-v",
        "-s",
        ".",
        "-p",
        "test_*.py"
    ],

now it says:

[ERROR 2022-7-8 11:38:54.740]: Error discovering unittest tests:
 Traceback (most recent call last):
  File "/home/fratajcz/.vscode-server/extensions/ms-python.python-2022.12.0/pythonFiles/testing_tools/unittest_discovery.py", line 42, in <module>
    suite = loader.discover(start_dir, pattern=pattern, top_level_dir=top_level_dir)
  File "/home/icb/fratajcz/anaconda3/envs/compat/lib/python3.7/unittest/loader.py", line 349, in discover
    tests = list(self._find_tests(start_dir, pattern))
  File "/home/fratajcz/anaconda3/envs/compat/lib/python3.7/unittest/loader.py", line 387, in _find_tests
    name = self._get_name_from_path(start_dir)
  File "/home/fratajcz/anaconda3/envs/compat/lib/python3.7/unittest/loader.py", line 371, in _get_name_from_path
    assert not _relpath.startswith('..'), "Path must be within the project"
AssertionError: Path must be within the project
Traceback (most recent call last):
  File "/home/fratajcz/.vscode-server/extensions/ms-python.python-2022.12.0/pythonFiles/get_output_via_markers.py", line 26, in <module>

So now, even . is not in the project anymore? What is this ominous project that VS Code is talkin about here?

If I set my settings.json more explicitely to:

"python.testing.unittestArgs": [
        "-v",
        "-s",
        "workdir/package/tests",
        "-t",
        "workdir/package/",
        "-p",
        "test_*.py"
    ],

it tells me:

[ERROR 2022-7-8 11:49:33.496]: Error discovering unittest tests:
 Traceback (most recent call last):
  File "/home/fratajcz/.vscode-server/extensions/ms-python.python-2022.12.0/pythonFiles/testing_tools/unittest_discovery.py", line 42, in <module>
    suite = loader.discover(start_dir, pattern=pattern, top_level_dir=top_level_dir)
  File "/home/fratajcz/anaconda3/envs/compat/lib/python3.7/unittest/loader.py", line 346, in discover
    raise ImportError('Start directory is not importable: %r' % start_dir)
ImportError: Start directory is not importable: 'workdir/package/tests'

if i just run unittest test discovery from the terminal with the same arguemnts it runs smoothly.

I just don’t know anymore, is this another sarcastic joke from VS Code?

Thanks for any help!

Asked By: fratajcz

||

Answers:

In case someone has a comparably frustrating experience with unittest in VS Code, simply switching to pytest solved the issue. My settings that work now (which are basically identical but in pytest syntax):

"python.testing.pytestArgs": [
        "package/tests/",
        "-v",
        "-k",
        "test_"
    ],
Answered By: fratajcz

I also had this issue when I had more than one test*.py files in the root of my workspace, and I used the root as the source for tests. Moving my tests into their own folder fixed this error. Although it appears your workspace is setup with tests in their own folder, so I’m not sure why you saw this error.

Also note, there is a bug where VS Code doesn’t respect all the parameters passed to unittest via settings.json

But for anyone searching here, try moving your tests to their own folder.

Answered By: Alex E