我有一个包含多个单元测试项目的解决方案。
所以这里的目标是排除 Migrations 文件夹
我在测试项目的根目录中添加了一个名为 .runsettings 的运行设置文件
<RunSettings>
<DataCollectionRunSettings>
<DataCollectors>
<DataCollector friendlyName="XPlat code coverage">
<Configuration>
<Format>json,cobertura,lcov,teamcity,opencover</Format>
<Exclude>[*]stackoverflow.Infrastructure.Migrations*</Exclude>
<IncludeTestAssembly>false</IncludeTestAssembly>
<DeterministicReport>false</DeterministicReport>
</Configuration>
</DataCollector>
</DataCollectors>
</DataCollectionRunSettings>
</RunSettings>
我们在 azure-pipelines.yml 中引用了这样的运行设置文件:
- task: DotNetCoreCLI@2
displayName: Test
inputs:
command: test
projects: '**/*Tests.csproj'
arguments: '--collect "XPlat Code Coverage" --configuration $(buildConfiguration) --settings $(Build.Repository.LocalPath)\Tests\stackoverflow.Infrastructure.Tests\.runsettings'
执行此操作后,覆盖结果消失了!
你尝试过这样的事情吗?我遇到了类似的问题,在添加 CodeCoverage 块并排除 ModulePath 后,它确实起作用了。
<RunSettings>
<DataCollectionRunSettings>
<DataCollectors>
<DataCollector friendlyName="XPlat code coverage">
<Configuration>
<CodeCoverage>
<ModulePaths>
<Exclude>
<ModulePath>.*stackoverflow.Infrastructure.Migrations.*</ModulePath>
</Exclude>
</ModulePaths>
</CodeCoverage>
</Configuration>
</DataCollector>
</DataCollectors>
</DataCollectionRunSettings>
</RunSettings>
跳过 .runsettings 文件并直接在测试参数中提供排除信息,如从覆盖范围中排除中所述:
arguments: '--collect "XPlat Code Coverage" /p:Exclude="[*]stackoverflow.Infrastructure.Migrations*"
经过多次尝试和错误,我意识到以下几点:
# restore and build tasks before..
- task: DotNetCoreCLI@2
displayName: 'dotnet test'
inputs:
command: 'test'
projects: '*.sln'
# important is only --settings and it's value. Settings like /p:CollectCoverage=true /p:CoverletOutputFormat=cobertura are kind of redundant
arguments: '--nologo --no-build --no-restore --settings "$(Build.SourcesDirectory)\CodeCoverage.runsettings"'
publishTestResults: true
# Not sure, if reportgenerator is generally available
- task: reportgenerator@5
displayName: Generate reports
inputs:
reports: '$(Agent.TempDirectory)/*/*cobertura.xml'
targetdir: '$(Agent.TempDirectory)'
reporttypes: 'Cobertura;HtmlInline_AzurePipelines_Dark'
publishCodeCoverageResults: true
## works fine, repaced by reportgenerator
# - task: PublishCodeCoverageResults@2
# displayName: 'Publish code coverage (XML)'
# inputs:
# summaryFileLocation: '$(Agent.TempDirectory)/**/coverage.cobertura.xml'
但是有一个问题:它看起来像是对“XPlat Code Coverage”的简单调用
arguments: '... --collect:"XPlat Code Coverage"'
产生除调用使用 .runsettings 文件之外的其他结果...
arguments: '... --settings "$(Build.SourcesDirectory)\.runsettings"'
使用基本上空的 .runsettings 文件,如下所示:
<?xml version="1.0" encoding="utf-8" ?>
<RunSettings>
<DataCollectionRunSettings>
<DataCollectors>
<DataCollector friendlyName="XPlat code coverage">
<Configuration>
<Format>cobertura</Format>
</Configuration>
</DataCollector>
</DataCollectors>
</DataCollectionRunSettings>
</RunSettings>
我希望未使用的设置默认为与 --collect:"XPlat Code Coverage"'
使用的相同值,但事实并非如此。就我而言,当我使用 .runsettings 文件时,无法正确检测到某些被覆盖的行(有时它会影响具有多种方法的文件以及使用 `--collect:"XPlat Code Coverage" 时正确检测到的大量代码)但是文档来拯救:
高级选项(通过运行设置支持)提到“过滤详细信息存在于 msbuild 指南中。”,这导致“从覆盖范围中排除”。该区域显示了如何在控制台(或天蓝色管道调用)中排除内容(例如按属性)。