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

如何在Jenkins中Postman测试失败时调用Azure DevOps Rest API自动创建Bug

实现Jenkins+Postman+Azure DevOps自动创建Bug的方案

1. 可行性确认

完全可以实现。核心逻辑是:Jenkins流水线通过Newman运行Postman测试集合,捕获测试失败结果后,调用Azure DevOps的Work Item API自动创建Bug。

2. 具体实现步骤

步骤1:Postman集合与Newman配置

  • 将Postman测试集合导出为JSON文件,上传到Jenkins项目代码仓库或存储为Jenkins文件资源。
  • 在Jenkins节点安装Newman(Postman命令行工具),可通过npm安装:npm install -g newman。
  • 运行测试时指定JSON格式报告输出,方便后续解析失败用例:
    newman run your-collection.json -r json --reporter-json-export test-results.json
    

步骤2:Jenkins流水线捕获失败用例

在Jenkinsfile中,通过Groovy脚本解析Newman生成的测试结果JSON,提取失败用例的名称、错误信息等关键内容:

def testResults = readJSON file: 'test-results.json'
// 统计失败用例数量
def failedCount = testResults.run.failures
// 提取所有失败的测试用例详情
def failedTests = testResults.run.executions.findAll { 
  it.assertions.any { assertion -> !assertion.passed } 
}

步骤3:调用Azure DevOps API创建Bug

当检测到失败用例时,构造API请求体并调用Azure DevOps的Work Item创建接口:

  1. 在Azure DevOps中创建个人访问令牌(PAT),需授予「Work Items (Write)」权限,将PAT存储为Jenkins的字符串凭据(避免硬编码)。
  2. 用curl或Jenkins HTTP插件发送请求,示例curl命令(替换占位符):
    curl -X POST \
      https://dev.azure.com/{组织名}/{项目名}/_apis/wit/workitems/%24Bug?api-version=7.1-preview.3 \
      -H "Content-Type: application/json-patch+json" \
      -H "Authorization: Basic $(echo -n :{PAT令牌} | base64)" \
      -d '[
        {
          "op": "add",
          "path": "/fields/System.Title",
          "value": "Postman测试失败:{失败用例名称}"
        },
        {
          "op": "add",
          "path": "/fields/System.Description",
          "value": "失败原因:{错误详情}\nJenkins构建编号:{BUILD_NUMBER}\n构建链接:{BUILD_URL}"
        },
        {
          "op": "add",
          "path": "/fields/Microsoft.VSTS.Common.Priority",
          "value": 2
        }
      ]'
    

步骤4:流水线整合完整逻辑

将上述步骤整合到Jenkinsfile的阶段中,通过post条件触发Bug创建:

pipeline {
  agent any
  stages {
    stage('Run Postman Tests') {
      steps {
        sh 'newman run your-collection.json -r json --reporter-json-export test-results.json'
      }
      post {
        always {
          script {
            def testResults = readJSON file: 'test-results.json'
            def failedTests = testResults.run.executions.findAll { 
              it.assertions.any { !it.passed } 
            }
            if (failedTests.size() > 0) {
              withCredentials([string(credentialsId: 'azure-devops-pat', variable: 'AZURE_PAT')]) {
                failedTests.each { test ->
                  def testName = test.item.name
                  def errorMsg = test.assertions.find { !it.passed }.error.message
                  sh """
                  curl -X POST \
                    https://dev.azure.com/your-org/your-project/_apis/wit/workitems/%24Bug?api-version=7.1-preview.3 \
                    -H "Content-Type: application/json-patch+json" \
                    -H "Authorization: Basic $(echo -n :${AZURE_PAT} | base64)" \
                    -d '[
                      {
                        "op": "add",
                        "path": "/fields/System.Title",
                        "value": "Postman测试失败:${testName}"
                      },
                      {
                        "op": "add",
                        "path": "/fields/System.Description",
                        "value": "失败原因:${errorMsg}\\nJenkins构建编号:${BUILD_NUMBER}\\n构建链接:${BUILD_URL}"
                      },
                      {
                        "op": "add",
                        "path": "/fields/Microsoft.VSTS.Common.Priority",
                        "value": 2
                      }
                    ]'
                  """
                }
              }
            }
          }
        }
      }
    }
  }
}

3. 关键注意事项

  • 确保Jenkins节点能访问Azure DevOps的API端点,若有网络限制需配置代理。
  • 测试API请求格式时,可先在本地用curl验证,确认能成功创建Bug后再整合到流水线。
  • 可根据需求扩展Bug字段,比如添加指派人员、标签、关联测试用例等,参考Azure DevOps Work Item字段定义调整请求体。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.21 15:09:22