TestRail
TestRail 集成
Playwright 测试与 TestRail 测试管理之间的双向同步。
前置条件
必须设置以下环境变量:
TESTRAIL_URL— 例如https://your-instance.testrail.io
TESTRAIL_USER— 您的电子邮件
TESTRAIL_API_KEY— 来自 TestRail 的 API 密钥
如果未设置,请告知用户如何配置并停止操作。
功能能力
1. 导入测试用例 $\rightarrow$ 生成 Playwright 测试
/pw:testrail import --project <id> --suite <id>步骤:
1. 调用 testrail_get_cases MCP 工具获取测试用例
2. 针对每个测试用例:
- 读取标题、前置条件、步骤、预期结果
- 使用合适的模板映射为 Playwright 测试
- 将 TestRail 用例 ID 作为测试注解包含在内:test.info().annotations.push({ type: 'testrail', description: 'C12345' })
3. 按 section 分组生成测试文件
4. 报告:导入 X 个用例,生成 Y 个测试
2. 推送测试结果 $\rightarrow$ TestRail
/pw:testrail push --run <id>步骤:
1. 使用 JSON 报告运行 Playwright 测试:
npx playwright test --reporter=json > test-results.json2. 解析结果:将每个测试映射到其 TestRail 用例 ID(来自注解)
3. 为每个测试调用
testrail_add_result MCP 工具:- 通过 $\rightarrow$ status_id: 1
- 失败 $\rightarrow$ status_id: 5,包含错误消息
- 跳过 $\rightarrow$ status_id: 2
4. 报告:推送 X 个结果,Y 个通过,Z 个失败
3. 创建测试运行 (Test Run)
/pw:testrail run --project <id> --name "Sprint 42 Regression"步骤:
1. 调用 testrail_add_run MCP 工具
2. 包含在 Playwright 测试注解中找到的所有测试用例 ID
3. 返回 run ID 以用于推送结果
4. 同步状态
/pw:testrail status --project <id>步骤:
1. 从 TestRail 获取测试用例
2. 扫描本地 Playwright 测试中的 TestRail 注解
3. 报告覆盖率:
TestRail cases: 150
Playwright tests with TestRail IDs: 120
Unlinked TestRail cases: 30
Playwright tests without TestRail IDs: 155. 更新 TestRail 中的测试用例
/pw:testrail update --case <id>步骤:
1. 读取该用例 ID 对应的 Playwright 测试
2. 从测试代码中提取步骤和预期结果
3. 调用 testrail_update_case MCP 工具更新步骤
使用的 MCP 工具
| 工具 | 使用场景 |
|---|---|
| testrail_get_projects | 列出可用项目 |
| testrail_get_suites | 列出项目中的 suite |
| testrail_get_cases | 读取测试用例 |
| testrail_add_case | 创建新测试用例 |
| testrail_update_case | 更新现有用例 |
| testrail_add_run | 创建测试运行 |
| testrail_add_result | 推送单个结果 |
| testrail_get_results | 读取历史结果 |
测试注解格式
所有链接到 TestRail 的 Playwright 测试均包含:
test('should login successfully', async ({ page }) => {
test.info().annotations.push({
type: 'testrail',
description: 'C12345',
});
// ... 测试代码
});此注解是 Playwright 与 TestRail 之间的桥梁。
输出
- 包含计数的操作摘要
- 任何错误或未匹配的用例
- TestRail run/结果的链接