首页 > 解决方案 > 如何在将测试声明为“失败”之前多次重试 chaijs 预期断言

问题描述

我没有使用 Mocha 作为我的测试运行程序,我使用的是 cucumberJs。有没有办法在声明步骤失败之前仅重新运行预期条件给定次数?

假设一个简单(但不现实)的步骤是:

Given(/^I want to check the username to be John$/, (name: string)=>{
   expect(name).to.be.equals('John'); 
});

现在,如果名字是 Sam,expect 条件将失败。但是,我想在声明它失败之前运行期望条件至少 5 次。是否有任何 chaijs 配置/插件可以实现相同的功能?

任何帮助将不胜感激。

标签: javascriptchaiassertioncucumberjs

解决方案


据我所知,Chai 没有任何此类功能。看起来MochaJS确实有一些东西可以实现这一点。如果您仍想使用 chai,这是可行的。

Given(/^I want to check the username to be John$/, (name: string) => {
  let retry = 3;
  for (let index = 0; index < retry; index++) {
    try {
      expect(name).to.be.equals('John');
      index = retry + 1;
    } catch (error) {
      console.log(index);
      if (index == retry - 1) throw new Error('Failed')
    }
  }
});

推荐阅读