【问题标题】:In TestCafe is there a way to know if the test passed or failed in after hook?在 TestCafe 中,有没有办法知道在钩子后测试是通过还是失败?
【发布时间】:2019-05-08 11:08:41
【问题描述】:

当我的 testcafe 测试正在运行时,我试图通过休息 API (Zephyr) 将测试标记为通过/失败。我想知道是否可以在 afterafterEach 挂钩中知道测试是否通过/失败,以便我可以根据结果运行一些脚本。

类似:

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

【问题讨论】:

    标签: automated-tests hook e2e-testing testcafe


    【解决方案1】:

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

    t.testRun.errs
    

    如果数组已填充,则测试失败。

    【讨论】:

      【解决方案2】:

      我看到了解决您的任务的两种方法。首先,不要订阅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钩子中得到一个错误。

      【讨论】:

      • 这正是我最终要做的(自定义记者)。谢谢你:)
      猜你喜欢
      • 1970-01-01
      • 2021-04-17
      • 1970-01-01
      • 2020-02-01
      • 1970-01-01
      • 2020-05-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-11
      相关资源
      最近更新 更多