在 TestCafe 中有没有办法知道测试是通过还是失败?
In TestCafe is there a way to know if the test passed or failed in after hook?
我正在尝试通过休息 API (Zephyr) 将测试标记为 pass/failed,而我的 testcafe 测试正在 运行ning。我想知道是否可以在 after 或 afterEach 挂钩中知道测试 passed/failed 是否可以 运行 基于结果的一些脚本。
类似于:
test(...)
.after(async t => {
if(testFailed === true) { callApi('my test failed'); }
})
我看到有两种方法可以解决您的任务。首先,不要订阅 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
钩子中得到一个错误。
这可以从测试控制器中确定,其中嵌套了更多信息,这些信息仅在 运行 时间可见。包含测试中抛出的所有错误的数组可用如下
t.testRun.errs
如果数组已填充,则测试失败。
我正在尝试通过休息 API (Zephyr) 将测试标记为 pass/failed,而我的 testcafe 测试正在 运行ning。我想知道是否可以在 after 或 afterEach 挂钩中知道测试 passed/failed 是否可以 运行 基于结果的一些脚本。
类似于:
test(...)
.after(async t => {
if(testFailed === true) { callApi('my test failed'); }
})
我看到有两种方法可以解决您的任务。首先,不要订阅 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
钩子中得到一个错误。
这可以从测试控制器中确定,其中嵌套了更多信息,这些信息仅在 运行 时间可见。包含测试中抛出的所有错误的数组可用如下
t.testRun.errs
如果数组已填充,则测试失败。