dev*_*ife 7 python pytest visual-studio-code vscode-settings
Pytest测试发现失败。UI指出:
Test discovery error, please check the configuration settings for the tests
输出窗口指出:
Test Discovery failed:
Error: Traceback (most recent call last):
File "C:\Users\mikep\.vscode\extensions\ms-python.python-2019.4.11987\pythonFiles\testing_tools\run_adapter.py", line 16, in <module>
main(tool, cmd, subargs, toolargs)
File "C:\Users\mikep\.vscode\extensions\ms-python.python-2019.4.11987\pythonFiles\testing_tools\adapter\__main__.py", line 90, in main
parents, result = run(toolargs, **subargs)
File "C:\Users\mikep\.vscode\extensions\ms-python.python-2019.4.11987\pythonFiles\testing_tools\adapter\pytest.py", line 43, in discover
raise Exception('pytest discovery failed (exit code {})'.format(ec))
Exception: pytest discovery failed (exit code 3)
Run Code Online (Sandbox Code Playgroud)
这是我的设置:
{
"python.pythonPath": ".venv\\Scripts\\python.exe",
"python.testing.pyTestArgs": [
"tests"
],
"python.testing.unittestEnabled": false,
"python.testing.nosetestsEnabled": false,
"python.testing.pyTestEnabled": true
}
Run Code Online (Sandbox Code Playgroud)
我可以从命令行FWIW成功运行pytest。
Aut*_*umn 72
在创建了一个有导入错误的测试后,我花了很长时间试图破译这个无用的错误。在进行任何更深入的故障排除之前,验证您的测试套件实际上可以执行。
pytest --collect-only 是你的朋友。
小智 28
这不是一个完整的答案,因为我不知道为什么会发生这种情况,并且可能与您的问题无关,具体取决于您的测试结构。
我通过__init__.py在我的测试文件夹中放置一个文件解决了这个问题
例如:
????.vscode
? settings.json
?
????app
? myapp.py
?
????tests
test_myapp.py
__init__.py
Run Code Online (Sandbox Code Playgroud)
这几天前没有这个工作,但最近更新了python扩展。我不确定这是预期的行为还是现在发现的副作用
https://github.com/Microsoft/vscode-python/blob/master/CHANGELOG.md
Use Python code for discovery of tests when using pytest. (#4795)
小智 11
我只是想我会在这里添加我的答案,因为这很可能会影响使用.env文件进行项目环境设置的人,因为它是 12 因子应用程序的常见配置。
我的示例假设您使用的pipenv是虚拟环境管理,并且您.env在项目的根目录中有一个文件。
我的 vscode 工作区设置 json 文件如下所示。对我来说,这里的关键线是"python.envFile": "${workspaceFolder}/.env",
{
"python.pythonPath": ".venv/bin/python",
"python.linting.enabled": true,
"python.linting.pylintEnabled": true,
"python.linting.pycodestyleEnabled": false,
"python.linting.flake8Enabled": false,
"python.linting.pylintPath": ".venv/bin/pylint",
"python.linting.pylintArgs": [
"--load-plugins=pylint_django",
],
"python.formatting.provider": "black",
"python.formatting.blackArgs": [
"--line-length",
"100"
],
"python.testing.unittestEnabled": false,
"python.testing.nosetestsEnabled": false,
"python.testing.pytestEnabled": true,
"python.testing.pytestPath": ".venv/bin/pytest",
"python.envFile": "${workspaceFolder}/.env",
"python.testing.pytestArgs": [
"--no-cov"
],
}
Run Code Online (Sandbox Code Playgroud)
我希望这可以节省我花在解决这个问题上的时间。
小智 6
我通过使用“pip install --upgrade pytest”将 pytest 升级到最新版本解决了这个问题:4.4.1。我显然运行的是旧版本 3.4.2
在我的情况下,vscode 无法发现测试的问题是在(或者这可能是 a )中启用了覆盖模块,即setup.cfgpytest.ini
addopts= --cov <path> -ra
Run Code Online (Sandbox Code Playgroud)
由于覆盖率低导致测试发现失败。解决方案是从配置文件中删除该行。
另外,正如文档中所建议的:
您也可以手动配置测试通过设置一个且只有一个以下设置为true: ,
python.testing.unittestEnabled,python.testing.pytestEnabled和python.testing.nosetestsEnabled。
编辑:
稍微相关 - 在更复杂的项目的情况下,可能还需要在使用pytest运行测试时更改rootdir参数(在您的 内settings.json)(在 的情况下):ModuleNotFoundError
"python.testing.pytestArgs": [
"--rootdir","${workspaceFolder}/<path-to-directory-with-tests>"
],
Run Code Online (Sandbox Code Playgroud)
似乎是最新版本的VS Code Python扩展中的错误。我遇到了同样的问题,然后我将Python扩展降级为2019.3.6558,然后再次起作用。因此,我们应该转到VS Code扩展列表,选择Python扩展,然后从该扩展的设置中“安装其他版本...”。
我希望这也对您有用。
在开始测试发现之前,请检查它python.testing.cwd是否正确指向您的测试目录并且您的python.testing.pytestEnabled设置为true.
正确设置这些要求后,运行测试发现及其输出(请参阅输出窗口)。你应该看到这样的东西:
python $HOME/.vscode/extensions/ms-python.python-$VERSION/pythonFiles/testing_tools/run_adapter.py discover pytest -- --rootdir $ROOT_DIR_OF_YOUR_PROJECT -s --cache-clear
Test Discovery failed:
Error: ============================= test session starts ==============================
<SETTINGS RELATED TO YOUR MACHINE, PYTHON VERSION, PYTEST VERSION,...>
collected N items / M errors
...
Run Code Online (Sandbox Code Playgroud)
在这里突出显示最后一行很重要:collected N items / M errors。以下行将包含有关 发现的测试的信息pytest。因此,您的测试是可发现的,但存在与其正确执行相关的错误。
以下行将包含在大多数情况下与不正确的导入相关的错误。
检查之前是否已下载所有依赖项。如果您正在使用特定版本的依赖项(在requirements.txt文件中您有类似的内容your_package == X.Y.Z),请确保它是您需要的正确版本。
| 归档时间: |
|
| 查看次数: |
4178 次 |
| 最近记录: |