子测试


【Subtests】

测试上下文的 test() 方法允许创建子测试。该方法的行为与顶层 test() 函数完全相同。下面的示例演示了如何创建一个包含两个子测试的顶层测试。

【The test context's test() method allows subtests to be created. This method behaves identically to the top level test() function. The following example demonstrates the creation of a top level test with two subtests.】

test('top level test', async (t) => {
  await t.test('subtest 1', (t) => {
    assert.strictEqual(1, 1);
  });

  await t.test('subtest 2', (t) => {
    assert.strictEqual(2, 2);
  });
}); 

在此示例中,使用 await 来确保两个子测试都已完成。这是必要的,因为父测试不会等待其子测试完成。任何在父测试结束时仍未完成的子测试都会被取消并视为失败。任何子测试失败都会导致父测试失败。

【In this example, await is used to ensure that both subtests have completed. This is necessary because parent tests do not wait for their subtests to complete. Any subtests that are still outstanding when their parent finishes are cancelled and treated as failures. Any subtest failures cause the parent test to fail.】