VS Code工作区:如何配置pytest调试以禁用justMyCode并进入第三方代码
VS Code中Pytest测试调试的justMyCode配置方案
需求
- 在「Testing」标签页运行pytest时禁用
justMyCode - 通过GUI调试单元测试时,允许单步进入第三方代码
通用配置(推荐)
不用为单个测试重复创建调试配置,可通过以下两种方式实现全局或测试专用的配置:
方式1:全局禁用justMyCode
打开VS Code设置(快捷键Ctrl+,),搜索python.debugger.justMyCode并设置为false,也可直接在settings.json中添加:
{ "python.debugger.justMyCode": false }
此配置会对所有Python调试场景生效,包括「Testing」标签页的测试调试,直接允许单步进入第三方库代码。
方式2:仅针对测试调试禁用justMyCode
若不想全局修改,可在launch.json中添加测试专用的调试模板:
"configurations": [ { "name": "Pytest: Debug All Tests", "type": "python", "module": "pytest", "request": "launch", "purpose": ["debug-test"], "console": "integratedTerminal", "justMyCode": false } ]
配置完成后,在「Testing」标签页调试任意测试时,VS Code会自动套用该配置,无需手动指定测试路径。
单个测试的临时调试配置
如果仅需调试某一特定测试用例,可创建如下指定路径的调试配置:
"configurations": [ { "name": "Debug specific test", "type": "python", "module": "pytest", "request": "launch", "purpose": ["debug-test"], "console": "integratedTerminal", "justMyCode": false, "args": [ "explorer/test/test_projects_controller.py::TestProjectsController::test_get_metadata" ] } ]
内容的提问来源于stack exchange,提问作者Kiruahxh
相关产品推荐
相关产品推荐

