我的任务是使用 testcafe 构建测试套件,在编写测试时,我偶然发现了一个特定问题“多少断言太多了?”。 基本上,测试完成后,会生成一份报告。看报告不直观。例如, 如果在网页上找不到某个元素,我会看到如下内容:
>Selector('tads') does not exist in the DOM.
这迫使我手动完成测试以验证失败的原因。
根据 testcafe 文档,您可以向断言添加可选消息。 as seen here
截至目前,我在一些地方对一些消息进行了断言。在每次点击或每个 Action 后都有一个断言(带有简明的错误消息)是否明智? (即单击登录按钮,执行断言以查看是否出现登录模式。现在登录,断言登录模式消失)
代码看起来像这样:
await t.click(this.loginButton);
await t.expect(this.loginButton.exists).ok("I don’t see the login button");
await signup.newUserSignUp();
await t.expect(this.loginButton.exists).notOk("The login modal didn’t disappear");
任何反馈都会很棒。
最佳答案
TestCafe 断言既可用于断言测试中的预期内容,也可用作等待机制以确保元素在对其执行操作之前已准备就绪。
这意味着您最终可能会在单个测试中得到许多断言。
就个人而言,我以 BDD 风格编写每个测试,如下所示:
fixture("Feature <feature-id>: description of the feature ");
test("Scenario <scenario-id>: description of the scenario", async (t) => {
// Given
await t
.<any action>
.<any action>
...
// When
await t
.<any action>
// Then
await t
.expect ...
.expect ...
...
});
在Given
和 When
部分你可以使用 t.expect() 但只能作为等待机制。
我也从未在 .ok()
中留言或 .notOk()
因为当测试失败时,我总是必须手动完成测试以验证失败的原因。
因此,以 BDD 风格构建您的测试将帮助您从这个问题切换:how much assertions is too much?
这个问题:how much tests is too much?
关于javascript - 自动化测试中有多少断言太多?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54725142/