You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VSCode运行Debug Test时Python代码断点失效问题求助

解决VSCode Python测试调试断点无效及launch.json不生效问题

1. 强制绑定自定义调试配置

右键「Debug Test」默认会用自动生成的临时配置,而非你写的launch.json。可以通过两种方式强制关联:

  • 打开测试面板,找到目标测试用例,右键选「Debug with Specific Configuration」,挑你定义的「Python: Debug Tests」配置。
  • 直接修改远程窗口的settings.json(Ctrl+Shift+P搜「Remote: Open Settings (JSON)」),添加以下配置锁定测试调试规则:
    "python.testing.debugEnabled": true,
    "python.testing.debugPort": 5678,
    "python.testing.pytestArgs": ["-xvs", "${workspaceFolder}/tests/"]
    

2. 修正launch.json配置结构

确保你的调试配置格式完全符合要求,尤其是purpose和justMyCode参数:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Debug Tests",
            "type": "debugpy",
            "request": "launch",
            "purpose": ["debug-test"],
            "program": "${file}",
            "args": ["-xvs", "${workspaceFolder}/tests/"],
            "console": "integratedTerminal",
            "justMyCode": false // 关键:默认开启会过滤非用户代码断点,测试场景建议关闭
        }
    ]
}

3. 排查SSH远程路径映射问题

因为是SSH连接VM,路径不匹配会导致调试器找不到断点文件:

  • 确认远程settings.json里的python.pythonPath指向VM中正确的解释器,比如:
    "python.pythonPath": "/home/your-user/venv/bin/python"
    
  • launch.json里的program和args必须用远程路径,比如${workspaceFolder}对应VM的工作目录,别写本地绝对路径。

4. 彻底清理测试缓存

手动清理VSCode远程缓存,避免旧配置干扰:

  • 关闭VSCode,删除VM上的扩展缓存目录:~/.vscode-server/extensions/ms-python.python-2024.14.1(对应你的Python扩展版本),重启VSCode重新加载扩展。
  • 在VSCode里执行「Python: Clear Test Cache」命令(Ctrl+Shift+P搜索)。

5. 切换到Attach模式调试

如果Launch模式始终无效,试试Attach模式绕开自动配置:

  • 在远程终端手动启动测试并监听调试端口:
    python -m pytest tests/test_demo.py -xvs --debugpy --listen 0.0.0.0:5678 --wait-for-client
    
  • 在launch.json里添加Attach配置:
    {
        "name": "Python: Attach to Test",
        "type": "debugpy",
        "request": "attach",
        "connect": {
            "host": "localhost",
            "port": 5678
        },
        "justMyCode": false
    }
    
  • 启动这个Attach配置,测试会继续执行,此时断点应该能触发。

6. 检查扩展版本兼容性

当前Python扩展v2024.14.1和Python Debugger v2024.10.0可能存在版本不匹配问题:

  • 尝试回退Python Debugger到上一个稳定版本(比如v2024.9.0),或者更新到最新版本,再测试调试效果。

内容的提问来源于stack exchange,提问作者Rolf

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 09:23:26