当前位置: 首页 > news >正文

要将ITP集成到Jenkins Pipeline中,实现开发发版时自动触发自动化测试

1. 准备工作
Jenkins插件安装

# 需要安装以下插件
- HTTP Request Plugin (用于调用ITP API)
- Pipeline Plugin (Jenkins Pipeline支持)
- Git Plugin (代码拉取)
- Workspace Cleanup Plugin (清理工作空间)


2. ITP API接口准备


根据提供的接口文档,需要使用以下关键API:
用户认证接口

# 1. 登录获取token
POST /users/authorizations/
{
    "username": "your_username",
    "password": "your_password"
}
# 返回token用于后续API调用

测试任务执行接口

# 2. 运行测试任务
POST  ​/testtask​/api​/testTask​/tasks​/run​/
{
    "env": 1,        # 测试环境ID
    "task": 1        # 测试任务ID
}

3. Jenkins Pipeline配置
Jenkinsfile示例

pipeline {
    agent any
    
    environment {
        ITP_USERNAME = credentials('ITP_USERNAME')
        ITP_PASSWORD = credentials('ITP_PASSWORD')
        ITP_BASE_URL = 'http://your-itp-server:8898'
        ITP_ENV_ID = '1'  // 测试环境ID
        ITP_TASK_ID = '1' // 测试任务ID
    }
    
    stages {
        stage('Build') {
            steps {
                // 构建步骤
                echo 'Building application...'
            }
        }
        
        stage('Deploy') {
            steps {
                // 部署步骤
                echo 'Deploying application...'
            }
        }
        
        stage('Trigger ITP Automation Test') {
            steps {
                script {
                    // 1. 获取ITP认证token
                    def authToken = getITPAuthToken()
                    
                    // 2. 触发自动化测试
                    def testResult = triggerITPAutomationTest(authToken)
                    
                    // 3. 等待测试完成并获取结果
                    def report = getTestReport(testResult.report_id, authToken)
                    
                    // 4. 根据测试结果决定流水线状态
                    if (report.state != "success") {
                        error "自动化测试失败,请检查测试报告"
                    }
                }
            }
        }
    }
    
    post {
        always {
            // 清理工作
            cleanWs()
        }
    }
}

def getITPAuthToken() {
    def response = httpRequest(
        url: "${ITP_BASE_URL}/api/users/login/",
        httpMode: 'POST',
        requestBody: """{
            "username": "${ITP_USERNAME}",
            "password": "${ITP_PASSWORD}"
        }""",
        contentType: 'APPLICATION_JSON'
    )
    
    def json = readJSON text: response.content
    return json.token
}

def triggerITPAutomationTest(token) {
    def response = httpRequest(
        url: "${ITP_BASE_URL}/api/testTask/tasks/run/",
        httpMode: 'POST',
        requestBody: """{
            "env": ${ITP_ENV_ID},
            "task": ${ITP_TASK_ID}
        }""",
        contentType: 'APPLICATION_JSON',
        customHeaders: [[name: 'Authorization', value: "Bearer ${token}"]]
    )
    
    return readJSON text: response.content
}

def getTestReport(reportId, token) {
    def response = httpRequest(
        url: "${ITP_BASE_URL}/api/testTask/report/${reportId}/",
        httpMode: 'GET',
        customHeaders: [[name: 'Authorization', value: "Bearer ${token}"]]
    )
    
    return readJSON text: response.content
}
 

4. 高级集成方案
轮询测试状态

def waitForTestCompletion(recordId, token, timeout=300) {
    def startTime = new Date().getTime()
    def status = "running"
    
    while (status == "running" || status == "pending") {
        def currentTime = new Date().getTime()
        if ((currentTime - startTime) > (timeout * 1000)) {
            error "测试执行超时"
        }
        
        def response = httpRequest(
            url: "${ITP_BASE_URL}/api/testTask/records/${recordId}/",
            httpMode: 'GET',
            customHeaders: [[name: 'Authorization', value: "Bearer ${token}"]]
        )
        
        def record = readJSON text: response.content
        status = record.status
        
        if (status == "running" || status == "pending") {
            sleep 10 // 等待10秒后再次检查
        }
    }
    
    return status
}
发送测试报告到通知渠道

