子测试


¥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) => {
  t.test('subtest 1', (t) => {
    assert.strictEqual(1, 1);
  });

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

注意:beforeEachafterEach 钩子在每个子测试执行之间触发。

¥Note: beforeEach and afterEach hooks are triggered between each subtest execution.

任何子测试失败都会导致父测试失败。

¥Any subtest failures cause the parent test to fail.