pytest 无法在 VSCode 中发现测试
pytest unable to discover tests in VSCode
我的项目结构如下所示:
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
而 settings.json
是:
{
"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
}
行 "python.autoComplete.extraPaths": ["./src"],
是必要的,否则 flake8 会抱怨脚本中的每个导入,我必须在每个导入前加上 src.
。除此之外,当我尝试 运行 来自内置 VSCode 按钮的测试发现时,我从输出中得到:
=================================== 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)]
即使在 test_model_mapper.py
文件中的每个导入中添加 src.
也无济于事。
解决方法是创建一个.env
文件来设置PYTHONPATH
中的src
文件夹,否则pytest无法找到model
包。
此外,将 conftest.py
放入 src
文件夹中并没有像某人建议的那样帮助。
我的项目结构如下所示:
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
而 settings.json
是:
{
"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
}
行 "python.autoComplete.extraPaths": ["./src"],
是必要的,否则 flake8 会抱怨脚本中的每个导入,我必须在每个导入前加上 src.
。除此之外,当我尝试 运行 来自内置 VSCode 按钮的测试发现时,我从输出中得到:
=================================== 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)]
即使在 test_model_mapper.py
文件中的每个导入中添加 src.
也无济于事。
解决方法是创建一个.env
文件来设置PYTHONPATH
中的src
文件夹,否则pytest无法找到model
包。
此外,将 conftest.py
放入 src
文件夹中并没有像某人建议的那样帮助。