【问题标题】:Code coverage gets generated only for Tests project仅为测试项目生成代码覆盖率
【发布时间】:2023-02-16 13:14:57
【问题描述】:

我有一个项目(比如A.csproj)和一个测试项目(比如T.csproj)。

T 有 coverlet.msbuildMicrosoft.NET.Test.Sdk 作为 nuget 引用。 T 有一个不包含包含/排除路径的 .runsettings 文件(包括所有程序集,不排除)。

执行dotnet test T.csproj /p:CollectCoverage=true /p:IncludeTestAssembly=true /p:CoverletOutputFormat=cobertura --settings:".runsettings",只会为测试项目生成覆盖率:

+--------------------+--------+--------+--------+
| Module             | Line   | Branch | Method |
+--------------------+--------+--------+--------+
| T                  | 98.87% | 100%   | 96.29% |
+--------------------+--------+--------+--------+

+---------+--------+--------+--------+
|         | Line   | Branch | Method |
+---------+--------+--------+--------+
| Total   | 98.87% | 100%   | 96.29% |
+---------+--------+--------+--------+
| Average | 98.87% | 100%   | 96.29% |
+---------+--------+--------+--------+

如果我删除/p:IncludeTestAssembly=true,只会生成一个空报告:

| Module | Line | Branch | Method |
+--------+------+--------+--------+

+---------+------+--------+--------+
|         | Line | Branch | Method |
+---------+------+--------+--------+
| Total   | 0%   | 0%     | 0%     |
+---------+------+--------+--------+
| Average | 0%   | 0%     | 0%     |
+---------+------+--------+--------+

即使我通过 cmdline 本身指定包含所有模块,也会发生同样的情况:dotnet test T.csproj /p:CollectCoverage=true /p:CoverletOutputFormat=cobertura /p:Include="[*]*"

T.csproj的内容

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

  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <!--<IsPackable>false</IsPackable>-->

  <PropertyGroup>
    <ProduceReferenceAssembly>false</ProduceReferenceAssembly>
    <GenerateDocumentationFile>false</GenerateDocumentationFile>
  </PropertyGroup>

  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
    <PlatformTarget>x64</PlatformTarget>
  </PropertyGroup>

  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
    <PlatformTarget>x64</PlatformTarget>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="coverlet.collector" Version="3.1.2">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="coverlet.msbuild" Version="3.1.2">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.0.0" />
    <PackageReference Include="Moq" Version="4.18.1" />
    <PackageReference Include="MSTest.TestAdapter" Version="2.2.10" />
    <PackageReference Include="MSTest.TestFramework" Version="2.2.10" />
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\E2EPoC.Specs\E2EPoC.Specs.csproj" />
  </ItemGroup>

</Project>

【问题讨论】:

  • 可以放T.csproj的内容吗

标签: .net .net-core code-coverage mstest coverlet


【解决方案1】:

尝试这个:

$>src> dotnet test <YOUR_PROJECT>.sln --no-build /p:CollectCoverage=true /p:CoverletOutput=..
esultscoverage /p:MergeWith=..
esultscoverage.json /p:CoverletOutputFormat=opencover
$>src> reportgenerator -reports:**/coverage.opencover.xml -targetdir:Tests
esultsWebResult
$>src> Tests
esultsWebResultindex.html

【讨论】:

    猜你喜欢
    • 2012-01-07
    • 2017-09-03
    • 2020-08-28
    • 2023-04-07
    • 2018-05-21
    • 2019-09-02
    • 2017-04-24
    • 2017-02-11
    • 1970-01-01
    相关资源
    最近更新 更多