自动化测试中有多少断言?

Roo*_*t H 5 javascript automated-tests e2e-testing testcafe

我被赋予了使用testcafe构建测试服的任务,并且在我编写测试时,我偶然发现了一个特定问题"多少断言太多了?".基本上,在测试完成后,会生成一个报告.查看报告并不直观.例如,如果在网页上找不到元素,我会看到如下内容:

>Selector('tads') does not exist in the DOM. 
Run Code Online (Sandbox Code Playgroud)

这迫使我手动完成测试以验证失败的原因.

根据testcafe文档,您可以向断言添加可选消息.如此处所见

截至目前,我在一些地方发布了一些消息.在每次点击或每次操作后都有一个断言(带有简明的错误信息)是否明智?(即单击登录按钮,执行断言以查看登录模式是否出现.现在登录,断言登录模式消失)

代码看起来像这样:

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

任何反馈都会很棒.

hdo*_*val 6

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 ...
   ...

});
Run Code Online (Sandbox Code Playgroud)

GivenWhen部分中,您可以使用t.expect()但仅作为等待机制.

我还从来没有在放消息.ok().notOk()因为测试失败时,我总是要经过测试手动验证什么失败.

因此,以BDD样式构建测试将帮助您从这个问题切换how much assertions is too much?到这个问题:how much tests is too much?