.net 核心项目代码覆盖率 visual studio 2017

.net core projects code coverage visual studio 2017

我正在使用 Visual Studio Enterprise 2017 开发我的 .net 核心项目。

我的解决方案还有一些单元测试项目,我想查看我当前的代码覆盖率。

当我点击测试 -> 分析代码覆盖率 -> 所有测试时。我所有的单元测试 运行 但在代码覆盖率结果中它只报告了我的单元测试项目的代码覆盖率,这对我来说没有任何意义。

问题1:你们有没有和我遇到同样的问题?有什么解决办法吗?我还需要在 VSTS 中设置构建定义以报告代码覆盖率。

问题 2:还有当我右击调试我的单元测试 1 时。它执行我所有的单元测试。这是 VS2017 中的错误吗?

更新 1:我找到了这个主题:https://github.com/Microsoft/vstest/issues/597

基于本文 (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.

看起来他们在发布之前仍在研究基础设施(数据收集器)位。他们应该很接近。

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

您可以试用 Jetbrains 的 dotCover。 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.

它适用于 .NET Core,并且可以显示代码覆盖率。

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

版本 15.3 已修复此问题:

https://github.com/Microsoft/vstest-docs/blob/master/docs/analyze.md#coverage

即使使用 "fixed" 版本我也有问题(只能看到测试项目的代码覆盖率,而不是我正在测试的实际项目)。为了解决这个问题,我从测试项目中删除了 <DebugType>Full</DebugType>

所以,总结一下:

  1. 将包 Microsoft.CodeCoverage (1.0.3) 添加到您的测试项目中。

  2. 在 .csproj 文件上添加 <DebugType>Full</DebugType> 标签 我们希望看到代码覆盖值的项目(在 <PropertyGroup> 内,如 vstest github link 中所述)。

  3. 运行 单元测试。
  4. 在 "Test Explorer" select 通过单元测试时,右键单击 -> "Analyze Code Coverage for Selected Tests"。
  5. 您应该会看到程序集的代码覆盖率。

我的设置(最小设置?)

  • xunit (2.3.1)
  • xunit.runner.visualstudio (2.3.1)
  • Microsoft.NET.Test.Sdk (15.3.0)
  • Microsoft.CodeCoverage (1.0.3)
  • Visual Studio 企业版 2017 (15.4.1)

JDC 的回答帮助我包含了我的实际项目,但我无法在覆盖率报告中删除测试项目。 我通过在我的 XUnit 测试项目中添加一个 "CodeCoverage.runsettings" 来管理它,内容如下:

<?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>

这里的重要部分是排除 ModulePaths 部分中的测试项目。