我有一个项目(比如说A.csproj
)和一个测试项目(比如T.csproj
)。
T具有CCD_ 3和CCD_。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.SpecsE2EPoC.Specs.csproj" />
</ItemGroup>
</Project>
试试这个:
$>src> dotnet test <YOUR_PROJECT>.sln --no-build /p:CollectCoverage=true /p:CoverletOutput=..resultscoverage /p:MergeWith=..resultscoverage.json /p:CoverletOutputFormat=opencover
$>src> reportgenerator -reports:**/coverage.opencover.xml -targetdir:TestsresultsWebResult
$>src> TestsresultsWebResultindex.html