答案:正确配置VS Code的launch.json文件可实现Jest和Pytest测试框架的高效调试。首先确保环境和依赖已安装,然后在项目根目录创建.vscod...(摘要截断,实际需完整)

要在 VS Code 中高效调试 Jest、Pytest 等测试框架,关键是正确配置 launch.json 文件,并确保相关环境和依赖已安装。下面分别介绍如何为不同测试框架设置调试功能。
适用于使用 Jest 进行 JavaScript/TypeScript 单元测试的项目。
前提条件:步骤:
{
"version": "0.2.0",
"configurations": [
{
"name": "Debug Jest Tests",
"type": "node",
"request": "launch",
"runtimeExecutable": "npx",
"runtimeArgs": ["jest", "--runInBand", "--coverage", "false"],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"disableOptimisticBPs": true,
"cwd": "${workspaceFolder}"
},
{
"name": "Debug Current Test File",
"type": "node",
"request": "launch",
"runtimeExecutable": "npx",
"runtimeArgs": [
"jest",
"${relativeFile}",
"--runInBand"
],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"disableOptimisticBPs": true,
"cwd": "${workspaceFolder}"
}
]
}说明:
--runInBand 防止并行执行,便于调试${relativeFile} 允许右键某个 test 文件时只运行该文件适用于使用 Pytest 的 Python 测试项目。
前提条件:方法一:通过命令面板快速调试
方法二:手动配置 launch.json
创建或编辑 .vscode/launch.json:
{
"version": "0.2.0",
"configurations": [
{
"name": "Debug Current Pytest File",
"type": "python",
"request": "launch",
"module": "pytest",
"args": [
"${file}",
"-v"
],
"console": "integratedTerminal",
"justMyCode": true,
"cwd": "${workspaceFolder}"
},
{
"name": "Debug Specific Test Function",
"type": "python",
"request": "launch",
"module": "pytest",
"args": [
"${file}::test_example_function",
"-v"
],
"console": "integratedTerminal",
"justMyCode": true,
"cwd": "${workspaceFolder}"
}
]
}说明:
module: "pytest" 表示调用 pytest 模块${file} 替换为当前打开的文件路径"python.defaultInterpreterPath": "./venv/bin/python"
sourceMap 开启并在 tsconfig.json 中配置正确基本上就这些。只要配置好 launch.json,VS Code 就能像调试普通脚本一样调试测试用例,支持断点、变量查看、调用栈等完整功能。
以上就是配置VS Code以调试Jest、Pytest等测试框架的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号