107

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Álvaro García
  • 18,114
  • 30
  • 102
  • 193

7 Answers7

93

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Toni Wenzel
  • 2,081
  • 2
  • 24
  • 39
  • The new extension version is now available at the VS Gallery https://visualstudiogallery.msdn.microsoft.com/6950a046-8919-4935-8542-c6f37956f688 – Toni Wenzel Sep 07 '15 at 21:14
  • 2
    OpenCover UI doesn't seem to support NUnit 3 – Lennart Sep 14 '16 at 13:14
  • 1
    opencover.UI (that is integrated with VS) has very messy report (results window) that is simply impossible to use. go to opencover directly: http://www.allenconway.net/2015/06/using-opencover-and-reportgenerator-to.html – Roman Pokrovskij Nov 28 '16 at 22:12
  • 3
    "Latest release - 2016". Does not work with modern Visual Studio – Alex from Jitbit Nov 21 '20 at 15:57
  • 1
    OpenCover repository is now in read-only. The author of OpenCover recommends AltCover: https://www.nuget.org/packages/altcover/ – Jean-Francois T. Mar 07 '23 at 03:05
48

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

Gordon Bean
  • 4,272
  • 1
  • 32
  • 47
  • 10
    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. – Jay May 11 '17 at 08:30
  • 6
    Dec 12, 2017, still not support for .NET Core / Xamarin – Hunter Tran Dec 07 '17 at 02:09
  • 1
    yup. no .net core. just realized after i installed. :( august 2018 – default_noob_network Aug 19 '18 at 19:10
  • 2
    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. – Sean Werkema Mar 12 '19 at 17:51
  • 2
    It's 2020 June and the above comments still hold true, sadly. – Ε Г И І И О Jun 03 '20 at 12:06
  • 1
    AxoCover does not support VS2019, as of this comment. The author has no time to put on this project anymore, unfortunately. https://github.com/axodox/AxoCover/issues/203#issuecomment-508544391 – Jerther Oct 05 '21 at 13:37
38

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.

  1. Install OpenCover NUGet component using NuGet interface

  2. Get OpenCoverUI extension. This can be installed directly from Visual Studio by using Tools -> Extensions and Updates

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

  1. Install ReportGenerator NUGet package

  2. Access OpenCover Test Explorer from OpenCover menu. Try discovering tests from there. If it fails, check Output windows for more details.

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

Alexei - check Codidact
  • 22,016
  • 16
  • 145
  • 164
10

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.

Bas
  • 101
  • 1
  • 2
  • 2
    This is a good hint. However, it only works with .NET Core. – Alexei - check Codidact Sep 25 '20 at 15:14
  • 2
    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. – Jberg Feb 06 '21 at 20:10
  • 1
    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. – Jerther Oct 05 '21 at 13:40
  • The readme for Fine Code Coverage is awful. It could do with something similar to this one: https://marketplace.visualstudio.com/items?itemName=ChrisDexter.RunCoverletReport – user1007074 Apr 11 '23 at 14:10
  • For VS 2022 there is new extension: https://marketplace.visualstudio.com/items?itemName=FortuneNgwenya.FineCodeCoverage2022 – milos Jul 04 '23 at 17:53
5

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:

enter image description here

While the article focuses on C# and xUnit as the test framework, both MSTest and NUnit would also work.

Guide:

https://learn.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
Ogglas
  • 62,132
  • 37
  • 328
  • 418
  • Just a note: The author doesn't seem to be updating the Marketplace link referenced in this answer. You can get the latest version from [github](https://github.com/FortuneN/FineCodeCoverage/releases). – Andy Feb 06 '23 at 21:21
3

Microsoft Visual Studio Enterprise 2022 has code coverage feature, nevertheless i prefere Fine Code Coverage.

A feature of Fine Code Coverage is the highlighting on the code being tested. You can see this here: enter image description here

Code coverage feature of Visual Studio Enterprise only: Code coverage feature of Visual Studio

enter image description here

The result of the built-in coverage tools looks like this: enter image description here

ping
  • 663
  • 6
  • 13
  • 2
    Professional doesn't have code coverage built in. The Requirements section of the link you posted states that Enterprise is required. – midoriha_senpai Sep 14 '22 at 20:47
  • @midoriha_senpai I appreciate your feedback. I installed by mistake the VS Enterprise, but now I'm back on Professional and there is defenetly no code coverage. I have made the appropriate corrections in the post – ping Sep 16 '22 at 05:12
  • Analyzing all tests at every turn can waste your time. You can also run code coverage from the Test Explorer window. Right click an item in Test Explorer and choose *Analyze Code Coverage* to analyze a specific class. – Mustafa Özçetin Mar 14 '23 at 12:02
2

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.

  • 2
    It is 2023 and obviously Microsoft still treats code coverage as a "luxury" and a prominent feature so they provide it in the Enterprise edition. Eclipse has this basic and essential feature for ages! – Mustafa Özçetin Mar 13 '23 at 14:28