ChatGPT解决这个技术问题 Extra ChatGPT

Does Visual Studio have code coverage for unit tests?

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?


P
Peter Mortensen

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).


The new extension version is now available at the VS Gallery visualstudiogallery.msdn.microsoft.com/…
OpenCover UI doesn't seem to support NUnit 3
opencover.UI (that is integrated with VS) has very messy report (results window) that is simply impossible to use. go to opencover directly: allenconway.net/2015/06/…
"Latest release - 2016". Does not work with modern Visual Studio
G
Gordon Bean

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).

VS Extension page

https://github.com/axodox/AxoTools


It's worth noting that this (currently) only supports desktop versions of .NET - i.e. .NET Core doesn't appear to be supported at time of writing.
Dec 12, 2017, still not support for .NET Core / Xamarin
yup. no .net core. just realized after i installed. :( august 2018
As of March 2019, AxoCover also only supports up through xUnit 2.2 (which is already over two years old), with no plans at present to support xUnit 2.3 or 2.4. If you're hoping to run xUnit tests, don't bother.
It's 2020 June and the above comments still hold true, sadly.
A
Alexei - check Codidact

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.


The extension does not work on VS 2019 community. What a pity
@nicolascolman Yes, but Fine Code Coverage seems work.
B
Bas

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.


This is a good hint. However, it only works with .NET Core.
The comment above claims this only works with .NET Core but I'm using it with a .NET Framework Winforms project no problem. Not sure if it's been updated since that comment was written but for anyone reading now it seems to work well.
Works well but is very very slow. It "reacts" to the test explorer, and here it takes up to a minute to update every time I run a test. It may be due to an underlying component like coverlet or something and not FCC itself though.
O
Ogglas

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:

https://docs.microsoft.com/en-us/dotnet/core/testing/unit-testing-code-coverage?tabs=windows#generate-reports

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

p
ping

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


u
user19256035

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.