在 TestCafe 中,有没有办法知道钩子后测试是通过还是失败?

Mar*_*son 3 hook automated-tests e2e-testing testcafe

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

就像是:

test(...)
.after(async t => {
  if(testFailed === true) { callApi('my test failed'); }
})
Run Code Online (Sandbox Code Playgroud)

Ale*_*aev 5

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

第二种方法是在钩子和测试代码之间使用共享变量

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

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');
});
Run Code Online (Sandbox Code Playgroud)

这里我使用了passed测试代码和钩子之间的共享变量。如果测试失败,变量将不会被设置为 true,我会在after钩子中得到一个错误。