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

问题描述 投票:0回答:1

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

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

所定义

如何提供测试套件重试?

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

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

此测试套件的预期目标,test

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

但是,发生的情况是,只有 test

'TEST counter'
重试了
_runModeRetries
次,并且
_counter
没有递增,因为 test
'TEST increment'
只被调用一次。

我为什么想要这个?

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

test 
_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);
        }
      });
    }
  );
cypress retry-logic test-suite
1个回答
4
投票

这真的很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 })
通过调整 onFail (suite = test.parent) 内的套件并避免将更改添加到单个测试中,可能会有一些改进。


更简洁的方法是使用

Module API,它允许您运行测试套件,检查结果并在出现任何失败时再次运行。有点手动重试。

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.