在 Cypress 和 Before 钩子中重试

Retries in Cypress and Before hooks

大家早上好。我的测试设计有点不寻常。一个典型的例子可能是

describe('1', () => {
   describe('2', () => {

       before()

       describe('3', () => {
           it('1')
           // ...
           it('n')
        });
    });
});

如果其中一个测试失败(1..n),我想重新运行所有这些测试,运行“之前”的代码首先 - 即来自“描述 2”。如果我使用 before 钩子,则 re-运行s 不会再次触发它。如果我更改为 beforeEach,那么它会在每个“它”块之前被调用,这是我不想要的。

实际上,每个 it 块都是测试检查,描述 3 是测试步骤,描述 2 是测试规范,描述 1 是测试“组”

任何人都可以建议一种方法,我可以在一个测试检查失败时重新运行 测试规范(描述 2),包括重新运行该规范的之前代码?

(我知道这可能是反模式等,但是....)

您可以外部化 before() 回调函数,并使用 test:after:run 事件在重试时触发它。

我还没有对此进行广泛的测试,但要点是

const beforeCallback = () => {...}

before(beforeCallback)

Cypress.on('test:after:run', (result) => {
  if (result.currentRetry < result.retries && result.state === 'failed') {
    beforeCallback()
  }
})

it('fails', {retries:3}, () => expect(false).to.eq(true)) // failing test to check it out