如何在 VS Code 中进行 dotnet 代码覆盖率分析?

问题描述 投票:0回答:1

我编写了“NUnit”测试来测试 dotnet 代码。现在我想在 VS Code 中查看我的覆盖率统计数据。

有什么方法可以在VS Code中获得视觉/文本覆盖率分析用于dotnet单元测试

也许有一些好的扩展?

c# .net nunit
1个回答
0
投票

目前,我不知道 VS Code 中有任何扩展。但我找到了另一种在 VS Code 中查看覆盖率统计信息的方法。

您需要做的就是:

使用命令在终端中安装 dotnet-reportgenerator-globaltool:

dotnet tool install -g dotnet-reportgenerator-globaltool

使用命令从终端运行 dotnet 测试:

dotnet test /p:CollectCoverage=true /p:CoverletOutputFormat=lcov /p:CoverletOutput=lcov.info

配置添加到

tasks.json
文件夹中的
.vscode
文件(如果文件不存在,则创建新文件):

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Generate coverage stats",
            "command": "reportgenerator",
            "type": "shell",
            "args": [
                "-reports:${workspaceFolder}/YourUnitTestProjectFolder/lcov.info",
                "-targetdir:${workspaceFolder}/YourUnitTestProjectFolder/covstats"
            ],
            "problemMatcher": []
        }
    ]
}
通过按

Ctrl + Shift + P 并执行 Tasks: Run task

 命令,在 VS Code 中启动
任务。

然后

Generate coverage stats


在您的测试项目中应该出现

covstats

 文件夹,其中包含 
index.html
 文件。用鼠标右键
单击Show preview
该文件。将出现覆盖率分析。

归功于:https://jasonwatmore.com/net-vs-code-xunit-setup-unit-testing-code-coverage-in-aspnet-core .

© www.soinside.com 2019 - 2024. All rights reserved.