I am using Visual Studio 2015 Community edition, and I know that it has the option to create unit tests to test the code, but I don't see the option to test the code coverage, so I would like to know if Visual Studio has this option or if I have to use a third-party plugin.
If I have to use a third-party solution, would that be a good option?
Only Visual Studio 2015 Enterprise has code coverage built-in. See the feature matrix for details.
You can use the OpenCover.UI extension for code coverage check inside Visual Studio. It supports MSTest, nUnit, and xUnit.
The new version can be downloaded from here (release notes).
If you are using Visual Studio 2017 and come across this question, you might consider AxoCover. It's a free VS extension that integrates OpenCover, but supports VS2017 (it also appears to be under active development. +1).
https://github.com/axodox/AxoTools
Toni
's answer is very useful, but I thought a quick start for total beginners to test coverage assessment (like I am).
As already mentioned, Visual Studio Professional and Community Editions do not have built-in test coverage support. However, it can be obtained quite easily. I will write step-by-step configuration for use with NUnit tests within Visual Studion 2015 Professional.
Install OpenCover NUGet component using NuGet interface Get OpenCoverUI extension. This can be installed directly from Visual Studio by using Tools -> Extensions and Updates Configure OpenCoverUI to use the appropriate executables, by accessing Tools -> Options -> OpenCover.UI Options -> General
NUnit Path: must point to the `nunit-console.exe file. This can be found only within NUnit 2.xx version, which can be downloaded from here.
OpenCover Path: this should point to the installed package, usually <solution path>\packages\OpenCover.4.6.519\tools\OpenCover.Console.exe
Install ReportGenerator NUGet package Access OpenCover Test Explorer from OpenCover menu. Try discovering tests from there. If it fails, check Output windows for more details. Check OpenCover Results (within OpenCover menu) for more details. It will output details such as Code Coverage in a tree based view. You can also highlight code that is or is not covered (small icon in the top-left).
NOTE: as mentioned, OpenCoverUI does not support latest major version of NUnit (3.xx). However, if nothing specific to this version is used within tests, it will work with no problems, regardless of having installed NUnit 3.xx version.
This covers the quick start. As already mentioned in the comments, for more advanced configuration and automation check this article.
For anyone that is looking for an easy solution in Visual Studio Community 2019, Fine Code Coverage is simple but it works well.
It cannot give accurate numbers on the precise coverage, but it will tell which lines are being covered with green/red gutters.
As already mentioned you can use Fine Code Coverage that visualize coverlet output. If you create a xunit test project
(dotnet new xunit
) you'll find coverlet reference already present in csproj
file because Coverlet
is the default coverage tool for every .NET Core and >= .NET 5 applications.
Microsoft has an example using ReportGenerator that converts coverage reports generated by coverlet, OpenCover, dotCover, Visual Studio, NCover, Cobertura, JaCoCo, Clover, gcov or lcov into human readable reports in various formats.
Example report:
https://i.stack.imgur.com/RH56M.png
While the article focuses on C# and xUnit as the test framework, both MSTest and NUnit would also work.
Guide:
If you want code coverage in .xml files you can run any of these commands:
dotnet test --collect:"XPlat Code Coverage"
dotnet test /p:CollectCoverage=true /p:CoverletOutputFormat=cobertura
Microsoft Visual Studio Professional 2022 has code coverage feature, but i prefere Fine Code Coverage.
https://i.stack.imgur.com/khuyf.png
Code coverage feature of Visual Studio: Code coverage feature of Visual Studio
https://i.stack.imgur.com/BQOzA.png
https://i.stack.imgur.com/cCxh5.png
Microsoft Visual Studio Professional 2022 has code coverage feature
No Professional, only Enterprise :-(
Code coverage feature of Visual Studio: Code coverage feature of Visual Studio
Requirements: The code coverage feature is available only in Visual Studio Enterprise edition.
Success story sharing