首页 文章

Visual Studio 2017中.NET Core 2.0项目的代码覆盖率

提问于
浏览
2

Version: 15.4.1 - Visual Studio Enterprise 2017

我创建了一个简单的.NET Core 2.0库项目,并为其添加了一个UnitTest项目 . 当我运行Test - > Analyze Code Coverage - > All tests . 代码覆盖率结果仅报告单元测试项目的代码覆盖率 .

enter image description here

我已经根据Working with Code Coverage配置了项目 . 这是我的.csproj:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netcoreapp2.0</TargetFramework>

    <IsPackable>false</IsPackable>

    <!-- Required in both test/product projects. This is a temporary workaround for https://github.com/Microsoft/vstest/issues/800 -->
    <DebugType>Full</DebugType>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0" />
    <PackageReference Include="MSTest.TestAdapter" Version="1.2.0" />
    <PackageReference Include="MSTest.TestFramework" Version="1.2.0" />

    <!-- Required. Include this reference for coverage -->
    <PackageReference Include="Microsoft.CodeCoverage" Version="1.0.3" />
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\ClassLibrary1\ClassLibrary1.csproj" />
  </ItemGroup>

</Project>

Update: 我在解决方案中添加了与.NET 4.6.2项目相同的代码,并再次运行代码覆盖率 . 它正如您所期望的那样覆盖类库:

Code coverage covers the code of the class library

因此我的Visual Studio已正确配置为运行代码覆盖率 . 至少对于.NET 4.6.2 . 我也在新的VS安装和VSTS中尝试过它 .

我创建了一个repo with my sample code . 其他人可以重现这个吗?

1 回答

  • 2

    有一个我没有完全理解的解决方法 .

    您必须将 <DebugType>Full</DebugType> 添加到需要覆盖的 all the projects . 不是单元测试本身 .

相关问题