Cypress:如何重试整个测试套件?

Jef*_*ner 6 test-suite cypress

目标 能够对整个测试套件执行多次重试,直到所有测试都成功。

而不是配置测试套件中每个测试允许的重试,如配置测试重试:全局配置所定义

如何提供测试套件重试?

如果测试套件中的任何测试失败,则重新开始并再次重试整个测试套件,直到测试套件中的所有测试成功为止,或者当整个测试套件重试次数超过请求的重试次数时停止。

在以下代码中,配置适用于测试套件中每个测试的重试尝试。

此测试套件的预期目标是,测试'TEST counter'预计会失败,直到增量_counter等于_runModeRetries,然后才预计会成功。重复整个测试套件意味着重新运行测试'TEST counter'之前的每个测试,直到成功。

然而,发生的情况是,只有test'TEST counter'被重试了_runModeRetries几次,并且_counter不会增加,因为test'TEST increment'只被调用一次。

我为什么要这个?

我的测试套件包含一系列需要按顺序运行的测试,如果测试失败,则重试需要重新启动序列。例如,仅当通过完整的测试套件重再次调用测试_counter时才能递增。'TEST increment'

我如何进行测试套件重试?

  let _counter = 0;
  const _runModeRetries = 3;

  context(
    'CONTEXT Cypress Retries',
    {
      retries: {
        runMode: _runModeRetries,
        openMode: 0
      }
    },
    () => {
      it('TEST increment', () => {
        _counter++;
        expect(_counter).to.be.a('number').gt(0);
      });

      it('TEST true', () => {
        expect(true).to.be.a('boolean').to.be.true;
      });

      it('TEST false', () => {
        expect(false).to.be.a('boolean').to.be.false;
      });

      it('TEST counter', () => {
        if (_counter < _runModeRetries) {
          assert.fail();
        } else {
          assert.isTrue(true);
        }
      });
    }
  );
Run Code Online (Sandbox Code Playgroud)

Fod*_*ody 3

这真的很hacky,我只是发布它以防你可以改进它

  • 运行套件_runModeRetries 时间
  • 添加一个skip变量来控制测试是否运行
  • 进行所有测试function()以便this.skip()可以调用
  • after()在套件中添加一个skip以在第一次通过后设置为 true
  • 添加 onFail 处理程序skip以在发生失败时重置
let _counter = 0;
const _runModeRetries = 3;
let skip = false

Cypress.on('fail', (error, test) => {
  skip = false
  throw error  // behave as normal
})

Cypress._.times(_runModeRetries, () => {

  context('CONTEXT', {retries: {runMode: _runModeRetries}}, () => {
      it('TEST increment', function() {
        if (skip) this.skip()
        _counter++;
        expect(_counter).to.be.a('number').gt(0);
      });
      it('TEST true', function() {
        if (skip) this.skip()
        expect(true).to.be.a('boolean').to.be.true;
      });
      it('TEST false', function() {
        if (skip) this.skip()
        expect(false).to.be.a('boolean').to.be.false;
      });
      it('TEST counter', function() {
        if (skip) this.skip()
        assert.isTrue(_counter < _runModeRetries ? false :true);
      });
    }
  );
  after(() => skip = true)  // any fail, this does not run 
})
Run Code Online (Sandbox Code Playgroud)

通过调整 onFail (suite = test.parent) 内的套件并避免将更改添加到单个测试中,可能会有一些改进。


一种更简洁的方法是使用模块 API,它允许您运行测试套件、检查结果并在出现任何失败时再次运行。有点手动重试。