Code Coverage in .NET Core
Long story short, you will not be able to be confident your test suite covers the code as it changes until you can compute something called Code Coverage, or the amount of your code that is traversed during unit tests. Mind you, this is not a measure of how much of your functionality is covered, only the lines of code. In Visual Studio, they did a dirty deed and restricted the functionality to the Enterprise edition. But I am here to tell you that in .NET Core (and possibly for Framework, too, but I haven't tested it) it's very easy to have all the functionality and more even for the free version of Visual Studio.
These are the steps you have to take:
- Add coverlet.msbuild NuGet to your unit tests project
- Add ReportGenerator NuGet to your unit tests project
- Write a batch file that looks like
and save it in your unit test project folder - Optional: follow this answer on StackOverflow to be able to see the coverage directly in Visual Studio
Notes about the batch file:
- newest is the current version of ReportGenerator, if that doesn't work, change it with whatever version you have (ex: 4.3.0)
- the DAL filter tells the report to ignore projects with DAL in their name. You shouldn't have to unit test your data access layer.
- the ServiceCollectionExtensions.cs filter is for files that should be ignored, like extension methods rarely need to be unit tested
Running the batch should start dotnet test and save the result in both coverage.opencover.xml and also some files in the TestResults folder. Then ReportGenerator will generate an html file with the coverage report that will get open at the end. However, if you followed the optional answer, now you are able to open the files in TestResults and see what parts of your code are covered when opened in the Visual Studio editor! I found this less useful than the HTML report, but some of my colleagues liked the option.
I hope this helps you.
Comments
Be the first to post a comment