.net core projects code coverage visual studio 201

2019-03-09 09:34发布

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

6条回答
一纸荒年 Trace。
2楼-- · 2019-03-09 09:59

JDC's answer helped me to include my actual projects but I could not get rid of the test projects in the coverage report. I managed it by adding a "CodeCoverage.runsettings" in my XUnit test project with the following content:

<?xml version="1.0" encoding="utf-8"?>
<!-- File name extension must be .runsettings -->
<RunSettings>
  <DataCollectionRunSettings>
    <DataCollectors>
      <DataCollector friendlyName="Code Coverage" uri="datacollector://Microsoft/CodeCoverage/2.0" assemblyQualifiedName="Microsoft.VisualStudio.Coverage.DynamicCoverageDataCollector, Microsoft.VisualStudio.TraceCollector, Version=11.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
        <Configuration>
          <CodeCoverage>
            <ModulePaths>
              <Exclude>
                <ModulePath>.*\.Tests.dll$</ModulePath>
                <!-- Add more ModulePath nodes here. -->
              </Exclude>
            </ModulePaths>
          </CodeCoverage>
        </Configuration>
      </DataCollector>
    </DataCollectors>
  </DataCollectionRunSettings>
</RunSettings>

The important part here is to exclude the test project in the ModulePaths section.

查看更多
成全新的幸福
3楼-- · 2019-03-09 10:03

It looks like they are still working out the infrastructure (data collectors) bits before releasing. They are supposedly close.

https://github.com/Microsoft/vstest/issues/579

查看更多
女痞
4楼-- · 2019-03-09 10:07

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)
查看更多
兄弟一词,经得起流年.
5楼-- · 2019-03-09 10:17

You can try dotCover from Jetbrains. https://www.jetbrains.com/dotcover/features/

dotCover is a .NET unit testing and code coverage tool that works right in Visual Studio, helps you know to what extent your code is covered with unit tests, provides great ways to visualize code coverage, and is Continuous Integration ready. dotCover calculates and reports statement-level code coverage in applications targeting .NET Framework, Silverlight, and .NET Core.

It works fine with .NET Core & can show code coverage.

Supports multiple unit testing frameworks, namely MSTest, NUnit, xUnit (all out of the box) and MSpec (via a plug-in).

enter image description here

查看更多
SAY GOODBYE
6楼-- · 2019-03-09 10:17
Emotional °昔
7楼-- · 2019-03-09 10:18

Based on this article (Code Coverage does not work in the IDE with netcoreapp1.x projects (VS 2017 RTM):

Code coverage is currently not implemented for netcore projects. The work on this issue is in progress, it will come as part of post RTW releases.

查看更多
登录 后发表回答