0

I wrote NUnit tests to test .NET code. Now I want to see my coverage statistics in VS Code.

Is there any way to get visual/textual coverage analysis for .NET unit tests in VS Code?

Maybe there is some good extension for that?

Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
Yoro
  • 805
  • 8
  • 17

1 Answers1

0

Currently, I don't know any extensions for that in VS Code. But I found another way to see coverage stats in VS Code.

All you need to do is:

Install .NET report generator in terminal with command:

dotnet tool install -g dotnet-reportgenerator-globaltool

Add Coverlet MS build package from NuGet to testing project

dotnet add package coverlet.msbuild

Run dotnet tests from terminal with command:

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

Add configuration to tasks.json file in .vscode folder (If file doesn't exist create new one):

{
    // 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": []
        }
    ]
}

Launch task in VS Code by pressing Ctrl + Shift + P and executing Tasks: Run task command. Tasks: Run task

And then press Generate coverage stats

Generate coverage stats

In your test project should appear covstats folder which contain index.html file. With right mouse button click Show preview on that file. Coverage analysis will appear.

enter image description here

Creds to: https://jasonwatmore.com/net-vs-code-xunit-setup-unit-testing-code-coverage-in-aspnet-core .

Yoro
  • 805
  • 8
  • 17
  • So your real question is how to view Coverlet output in VS Code? Googling for `VS Code Coverlet` brings up [a Coverlet extension](https://marketplace.visualstudio.com/items?itemName=urbanoanderson.vscode-coverlet), an [article by Scott Hanselman](https://www.hanselman.com/blog/automatic-unit-testing-in-net-core-plus-code-coverage-in-visual-studio-code) that points to the [Coverage Gutters](https://marketplace.visualstudio.com/items?itemName=ryanluker.vscode-coverage-gutters) extension. – Panagiotis Kanavos Aug 24 '23 at 09:14
  • Report Generator is mentioned in the .NET Docs, at [Use code coverage for unit testing](https://learn.microsoft.com/en-us/dotnet/core/testing/unit-testing-code-coverage?tabs=windows) – Panagiotis Kanavos Aug 24 '23 at 09:16
  • @Panagiotis-Kanavos Thanks for the comment. Currently, I haven't found how to preview coverage reports with coverage gutters. With coverage gutters, I can only visually see line coverage like mentioned here https://stackoverflow.com/questions/76946038/how-do-i-see-dotnet-line-coverage-in-vscode . – Yoro Aug 24 '23 at 10:00