You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Cypress更新Zephyr Scale测试周期执行结果?

实现Cypress测试结果同步至Zephyr Scale(仅更新测试用例状态)

我需要将Cypress测试执行结果同步到Zephyr Scale,无需更新测试步骤,仅需同步测试用例状态以满足可追溯性及报表需求。以下是我的实现方案及使用前的关键注意事项:

使用前注意事项

  • 代码需放置在Cypress的afterEach()钩子中,确保每个测试用例执行完成后自动同步结果至Zephyr Scale
  • Cypress测试用例的标题必须包含Zephyr测试用例ID(格式示例:PROJECTCODE-T123),否则脚本无法匹配到对应用例,导致同步失败

实现代码

afterEach(function() {

    console.log(this.currentTest.state)

    const pattern = /PROJECTCODE-T\d+/;
    const match = this.currentTest.title.match(pattern)

    console.log(match[0])

    const token = Cypress.env('bearerToken')
    const url = Cypress.env('zephyrBaseURL') + '/testexecutions'
    const testCycle = Cypress.env('testCycle')

    if(this.currentTest.state === 'passed'){
        cy.then(() => {
        cy.request({
            headers: {
                    Authorization: `Bearer ${token}`
                },
            url: `${url}`,
            method: 'POST',
            body: {
                    projectKey: "PROJECTCODE",
                    testCaseKey: `${match[0]}`,
                    testCycleKey: `${testCycle}`,
                    statusName: "Pass",
                    environmentName: "QA"
                }
            })
        })
    }else if (this.currentTest.state === 'failed')
    {
        cy.then(() => {
        cy.request({
            headers: {
                    Authorization: `Bearer ${token}`
                },
            url: `${url}`,
            method: 'POST',
            body: {
                    projectKey: "ALT",
                    testCaseKey: `${match[0]}`,
                    testCycleKey: `${testCycle}`,
                    statusName: "Fail",
                    environmentName: "QA"
                }
            })
        })
    }else if (this.currentTest.state === 'skipped')
    {
        cy.then(() => {
            cy.request({
                headers: {
                        Authorization: `Bearer ${token}`
                    },
                url: `${url}`,
                method: 'POST',
                body: {
                        projectKey: "ALT",
                        testCaseKey: `${match[0]}`,
                        testCycleKey: `${testCycle}`,
                        statusName: "Not Executed",
                        environmentName: "QA"
                    }
                })
            })
    }
})

优化建议征集

欢迎各位提出代码优化、逻辑完善等方面的建议,感谢!

内容的提问来源于stack exchange,提问作者Azri

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 02:45:19