Workspace Cleanup Plugin (清理工作空间)
ITP API 接口准备
根据提供的接口文档,需要使用以下关键 API:
用户认证接口
POST /users/authorizations/
{
"username": "your_username",
"password": "your_password"
}
测试任务执行接口
POST /testtask/api/testTask/tasks/run/
{
"env": 1, # 测试环境 ID
"task": 1 # 测试任务 ID
}
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
}
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'
)
}
Jenkins Credentials 配置
环境变量配置
ITP_BASE_URL = 'http://your-itp-server:8898'
ITP_ENV_ID = '1' # 根据实际情况调整
ITP_TASK_ID = '1' # 根据实际情况调整
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 平台的自动化测试任务,并根据测试结果决定流水线的成功或失败状态。
ITP 体验网址: http://1.95.215.79:18899/ tester(88888888) ITP 项目地址: https://gitee.com/hp631012651/itp