截至今天,测试发现停止为我工作,设置起来已经很棘手了,我似乎无法修复它。
- VS Code 版本:2018 年 6 月(版本 1.25((预览体验成员( 扩展
- 版本(在扩展侧边栏下可用(:2018.6.0
- 操作系统和版本: MacOS 10.13.3
- Python 版本(和发行版,如果适用,例如 Anaconda(:3.6.4
- 使用的虚拟环境类型(N/A | venv | virtualenv | conda | ...(: virtualenv
- 相关/受影响的 Python 包及其版本:pytest==3.2.1
文件夹结构:
-
src/- 所有代码
-
测试/- 所有测试
-
.env/- virtualenv
-
.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 测试日志:
[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 测试日志(见下文(,并注意到有关 ImportError 的错误。我修复了导入错误,然后在几秒钟后自动发现测试。
更多信息:要访问 Python 测试日志,请转到左侧边栏中的"测试">运行所有测试(图标有播放按钮和两个烧杯(,当您运行它时,您应该(希望(看到两个弹出通知:
- 发现测试时出错,继续测试
- 运行测试时出错
点击"查看输出'对于第二个。
我建议将Python扩展降级到2018.5.0。 GitHub 上有多个问题描述了运行测试的问题 (https://github.com/Microsoft/vscode-python/issues(。
Don Jayamanne本人在Github问题中回答了这一点: https://github.com/Microsoft/vscode-python/issues/2047