子测试
【Subtests】
测试上下文的 test() 方法允许创建子测试。它让你可以以层次化的方式组织测试,在一个较大的测试中创建嵌套测试。该方法的行为与顶层的 test() 函数完全相同。以下示例演示了创建一个包含两个子测试的顶层测试。
【The test context's test() method allows subtests to be created.
It allows you to structure your tests in a hierarchical manner,
where you can create nested tests within a larger test.
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);
});
}); 注意:
beforeEach和afterEach钩子会在每个子测试执行之间触发。
在这个例子中,await 用于确保两个子测试都已完成。这是必要的,因为测试不会等待其子测试完成,不像在测试套件中创建的测试。当父测试完成时,任何仍未完成的子测试将被取消并视为失败。任何子测试的失败都会导致父测试失败。
【In this example, await is used to ensure that both subtests have completed.
This is necessary because tests do not wait for their subtests to
complete, unlike tests created within suites.
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.】