如何从代码覆盖率统计中排除(带条件的)链接源文件
解决方案:排除链接文件的代码覆盖率统计
针对VS2022 Community + xUnit + coverlet.collector + Fine Code Coverage的场景,要排除ref/common下的链接文件出代码覆盖率,试下面几个可行方案:
方案1:给目标文件添加排除标记(最精准)
直接在被测试项目的.csproj里,给所有ref/common下的链接文件加上ExcludeFromCodeCoverage元数据,标记这些文件不参与覆盖率统计:
<!-- 原有的Compile ItemGroup,给每个文件加ExcludeFromCodeCoverage="true" --> <ItemGroup> <Compile Include="..\..\ref\common\Argument.IReadOnlyLIst.VerifyNotEmpty.cs" Link="ref\common\Argument.IReadOnlyLIst.VerifyNotEmpty.cs" ExcludeFromCodeCoverage="true" /> <Compile Include="..\..\ref\common\Argument.IReadOnlyList.VerifyNotNull.cs" Link="ref\common\Argument.IReadOnlyList.VerifyNotNull.cs" ExcludeFromCodeCoverage="true" /> <Compile Include="..\..\ref\common\Argument.VerifyNotNull.cs" Link="ref\common\Argument.VerifyNotNull.cs" ExcludeFromCodeCoverage="true" /> <Compile Include="..\..\ref\common\Object.GetObjectDisposedException.cs" Link="ref\common\Object.GetObjectDisposedException.cs" ExcludeFromCodeCoverage="true" /> <Compile Include="..\..\ref\common\String.VerifyNotEmpty.cs" Link="ref\common\String.VerifyNotEmpty.cs" ExcludeFromCodeCoverage="true" /> <Compile Include="..\..\ref\common\Type.GetStaticMethodInfo.cs" Link="ref\common\Type.GetStaticMethodInfo.cs" ExcludeFromCodeCoverage="true" /> <Compile Include="..\..\ref\common\Type.ResolveStaticGetter.cs" Link="ref\common\Type.ResolveStaticGetter.cs" ExcludeFromCodeCoverage="true" /> <Compile Include="..\..\ref\common\IReadOnlyList.IndexOf.cs" Link="ref\common\IReadOnlyList.IndexOf.cs" ExcludeFromCodeCoverage="true" /> <Compile Include="..\..\ref\common\AppendOnlyList.cs" Link="ref\common\AppendOnlyList.cs" ExcludeFromCodeCoverage="true" /> <Compile Include="..\..\ref\common\ListOfT.MoveRange.cs" Link="ref\common\LIstOfT.MoveRange.cs" ExcludeFromCodeCoverage="true" /> </ItemGroup> <!-- 带条件的ItemGroup同样添加标记 --> <ItemGroup Condition="('$(TargetFramework)' != 'netstandard2.0' and '$(TargetFramework)' != 'net461')"> <Compile Include="..\..\ref\common\System.Text.ValueStringBuilder.cs" Link="ref\common\System.Text.ValueStringBuilder.cs" ExcludeFromCodeCoverage="true" /> <Compile Include="..\..\ref\common\UInt32.AppendTo.cs" Link="ref\common\UInt32.AppendTo.cs" ExcludeFromCodeCoverage="true" /> </ItemGroup>
修改后重新生成解决方案,运行测试即可生效。
方案2:修正coverlet的runsettings配置
你之前的runsettings无效,是因为用了VS自带覆盖率工具的<Sources>配置,而你用的是coverlet.collector,需要用coverlet对应的配置结构:
- 创建完整的
CodeCoverage.runsettings文件:
<?xml version="1.0" encoding="utf-8"?> <RunSettings> <DataCollectionRunSettings> <DataCollectors> <DataCollector friendlyName="XPlat Code Coverage"> <Configuration> <Exclude> <!-- 匹配所有ref/common下的cs文件,支持通配符 --> <Source>**/ref/common/**/*.cs</Source> </Exclude> </Configuration> </DataCollector> </DataCollectors> </DataCollectionRunSettings> </RunSettings>
- 在VS中指定使用该文件:
- 点击顶部菜单测试 → 配置运行设置 → 选择解决方案的运行设置文件
- 选中刚创建的
CodeCoverage.runsettings,清理并重新生成后运行测试。
方案3:Fine Code Coverage面板直接过滤
如果上面两种方法没生效,可直接通过Fine Code Coverage的面板设置排除规则:
- 运行测试后,打开VS右侧的Fine Code Coverage面板
- 点击面板右上角的齿轮图标(设置)
- 在Exclude Patterns输入框中添加:
**/ref/common/**/*.cs - 保存设置后重新运行测试,即可看到排除效果。
内容的提问来源于stack exchange,提问作者Weifen Luo
相关产品推荐
相关产品推荐

