在TestCafe中,有一种方法可以知道挂钩后测试是通过还是失败?

时间:2018-12-06 17:26:27

标签: automated-tests hook e2e-testing testcafe

我正在尝试在我的testcafe测试运行时通过rest API(Zephyr)将测试标记为通过/失败。我想知道是否可以在之后 afterEach 挂钩中了解测试是否通过/失败,以便我可以根据结果运行一些脚本。

类似的东西:

test(...)
.after(async t => {
  if(testFailed === true) { callApi('my test failed'); }
})

2 个答案:

答案 0 :(得分:4)

我看到两种解决任务的方法。首先,不要订阅after钩子,而是创建自己的reporter或修改现有的reporter。请参阅以下文章:https://devexpress.github.io/testcafe/documentation/extending-testcafe/reporter-plugin/#implementing-the-reporter 其中最有趣的方法是reportTestDone,因为它接受errs作为参数,因此您可以在此处添加自定义逻辑。

第二种方法是使用sharing variables between hooks and test code

您可以通过以下方式编写测试:

test(`test`, async t => {
    await t.click('#non-existing-element');

    t.ctx.passed = true;
}).after(async t => {
    if (t.ctx.passed)
        throw new Error('not passed');
});

在这里,我正在测试代码和挂钩之间使用共享的passed变量。如果测试失败,则该变量将不会设置为true,并且在after挂钩中会出现错误。

答案 1 :(得分:0)

这可以从测试控制器中确定,该控制器中嵌套了更多信息,这些信息仅在运行时可见。包含测试中引发的所有错误的数组如下所示:

<div class="tooltipped top">
  <input type="number" min="0" max="4"/>
  <div class="on-invalid">
    <div>
      <div>
        <p>I am a tooltip!</p>
      </div>
    </div>
  </div>
</div>

<div class="tooltipped right">
  <input type="number" min="0" max="4"/>
  <div class="always">
    <div>
      <div>
        <p>I am a tooltip!</p>
      </div>
    </div>
  </div>
</div>

<div class="tooltipped bottom">
  <input type="number" min="0" max="4"/>
  <div class="on-invalid">
    <div>
      <div>
        <p>I am a tooltip!</p>
      </div>
    </div>
  </div>
</div>

<div class="tooltipped left">
  <input type="number" min="0" max="4"/>
  <div class="always">
    <div>
      <div>
        <p>I am a tooltip!</p>
      </div>
    </div>
  </div>
</div>

如果填充了阵列,则测试失败。

相关问题