从 CI 发送结果¶
在项目中打开 CI 集成。选择测试运行,然后下载适用于 Windows 或 Linux (x64) 的 ztm。将文件放入受信任的运行器的可执行文件搜索路径。在 Linux 上,先使用 chmod +x ztm 授予执行权限。
- 将服务器地址设为
ZTM_BASE_URL,将项目 GUID 设为ZTM_PROJECT_UNIQUE_ID,并将ZTM_API_KEY存放在流水线的机密存储中。如果部署使用应用程序路径,请将其加入基础 URL。 - 执行
ztm runs list,记录尚未结束的测试运行 ID。 -
先预览。预览会显示外部测试用例 ID、
Targets中符合条件的计划变体,以及ResultDefinitions中的项目结果定义。ztm results upload --run 4711 --file "results/**/*.xml" --dry-run --json -
检查目标,将映射保存到
ztm-mappings.json。每个条目都需要externalCaseId和plannedVariantId。如果项目提供多个成功或失败定义,请设置successResultDefinitionId或failureResultDefinitionId。单一的resultDefinitionId会同时应用于成功和失败,仅在有意固定结果时使用。 -
导入报告。导入摘要会区分已导入、此前已导入、已跳过和未映射的结果。导入后请检查测试运行。
ztm results upload --run 4711 --file "results/**/*.xml" --source CI --build-id 1082 --fail-on-unmapped
可重复使用 --file 来提供多个匹配模式。--format auto 会识别 JUnit、NUnit 3 和 TRX。--mappings 可选择其他文件。--url 和 --project 会覆盖环境变量。--timeout 对每次 REST 调用分别生效。使用 ztm help 查看所有选项,使用 ztm version 验证可执行文件。
流水线示例¶
这些示例假定报告已经生成,且运行器上有 ztm 可执行文件。配置步骤 1 的变量,并将测试运行 ID 设为 ZTM_TEST_RUN_ID。只向受信任的作业提供密钥。
GitHub Actions¶
# Merge this job into your trusted pipeline after reports have been generated.
name: Send test results to ZTM
on:
workflow_dispatch:
jobs:
upload:
runs-on: [self-hosted, linux, x64, ztm]
steps:
- uses: actions/checkout@v4
# Insert your existing report producer or artifact download step here.
- name: Import automated results
env:
ZTM_API_KEY: ${{ secrets.ZTM_API_KEY }}
ZTM_BASE_URL: ${{ vars.ZTM_BASE_URL }}
ZTM_PROJECT_UNIQUE_ID: ${{ vars.ZTM_PROJECT_UNIQUE_ID }}
ZTM_TEST_RUN_ID: ${{ vars.ZTM_TEST_RUN_ID }}
run: ztm results upload --run "$ZTM_TEST_RUN_ID" --file 'results/**/*.xml' --source GitHub-Actions --build-id "$GITHUB_RUN_ID" --fail-on-unmapped
GitLab CI¶
# Merge this job after the job that supplies results/ as an artifact.
ztm-upload:
stage: deploy
tags: [ztm-linux-x64]
rules:
- if: '$CI_COMMIT_REF_PROTECTED == "true"'
script:
- ztm results upload --run "$ZTM_TEST_RUN_ID" --file 'results/**/*.xml' --source GitLab --build-id "$CI_PIPELINE_ID" --fail-on-unmapped
Azure Pipelines¶
# Merge this step after your existing report-producing steps.
steps:
- bash: ztm results upload --run "$ZTM_TEST_RUN_ID" --file 'results/**/*.xml' --source Azure-Pipelines --build-id "$BUILD_BUILDID" --fail-on-unmapped
displayName: Import automated results
env:
ZTM_API_KEY: $(ZTM_API_KEY)
ZTM_BASE_URL: $(ZTM_BASE_URL)
ZTM_PROJECT_UNIQUE_ID: $(ZTM_PROJECT_UNIQUE_ID)
ZTM_TEST_RUN_ID: $(ZTM_TEST_RUN_ID)
Jenkins¶
pipeline {
agent { label 'ztm-linux-x64' }
stages {
// Add your existing test stage before this upload stage.
stage('Import automated results') {
steps {
withCredentials([string(credentialsId: 'ztm-project-api-key', variable: 'ZTM_API_KEY')]) {
sh 'ztm results upload --run "$ZTM_TEST_RUN_ID" --file "results/**/*.xml" --source Jenkins --build-id "$BUILD_TAG" --fail-on-unmapped'
}
}
}
}
}
权限、限制和重试¶
项目 API 密钥必须具备读取和预览权限,以及 project.automation-import.commit 权限。列出测试运行还需要 project.knowledge.query 权限。关联的项目用户必须可以编辑测试运行并查看目标变体。启用 MCP 和项目写入权限。预览不会修改测试结果。仅在通过回环接口进行测试时使用未加密的 HTTP。也可以在标准输入的第一行提供密钥。
工具会将大型报告拆分为多个批次,每批最多 1,000 个用例。每项结果及其必需元数据必须能够放入一个 REST 请求。已有结果受保护。如果连接中断,前面的批次可能已经保存。使用相同的报告、映射、测试运行和构建 ID 重试前,请先检查导入摘要和导入历史。
| 代码 | 含义 |
|---|---|
| 0 | 导入或预览成功 |
| 1 | 身份验证、传输或导入失败 |
| 2 | --fail-on-unmapped 发现未映射的结果 |
| 3 | 命令行或输入文件无效 |