You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Azure DevOps中实现失败测试占比超70%时终止流水线运行?

在Azure DevOps流水线中按测试失败占比终止运行的实现方法

1. 配置Maven生成可解析的测试报告

首先要确保Maven的Surefire插件生成XML格式的测试报告,方便后续脚本统计结果。在项目的pom.xml中添加或更新插件配置:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>3.2.5</version>
            <configuration>
                <reportsDirectory>${project.build.directory}/surefire-reports</reportsDirectory>
                <reportFormat>xml</reportFormat>
                <failIfNoTests>false</failIfNoTests>
            </configuration>
        </plugin>
    </plugins>
</build>

2. 编写测试失败占比统计与终止脚本

根据流水线代理的操作系统,选择对应脚本:

Bash脚本(Linux/macOS代理)

创建check_test_failure_rate.sh文件:

#!/bin/bash

# 统计总测试用例数
total_tests=$(grep -r "<testcase" target/surefire-reports/*.xml | wc -l)
# 统计失败测试用例数(含failure和error)
failed_tests=$(grep -r -E "<failure|<error" target/surefire-reports/*.xml | wc -l)

# 处理无测试用例的情况
if [ $total_tests -eq 0 ]; then
    echo "No tests executed."
    exit 0
fi

# 计算失败占比(保留两位小数)
failure_rate=$(echo "scale=2; $failed_tests / $total_tests * 100" | bc)
echo "Total tests: $total_tests, Failed tests: $failed_tests, Failure rate: ${failure_rate}%"

# 判断是否超过70%阈值
if (( $(echo "$failure_rate > 70" | bc -l) )); then
    echo "##vso[task.complete result=Failed;]Test failure rate (${failure_rate}%) exceeds 70%, terminating pipeline."
    exit 1
fi

PowerShell脚本(Windows代理)

创建Check-TestFailureRate.ps1文件:

# 获取所有测试报告XML文件
$testReports = Get-ChildItem -Path "target\surefire-reports" -Filter "*.xml"

$totalTests = 0
$failedTests = 0

foreach ($report in $testReports) {
    $xml = [xml](Get-Content $report.FullName)
    $totalTests += $xml.testsuite.testcase.Count
    $failedTests += ($xml.testsuite.failure.Count + $xml.testsuite.error.Count)
}

# 处理无测试用例的情况
if ($totalTests -eq 0) {
    Write-Host "No tests executed."
    exit 0
}

# 计算失败占比
$failureRate = [math]::Round(($failedTests / $totalTests) * 100, 2)
Write-Host "Total tests: $totalTests, Failed tests: $failedTests, Failure rate: ${failureRate}%"

# 判断是否超过70%阈值
if ($failureRate -gt 70) {
    Write-Host "##vso[task.complete result=Failed;]Test failure rate (${failureRate}%) exceeds 70%, terminating pipeline."
    exit 1
}

3. 配置Azure DevOps流水线YAML

在流水线YAML中添加以下步骤:

steps:
  # 执行Maven安装与测试,允许测试失败后继续执行后续脚本
  - task: Maven@3
    displayName: 'Maven install and run tests'
    inputs:
      mavenPomFile: 'pom.xml'
      goals: 'install'
      options: '-DskipTests=false'
      publishJUnitResults: true
      testResultsFiles: '**/surefire-reports/TEST-*.xml'
    continueOnError: true # 关键设置:避免测试失败直接终止流水线

  # 执行失败占比检查脚本(根据代理系统选择对应任务)
  - task: Bash@3
    displayName: 'Check test failure rate'
    inputs:
      filePath: 'check_test_failure_rate.sh'
    # Windows代理替换为以下任务:
    # - task: PowerShell@2
    #   displayName: 'Check test failure rate'
    #   inputs:
    #     filePath: 'Check-TestFailureRate.ps1'

关键说明

  • continueOnError: true必须设置,否则Maven任务在测试失败时会直接终止流水线,无法执行后续的占比检查逻辑。
  • 脚本中使用的##vso[task.complete result=Failed;]是Azure DevOps内置命令,会直接标记任务失败并终止流水线。
  • 确保脚本中的测试报告路径与Maven配置的路径一致,默认路径为target/surefire-reports。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.17 10:41:17