def sendTestReportToDingTalk(report, webhookUrl) {
    def message = """
    ITP自动化测试报告:
    - 测试任务: ${report.task_name}
    - 执行状态: ${report.state}
    - 通过率: ${report.pass_rate}%
    - 总用例数: ${report.all}
    - 成功用例: ${report.success}
    - 失败用例: ${report.fail}
    """
    
    httpRequest(
        url: webhookUrl,
        httpMode: 'POST',
        requestBody: """{
            "msgtype": "text",
            "text": {
                "content": "${message}"
            }
        }""",
        contentType: 'APPLICATION_JSON'
    )
}


5. 配置建议


Jenkins Credentials配置

# 在Jenkins中配置以下Credentials:
1. ITP_USERNAME - ITP平台用户名
2. ITP_PASSWORD - ITP平台密码
3. DINGTALK_WEBHOOK - 钉钉机器人webhook(可选)


环境变量配置

# 在Jenkins Pipeline中配置环境变量:
ITP_BASE_URL = 'http://your-itp-server:8898'
ITP_ENV_ID = '1'    # 根据实际情况调整
ITP_TASK_ID = '1'   # 根据实际情况调整

6. 错误处理和重试机制

def withRetry(Closure operation, int maxRetries=3) {
    def lastException = null
    for (int i = 0; i <= maxRetries; i++) {
        try {
            return operation()
        } catch (Exception e) {
            lastException = e
            if (i < maxRetries) {
                echo "操作失败,${i+1}/${maxRetries}次重试: ${e.getMessage()}"
                sleep 5
            }
        }
    }
    throw lastException
}
通过以上配置,当开发发版时,Jenkins Pipeline会自动触发ITP平台的自动化测试任务,并根据测试结果决定流水线的成功或失败状态。

http://www.dtcms.com/a/573942.html

相关文章:

  • Linux 定时监测 Java 服务
  • 体外产品的研发网站如何建设paypal网站做外贸
  • 浙江城乡建设局和住建局seo课程培训入门
  • 3系统需求调研项目整合管理
  • Nestjs框架: Consul健康检查与gRPC客户端动态管理优化方案
  • 开机自动启动activity
  • 医学图像分割评价指标Dice与HD95的详解
  • 杀毒软件杀毒原理(草稿)
  • 网站开发需要会的东西网页设计大赛主题
  • 如何将iPhone上的笔记传输到电脑
  • 发布公司信息的网站网推接单
  • MES 离散制造核心流程详解(含关键动作、角色与异常处理)
  • 网站建设方案与报价wordpress文章怎么生成标签
  • 雄安投资建设集团网站东莞网站建设咨询
  • ruoyi前端(vue3)框架,切换菜单白屏问题
  • HTML5+CSS3小实例:不用JS实现幽灵网格动画
  • 人工智能 机器学习 深度学习
  • 用C++从零开始实现的小型深度学习训练框架
  • 算法题(Python)数组篇 | 3.有序数组的平方
  • 网站引流怎么做广告海报图片
  • 专业人士怎样建网站超星网站开发实战答案
  • 做推广的网站那个好网路营销网站策划书
  • muduo库学习(1):Reactor模型的设计思想
  • 分布式ID|从源码角度深度解析美团Leaf双Buffer优化方案
  • 【UE·优化篇】使用FramePro优化UI性能
  • 网站建设个人工作室seo在线培训机构
  • 免费做网站怎么做网站吗上海做网站比较好的
  • 【观察者模式】深入 Spring 事件驱动模型:从入门到微服务整合实战
  • 大连永锐网站哪家做的推荐常州网站建设
  • React的设计理念与核心特性