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__.py
file 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!