Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.net core projects code coverage visual studio 2017

Tags:

I am using Visual Studio Enterprise 2017 to develop my .net core projects.

My solution also has some unit test projects and I want to view my current Code coverage.

When I clicked Test -> Analyze Code Coverage -> All tests. All my unit tests ran but in Code Coverage Results it only reported code coverage for my unit test projects which doesn't make any sense to me.

Question 1: Do you guys experience the same issue with me? Any solution for this? I also need to set up build definition in VSTS to report code coverage.

enter image description here

Question 2: Also when I right clicked to debug 1 of my unit test. It executes ALL of my unit tests. Is this a bug in VS2017?

Update1: I found this topic: https://github.com/Microsoft/vstest/issues/597

like image 361
Hung Cao Avatar asked Mar 25 '17 00:03

Hung Cao


People also ask

How do I get code coverage in Visual Studio 2017?

Please first write the unit test for the functional method (has input and output, and they have fixed relationship) you want to test in visual studio 2017 enterprise. Then, choose Analyze Code Coverage on the Test menu after ran your unit test code in Test Explorer.

How do I show code coverage in Visual Studio?

On the Test menu, select Analyze Code Coverage for All Tests. You can also run code coverage from the Test Explorer tool window. Show Code Coverage Coloring in the Code Coverage Results window. By default, code that is covered by tests is highlighted in light blue.


1 Answers

Even with the "fixed" version I had issues (Only able to see code coverage for the test projects, not the actual projects I was testing). To workaround the problem I removed the <DebugType>Full</DebugType> from the test project.

So, as a summary:

  1. Add package Microsoft.CodeCoverage (1.0.3) into your test project.

  2. Add the <DebugType>Full</DebugType> tag on the .csproj file on the projects we want to see code coverage values (inside <PropertyGroup> as explained on the vstest github link).

  3. Run unit tests.
  4. On the "Test Explorer" select passed unit tests, right click -> "Analyze Code Coverage for Selected Tests".
  5. You should see code coverage for your assemblies.

My setup (Minimal setup?)

  • xunit (2.3.1)
  • xunit.runner.visualstudio (2.3.1)
  • Microsoft.NET.Test.Sdk (15.3.0)
  • Microsoft.CodeCoverage (1.0.3)
  • Visual Studio Enterprise 2017 (15.4.1)
like image 65
JDC Avatar answered Dec 13 '22 10:12

JDC