VSCode Python 测试发现失败(2018 年 6 月)
VSCode Python Test Discovery fails (June 2018)
截至今天,测试发现对我不起作用,设置已经很棘手,我似乎无法修复它。
- VS Code 版本:2018 年 6 月(版本 1.25)(内部人员)
- 扩展版本(在扩展侧边栏下可用):2018.6.0
- OS 和版本:MacOS 10.13.3
- Python 版本(和发行版,如果适用,例如 Anaconda):3.6.4
- 使用的虚拟环境类型(N/A | venv | virtualenv | conda | ...):virtualenv
- Relevant/affected Python 软件包及其版本:pytest==3.2.1
文件夹结构:
src/ - 所有代码
测试/ - 所有测试
.env/ - 虚拟环境
.envFile -
(src 或测试中都没有 __init__.py 文件)
Python 扩展设置:
"python.autoComplete.addBrackets": true,
"python.autoComplete.extraPaths": [
"${workspaceRoot}/src"
],
"python.envFile": "${workspaceFolder}/.envFile",
"python.pythonPath": "${workspaceFolder}/.env/bin/python",
"python.unitTest.pyTestArgs": [
"-v", // increase verbosity
"-l", // show locals in tracebacks
"-x", // exit instantly on first error or failed test
"--ff", // run all tests but run the last failures first
"--color=yes" // color terminal output (yes/no/auto)
],
"python.unitTest.pyTestEnabled": true,
"python.unitTest.unittestArgs": [
"*test*.py",
"-p",
"-s",
"-v",
"."
],
"python.venvFolders": [
".env"
],
"python.workspaceSymbols.exclusionPatterns": [
"**/.env/",
"**/site-packages/**",
"_build"
],
.envFile
= PYTHONPATH=./src/
Python 运行 "Discover all tests":
时的测试日志
[1m============================= test session starts ==============================[0m
platform darwin -- Python 3.6.4, pytest-3.2.1, py-1.4.34, pluggy-0.4.0
rootdir: [redacted]/src, inifile:
plugins: cov-2.5.1
collected 0 items
[33m[1m========================= no tests ran in 0.05 seconds =========================[0m
最后:
PYTHONPATH=./src py.test tests --collect-only
======================================================================================================================== test session starts ========================================================================================================================
platform darwin -- Python 3.6.4, pytest-3.2.1, py-1.4.34, pluggy-0.4.0
rootdir: [redacted, note no src], inifile:
plugins: cov-2.5.1
collected 225 items
我建议将 Python 扩展降级到 2018.5.0。
GitHub 上有多个问题描述了 运行 测试 (https://github.com/Microsoft/vscode-python/issues) 的问题。
Don Jayamanne 在本 Github 期中亲自回答了这个问题:
https://github.com/Microsoft/vscode-python/issues/2047
某些人可能的解决方法:
当我 运行 进入这个时,我的解决方法是查看 Python 测试日志(见下文),并注意到有关 ImportError 的错误。我修复了那个导入错误,然后几秒钟后自动发现了测试。
更多信息:要获取 Python 测试日志,请转到左侧边栏中的 "Test" > 运行 所有测试(图标有播放按钮和两个烧杯),以及何时你 运行 这个,你应该(希望)看到两个弹出通知:
- 发现测试时出错,继续测试
- 运行测试时出错
点击'View Output'第二个。
截至今天,测试发现对我不起作用,设置已经很棘手,我似乎无法修复它。
- VS Code 版本:2018 年 6 月(版本 1.25)(内部人员)
- 扩展版本(在扩展侧边栏下可用):2018.6.0
- OS 和版本:MacOS 10.13.3
- Python 版本(和发行版,如果适用,例如 Anaconda):3.6.4
- 使用的虚拟环境类型(N/A | venv | virtualenv | conda | ...):virtualenv
- Relevant/affected Python 软件包及其版本:pytest==3.2.1
文件夹结构:
src/ - 所有代码
测试/ - 所有测试
.env/ - 虚拟环境
.envFile -
(src 或测试中都没有 __init__.py 文件)
Python 扩展设置:
"python.autoComplete.addBrackets": true,
"python.autoComplete.extraPaths": [
"${workspaceRoot}/src"
],
"python.envFile": "${workspaceFolder}/.envFile",
"python.pythonPath": "${workspaceFolder}/.env/bin/python",
"python.unitTest.pyTestArgs": [
"-v", // increase verbosity
"-l", // show locals in tracebacks
"-x", // exit instantly on first error or failed test
"--ff", // run all tests but run the last failures first
"--color=yes" // color terminal output (yes/no/auto)
],
"python.unitTest.pyTestEnabled": true,
"python.unitTest.unittestArgs": [
"*test*.py",
"-p",
"-s",
"-v",
"."
],
"python.venvFolders": [
".env"
],
"python.workspaceSymbols.exclusionPatterns": [
"**/.env/",
"**/site-packages/**",
"_build"
],
.envFile
= PYTHONPATH=./src/
Python 运行 "Discover all tests":
时的测试日志[1m============================= test session starts ==============================[0m
platform darwin -- Python 3.6.4, pytest-3.2.1, py-1.4.34, pluggy-0.4.0
rootdir: [redacted]/src, inifile:
plugins: cov-2.5.1
collected 0 items
[33m[1m========================= no tests ran in 0.05 seconds =========================[0m
最后:
PYTHONPATH=./src py.test tests --collect-only
======================================================================================================================== test session starts ========================================================================================================================
platform darwin -- Python 3.6.4, pytest-3.2.1, py-1.4.34, pluggy-0.4.0
rootdir: [redacted, note no src], inifile:
plugins: cov-2.5.1
collected 225 items
我建议将 Python 扩展降级到 2018.5.0。 GitHub 上有多个问题描述了 运行 测试 (https://github.com/Microsoft/vscode-python/issues) 的问题。
Don Jayamanne 在本 Github 期中亲自回答了这个问题: https://github.com/Microsoft/vscode-python/issues/2047
某些人可能的解决方法:
当我 运行 进入这个时,我的解决方法是查看 Python 测试日志(见下文),并注意到有关 ImportError 的错误。我修复了那个导入错误,然后几秒钟后自动发现了测试。
更多信息:要获取 Python 测试日志,请转到左侧边栏中的 "Test" > 运行 所有测试(图标有播放按钮和两个烧杯),以及何时你 运行 这个,你应该(希望)看到两个弹出通知:
- 发现测试时出错,继续测试
- 运行测试时出错
点击'View Output'第二个。