code-coverage在vscode中启用
时间: 2024-02-24 20:55:39 浏览: 262
在VS Code中启用Code Coverage可以通过以下步骤实现:
1. 安装Code Coverage插件
在VS Code中搜索Code Coverage插件并安装。
2. 配置launch.json文件
在VS Code中打开项目,选择Debug -> Open Configurations打开launch.json文件。
在launch.json文件中添加以下配置:
```
"configurations": [
{
"name": "Debug with Coverage",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/build/bin/myapp",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "/usr/bin/gdb",
"preLaunchTask": "build",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "Enable code coverage",
"text": "set coverage on",
"ignoreFailures": true
},
{
"description": "Set gdb working directory",
"text": "cd ${workspaceFolder}/build",
"ignoreFailures": true
}
],
"postDebugTask": "coverage",
"coverage": {
"enable": true,
"outputPath": "${workspaceFolder}/coverage_result",
"exclude": [
"*/usr/include/*",
"*/test/*"
],
"include": [
"${workspaceFolder}/*"
],
"report": [
"html",
"lcov"
],
"excludeSources": [],
"excludeHeaders": [],
"excludeLibraries": []
}
}
]
```
其中,program表示要运行的程序,preLaunchTask表示在启动程序之前先执行的任务,postDebugTask表示在结束调试之后执行的任务,coverage表示代码覆盖率相关的配置。
3. 配置tasks.json文件
在VS Code中打开项目,选择Tasks -> Configure Tasks打开tasks.json文件。
在tasks.json文件中添加以下配置:
```
{
"label": "coverage",
"type": "shell",
"command": "lcov -c -d build/CMakeFiles/myapp.dir -o build/app.info && genhtml build/app.info -o build/coverage_report"
}
```
其中,command表示要执行的命令,lcov和genhtml是生成代码覆盖率报告的工具。
4. 生成代码覆盖率报告
在VS Code中启动Debug with Coverage配置,运行测试,并且结束调试之后,会自动执行coverage任务,生成代码覆盖率报告。
5. 查看代码覆盖率报告
在VS Code中打开生成的HTML代码覆盖率报告,即可查看代码覆盖率情况。
通过以上步骤,就可以在VS Code中启用Code Coverage,并生成HTML格式的代码覆盖率报告了。
阅读全文