
要筛选出具有特定属性的测试用例,首先需要从 TestRail 获取测试套件中的所有用例数据,然后对这些数据进行解析和过滤。
get_cases 是 TestRail API 中用于检索测试用例的核心端点。它允许你根据项目ID和测试套件ID获取该套件下的所有测试用例。
GET index.php?/api/v2/get_cases/{project_id}&suite_id={suite_id}执行 get_cases 请求后,TestRail 会返回一个 JSON 格式的响应,其中包含测试套件中所有测试用例的详细信息。每个用例对象都包含其 ID、标题以及所有自定义字段。
以下是一个响应示例,其中展示了 custom_can_be_automated 这一自定义字段:
{
"cases":[
{
"id":22478,
"title":"Test Case Steps (Text)",
"section_id":2347,
// ... 其他字段 ...
"suite_id":196,
"custom_automation_type":6,
"custom_can_be_automated":true, // 关注此字段
// ... 更多自定义字段 ...
},
{
"id":22494,
"title":"Automated Checkout",
"section_id":2347,
// ... 其他字段 ...
"suite_id":196,
"custom_automation_type":0,
"custom_can_be_automated":false, // 关注此字段
// ... 更多自定义字段 ...
}
]
}在这个 JSON 结构中,custom_can_be_automated 字段是一个布尔值,用于指示该测试用例是否可自动化。
获取到 JSON 响应后,下一步是根据 custom_can_be_automated 属性进行过滤,提取出所有标记为 true 的测试用例的 ID。
命令行示例(使用 curl 和 jq): 如果你在命令行环境工作,可以使用 curl 发送 API 请求,并结合 jq 工具来解析和过滤 JSON 响应。
TESTRAIL_URL="your_testrail_instance.testrail.io"
TESTRAIL_EMAIL="your_email@example.com"
TESTRAIL_PASS="your_api_key_or_password"
PROJECT_ID="1" # 替换为你的项目ID
SUITE_ID="196" # 替换为你的测试套件ID
curl -s -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 '[.cases[] | select(.custom_can_be_automated == true) | .id]'JavaScript 环境下的实现思路: 如果你在 JavaScript 环境中开发(例如 Node.js 或浏览器环境),可以使用 fetch 或 axios 等库来发送 HTTP 请求并处理响应:
async function getAutomatedCaseIds(projectId, suiteId) {
const testrailUrl = "https://your_testrail_instance.testrail.io";
const email = "your_email@example.com";
const apiKey = "your_api_key_or_password"; // 推荐使用API Key
const auth = btoa(`${email}:${apiKey}`); // Base64 编码认证信息
try {
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(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
const automatedCaseIds = data.cases
.filter(testCase => testCase.custom_can_be_automated === true)
.map(testCase => testCase.id);
return automatedCaseIds;
} catch (error) {
console.error("Error fetching automated test cases:", error);
return [];
}
}
// 示例调用
// getAutomatedCaseIds(1, 196).then(ids => console.log("Automated Case IDs:", ids));获取到需要添加到测试运行的用例 ID 列表后,下一步就是使用 update_run API 端点来更新现有的测试运行。
update_run 用于修改一个已存在的测试运行的属性,包括其包含的测试用例。
POST index.php?/api/v2/update_run/{run_id}要将特定用例添加到测试运行,你需要在 POST 请求体中提供一个 JSON 对象,其中包含 case_ids 数组。
关键字段:
示例请求体:
{
"include_all": false,
"case_ids": [22478, 30001, 30005] // 替换为实际筛选出的用例ID
}命令行示例(使用 curl):
TESTRAIL_URL="your_testrail_instance.testrail.io"
TESTRAIL_EMAIL="your_email@example.com"
TESTRAIL_PASS="your_api_key_or_password"
RUN_ID="123" # 替换为你的测试运行ID
CASE_IDS="[22478, 30001, 30005]" # 替换为上一步获取到的用例ID数组
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\": $CASE_IDS}"JavaScript 环境下的实现思路:
async function addCasesToTestRun(runId, caseIds) {
const testrailUrl = "https://your_testrail_instance.testrail.io";
const email = "your_email@example.com";
const apiKey = "your_api_key_or_password";
const auth = btoa(`${email}:${apiKey}`);
try {
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(`HTTP error! status: ${response.status}`);
}
console.log(`Successfully added cases to TestRun ${runId}.`);
return await response.json(); // 返回更新后的 TestRun 信息
} catch (error) {
console.error(`Error adding cases to TestRun ${runId}:`, error);
return null;
}
}
// 示例调用
// const automatedIds = await getAutomatedCaseIds(1, 196); // 假设已获取
// if (automatedIds.length > 0) {
// addCasesToTestRun(123, automatedIds);
// } else {
// console.log("No automated cases found to add.");
// }当 update_run 请求成功执行后,TestRail API 会返回一个 HTTP 状态码 200 OK,表示操作成功。你可以根据这个状态码来判断用例是否已成功添加到测试运行中。
在实际集成 TestRail API 时,请考虑以下几点以确保操作的健壮性和可靠性:
通过本文的指导,你已经掌握了如何利用 TestRail API 动态筛选特定测试用例(例如,标记为“可自动化”的用例)并将其添加到现有的测试运行中。这一过程涉及到 get_cases 端点获取数据、解析 JSON 响应、根据自定义字段进行过滤,以及使用 update_run 端点更新测试运行。掌握这些技能将极大地提升你的自动化测试流程与 TestRail 管理的集成效率和灵活性。
以上就是TestRail API 实战:动态筛选测试用例并集成至测试运行的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号