
在TestRail中,我们经常需要根据特定的条件来管理和执行测试用例。一个常见的需求是,只将那些被标记为“可自动化”的测试用例添加到新的测试运行中。本教程将通过TestRail的RESTful API,演示如何实现这一目标。整个过程分为两个主要步骤:首先,从测试套件中筛选出符合条件的测试用例;其次,将这些筛选出的用例添加到指定的测试运行中。
要从TestRail套件中筛选出具有特定自定义字段(例如 custom_can_be_automated = true)的测试用例,我们需要使用 get_cases API 端点。
get_cases 端点用于获取指定项目和测试套件中的所有测试用例。
GET index.php?/api/v2/get_cases/{project_id}&suite_id={suite_id}执行上述查询后,TestRail会返回一个JSON格式的响应,其中包含测试套件中的所有用例及其详细信息。自定义字段通常以 custom_ 前缀命名。
"cases":[
{
"id":22478,
"title":"Test Case Steps (Text)",
"section_id":2347,
"template_id":1,
// ... 其他字段 ...
"suite_id":196,
"custom_automation_type":6,
"custom_can_be_automated":true, // 关注此自定义字段
"custom_preconds":"Test Case Step *TEXT* Precondition",
// ... 其他自定义字段 ...
},
{
"id":22494,
"title":"Automated Checkout",
"section_id":2347,
"template_id":9,
// ... 其他字段 ...
"suite_id":196,
"custom_automation_type":0,
"custom_can_be_automated":false, // 关注此自定义字段
"custom_preconds":null,
// ... 其他自定义字段 ...
}
]在上述响应中,我们可以看到 custom_can_be_automated 字段,它的值可以是 true 或 false。我们将根据这个字段来筛选用例。
获取到JSON响应后,你需要解析它并根据 custom_can_be_automated 字段的值来提取符合条件的用例ID。
使用 cURL 和 jq 进行筛选(示例)
如果你在命令行环境中使用 curl,可以结合 jq 工具来方便地解析和筛选JSON数据。
curl -H "Content-Type: application/json" \
-u "$TESTRAIL_EMAIL:$TESTRAIL_PASS" \
"$TESTRAIL_URL/index.php?/api/v2/get_cases/{project_id}&suite_id={suite_id}" | \
jq '[.[] | select(.custom_can_be_automated == true) | .id]'这个 curl 命令会:
在 JavaScript 环境中筛选
如果你在JavaScript中实现,你可以使用 fetch 或 axios 等库来发送HTTP请求,然后使用JavaScript的原生方法来解析和筛选JSON响应:
async function getAutomatedCaseIds(projectId, suiteId, testrailUrl, email, password) {
const auth = btoa(`${email}:${password}`); // Base64编码认证信息
const response = await fetch(`${testrailUrl}/index.php?/api/v2/get_cases/${projectId}&suite_id=${suiteId}`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'Authorization': `Basic ${auth}`
}
});
if (!response.ok) {
throw new Error(`Error fetching cases: ${response.status} ${response.statusText}`);
}
const cases = await response.json();
const automatedCaseIds = cases
.filter(testCase => testCase.custom_can_be_automated === true)
.map(testCase => testCase.id);
return automatedCaseIds;
}
// 示例调用
// getAutomatedCaseIds(1, 196, 'https://your-testrail-instance.testrail.io', 'your_email@example.com', 'your_password')
// .then(ids => console.log('Automated Case IDs:', ids))
// .catch(error => console.error(error));获取到符合条件的用例ID列表后,下一步就是将它们添加到指定的测试运行中。这可以通过 update_run API 端点实现。
update_run 端点用于修改现有测试运行的属性,包括其包含的测试用例。
POST index.php?/api/v2/update_run/{run_id}要添加特定的测试用例,请求体应包含 include_all 字段设置为 false,并提供一个 case_ids 数组,其中包含你想要添加的用例ID。
{
"include_all": false,
"case_ids": [1, 2, 3, 5, 8]
}重要提示: 根据TestRail API文档,当您希望指定要包含的特定测试用例时,include_all 必须设置为 false。如果 include_all 为 true,则 case_ids 字段将被忽略或用于排除用例(取决于TestRail版本和配置),这与我们仅添加特定用例的目标不符。
使用 cURL 进行 POST 请求
curl -X POST \
"https://$TESTRAIL_URL/index.php?/api/v2/update_run/{run_id}" \
-H "Content-Type: application/json" \
-u "$TESTRAIL_EMAIL:$TESTRAIL_PASS" \
-d '{"include_all": false, "case_ids": [22478, 22499, 22501]}'在 JavaScript 环境中进行 POST 请求
async function addCasesToTestRun(runId, caseIds, testrailUrl, email, password) {
const auth = btoa(`${email}:${password}`); // Base64编码认证信息
const response = await fetch(`${testrailUrl}/index.php?/api/v2/update_run/${runId}`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Basic ${auth}`
},
body: JSON.stringify({
include_all: false,
case_ids: caseIds
})
});
if (!response.ok) {
throw new Error(`Error updating test run: ${response.status} ${response.statusText}`);
}
return response.json(); // 通常会返回更新后的run对象
}
// 示例调用
// const automatedIds = [22478, 22499, 22501]; // 假设这是从getAutomatedCaseIds获取到的ID
// addCasesToTestRun(123, automatedIds, 'https://your-testrail-instance.testrail.io', 'your_email@example.com', 'your_password')
// .then(run => console.log('Test Run updated successfully:', run))
// .catch(error => console.error(error));如果操作成功,TestRail API将返回一个状态码为 200 的响应,表示测试运行已成功更新。
通过遵循上述步骤,你可以有效地利用TestRail API来自动化测试用例的管理流程,实现根据自定义字段筛选并添加到测试运行的需求,从而提高测试管理的效率和灵活性。
以上就是TestRail中筛选自动化测试用例并添加到测试运行的教程的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号