You need to enable JavaScript to run this app. 如何通过dotnet test的runsettings参数排除单元测试的代码覆盖率统计
优惠活动
大模型
产品
解决方案
定价
更多

如何通过dotnet test的runsettings参数排除单元测试的代码覆盖率统计

正确配置方案

核心错误原因

  • 代码覆盖率的ModulePath属于数组类型配置,多规则必须加[索引]标识下标
  • 你之前的命令缺失了Include规则,仅配置Exclude会导致匹配逻辑不生效
  • --collect "Code coverage"多打了空格,会导致采集器识别异常

可用命令

Windows(PowerShell/CMD)

PowerShell环境直接执行:

dotnet test --filter "TestCategory != Ignore" --no-build --no-restore -v normal --collect "Code coverage" -- `
DataCollectionRunSettings.DataCollectors.DataCollector.Configuration.CodeCoverage.ModulePaths.Include.ModulePath[0]=".*\.dll$" `
DataCollectionRunSettings.DataCollectors.DataCollector.Configuration.CodeCoverage.ModulePaths.Include.ModulePath[1]=".*\.exe$" `
DataCollectionRunSettings.DataCollectors.DataCollector.Configuration.CodeCoverage.ModulePaths.Exclude.ModulePath[0]=".*\\[^\\]*UnitTests[^\\]*\.dll"

如果使用CMD,将换行符的反引号替换为^`即可。

Linux/macOS(bash/zsh)

终端内反斜杠需要额外转义,使用以下命令:

dotnet test --filter "TestCategory != Ignore" --no-build --no-restore -v normal --collect "Code coverage" -- \
DataCollectionRunSettings.DataCollectors.DataCollector.Configuration.CodeCoverage.ModulePaths.Include.ModulePath[0]=".*\\.dll$" \
DataCollectionRunSettings.DataCollectors.DataCollector.Configuration.CodeCoverage.ModulePaths.Include.ModulePath[1]=".*\\.exe$" \
DataCollectionRunSettings.DataCollectors.DataCollector.Configuration.CodeCoverage.ModulePaths.Exclude.ModulePath[0]=".*\\\\[^\\\\]*UnitTests[^\\\\]*\\.dll"

验证方式:执行完成后打开生成的覆盖率报告,确认所有名称带UnitTests的程序集已不在统计范围内即可。

内容的提问来源于stack exchange,提问作者Ofek

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.21 21:06:11