Visual Studio Code中Test Explorer覆盖率运行图标未显示,求启用方法
问题
我正在使用Python pytest开展测试工作,通过Remote-SSH扩展远程连接Linux Ubuntu服务器。当前Visual Studio Code的Test Explorer中未显示覆盖率运行图标,已附上Testing面板截图及Settings.json配置内容:

{ "python.analysis.autoImportCompletions": true, "python.analysis.fixAll": ["source.unusedImports"], "editor.defaultFormatter": "charliermarsh.ruff", "files.exclude": { "**/__pycache__": true, "**/.cache": true, "**/.coverage": true, "**/.coverage.*": true, "**/.hypothesis": true, "**/.mypy_cache": true, "**/.nox": true, "**/.pytest_cache": true, "**/.ruff_cache": true, "**/.tox": true }, "python.testing.pytestEnabled": true }
请问如何在VS Code界面中启用覆盖率功能?
解决方案
- 安装覆盖率依赖包
在远程Ubuntu服务器的目标Python环境中安装pytest-cov:
pip install pytest-cov
- 添加VS Code覆盖率配置
编辑你的settings.json,加入以下两项配置:
"python.testing.pytestArgs": [ "--cov", "--cov-report=term-missing" ], "python.testing.coverageEnabled": true
重载VS Code窗口
按下Ctrl+Shift+P,执行Reload Window命令,让配置生效。验证功能
重新打开Test Explorer,此时会显示覆盖率运行图标,点击即可生成测试覆盖率报告。
注意:要确保pytest-cov安装在Remote-SSH连接的远程Python环境中,若使用虚拟环境,需先激活对应环境再安装依赖。
内容的提问来源于stack exchange,提问作者Michael
相关产品推荐
相关产品推荐

