如果线路覆盖率低于 60%,我想强制我的 Azure 管道失败。
看起来很简单。我的管道结果中已经显示了行覆盖率,但如果行覆盖率低于 60%,我找不到强制 VSTest@2 任务失败的方法。:
有人可以帮我找到一种方法吗?
这是我的管道脚本:
# ASP.NET
# Build and test ASP.NET projects.
# Add steps that publish symbols, save build artifacts, deploy, and more:
# https://docs.microsoft.com/azure/devops/pipelines/apps/aspnet/build-aspnet-4
trigger: none
pool:
vmImage: 'windows-latest'
variables:
solution: '**/*.sln'
buildPlatform: 'Any CPU'
buildConfiguration: 'Release'
steps:
- task: NuGetToolInstaller@1
- task: NuGetCommand@2
inputs:
restoreSolution: '$(solution)'
- task: VSBuild@1
inputs:
solution: '$(solution)'
msbuildArgs: '/p:DeployOnBuild=true /p:WebPublishMethod=Package /p:PackageAsSingleFile=true /p:SkipInvalidConfigurations=true /p:PackageLocation="$(build.artifactStagingDirectory)"'
platform: '$(buildPlatform)'
configuration: '$(buildConfiguration)'
- task: VSTest@2
inputs:
platform: '$(buildPlatform)'
configuration: '$(buildConfiguration)'
codeCoverageEnabled: true
runSettingsFile: '$(Build.SourcesDirectory)\src\Alper.SinergyIntegration.Tests.UnitTests\.runsettings'
.runsettings 内容文件:
<?xml version="1.0" encoding="utf-8"?>
<RunSettings>
<DataCollectionRunSettings>
<DataCollectors>
<DataCollector friendlyName="Code Coverage">
<Configuration>
<CodeCoverage>
<ModulePaths>
<Include>
<ModulePath>alpe.sinerintegration.application.dll</ModulePath>
<ModulePath>alpe.sinerintegration.core.dll</ModulePath>
</Include>
</ModulePaths>
</CodeCoverage>
</Configuration>
</DataCollector>
</DataCollectors>
</DataCollectionRunSettings>
</RunSettings>
我的项目使用的是net7.0和xunit 2.4.2
您可以使用此构建质量检查扩展来使代码覆盖率过低的构建失败。
在 vstest 任务后添加以下任务。
- task: BuildQualityChecks@9
inputs:
checkCoverage: true
coverageFailOption: 'fixed'
coverageType: 'lines'
coverageThreshold: '60'
当代码覆盖率超过60%时,管道通过。
当代码覆盖率低于60%时,管道失败。