Reputation: 907
My project structure looks like this:
src/
├---__init__.py
└---domain/
├---__init__.py
├---model/
| ├---__init__.py
| └---model_mapper.py
└---util/
├---__init__.py
└---utils.py
test/
├---__init__.py
└---unit/
├---__init__.py
└---test_model_mapper.py
And the settings.json
is:
{
"python.pythonPath": "C:\\Users\\asant\\AppData\\Local\\Programs\\Python\\Python37\\python.exe",
"python.linting.flake8Enabled": true,
"python.autoComplete.extraPaths": ["./src"],
"python.terminal.executeInFileDir": true,
"python.testing.pytestEnabled": true,
"gitlens.currentLine.enabled": false,
"gitlens.hovers.currentLine.over": "line",
"gitlens.codeLens.enabled": false,
"gitlens.hovers.enabled": false,
"python.testing.pytestArgs": [
"test"
],
"python.testing.unittestEnabled": false,
"python.testing.nosetestsEnabled": false
}
The line "python.autoComplete.extraPaths": ["./src"],
was necessary, otherwise flake8 complains about every import inside the scripts and I have to prepend src.
to every one. Besides this, when I try to run the test discovery from the built-in VSCode button I get from the output:
=================================== ERRORS ====================================
_______________ ERROR collecting test/unit/test_model_mapper.py _______________
ImportError while importing test module 'd:\asant\...\test\unit\test_model_mapper.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
C:\Users\asant\AppData\Local\Programs\Python\Python37\lib\importlib\__init__.py:127: in import_module
return _bootstrap._gcd_import(name[level:], package, level)
test\unit\test_model_mapper.py:2: in <module>
from domain.util.model_mapper import ModelMapper
E ModuleNotFoundError: No module named 'domain'
=========================== short test summary info ===========================
ERROR test/unit/test_model_mapper.py
!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!
==================== no tests collected, 1 error in 0.10s =====================
Traceback (most recent call last):
File "c:\Users\asant\.vscode\extensions\ms-python.python-2021.5.926500501\pythonFiles\testing_tools\run_adapter.py", line 22, in <module>
main(tool, cmd, subargs, toolargs)
File "c:\Users\asant\.vscode\extensions\ms-python.python-2021.5.926500501\pythonFiles\testing_tools\adapter\__main__.py", line 100, in main
parents, result = run(toolargs, **subargs)
File "c:\Users\asant\.vscode\extensions\ms-python.python-2021.5.926500501\pythonFiles\testing_tools\adapter\pytest\_discovery.py", line 44, in discover
raise Exception("pytest discovery failed (exit code {})".format(ec))
Exception: pytest discovery failed (exit code 2)
at ChildProcess.<anonymous> (c:\Users\asant\.vscode\extensions\ms-python.python-2021.5.926500501\out\client\extension.js:9:437028)
at Object.onceWrapper (events.js:422:26)
at ChildProcess.emit (events.js:315:20)
at maybeClose (internal/child_process.js:1048:16)
at Process.ChildProcess._handle.onexit (internal/child_process.js:288:5)]
Even adding src.
to each import inside the test_model_mapper.py
file won't help.
Upvotes: 2
Views: 9444
Reputation: 907
The solution is to create a .env
file to set the src
folder in PYTHONPATH
otherwise pytest is unable to find the model
package.
Also, putting conftest.py
inside the src
folder doesn't help as suggested by someone.
Upvotes: 2