按名称过滤测试
【Filtering tests by name】
--test-name-pattern 命令行选项可用于仅运行名称匹配所提供模式的测试,而 --test-skip-pattern 选项可用于跳过名称匹配所提供模式的测试。测试名称模式被解释为 JavaScript 正则表达式。--test-name-pattern 和 --test-skip-pattern 选项可以多次指定,以便运行嵌套测试。对于每个执行的测试,任何相应的测试钩子,例如 beforeEach(),也会运行。未执行的测试将从测试运行输出中省略。
【The --test-name-pattern command-line option can be used to only run
tests whose name matches the provided pattern, and the
--test-skip-pattern option can be used to skip tests whose name
matches the provided pattern. Test name patterns are interpreted as
JavaScript regular expressions. The --test-name-pattern and
--test-skip-pattern options can be specified multiple times in order to run
nested tests. For each test that is executed, any corresponding test hooks,
such as beforeEach(), are also run. Tests that are not executed are omitted
from the test runner output.】
给定以下测试文件,使用 --test-name-pattern="test [1-3]" 选项启动 Node.js 会导致测试运行器执行 test 1、test 2 和 test 3。如果 test 1 不符合测试名称模式,则其子测试将不会执行,即使它们匹配该模式。同一组测试也可以通过多次传递 --test-name-pattern 来执行(例如 --test-name-pattern="test 1"、--test-name-pattern="test 2" 等)。
【Given the following test file, starting Node.js with the
--test-name-pattern="test [1-3]" option would cause the test runner to execute
test 1, test 2, and test 3. If test 1 did not match the test name
pattern, then its subtests would not execute, despite matching the pattern. The
same set of tests could also be executed by passing --test-name-pattern
multiple times (e.g. --test-name-pattern="test 1",
--test-name-pattern="test 2", etc.).】
test('test 1', async (t) => {
await t.test('test 2');
await t.test('test 3');
});
test('Test 4', async (t) => {
await t.test('Test 5');
await t.test('test 6');
}); 测试名称模式也可以使用正则表达式字面量来指定。这允许使用正则表达式标志。在前面的示例中,使用 --test-name-pattern="/test [4-5]/i"(或 --test-skip-pattern="/test [4-5]/i")启动 Node.js 会匹配 Test 4 和 Test 5,因为该模式不区分大小写。
【Test name patterns can also be specified using regular expression literals. This
allows regular expression flags to be used. In the previous example, starting
Node.js with --test-name-pattern="/test [4-5]/i" (or --test-skip-pattern="/test [4-5]/i")
would match Test 4 and Test 5 because the pattern is case-insensitive.】
要将单个测试与模式匹配,可以在模式前加上该测试的所有祖级测试名称,用空格分隔,以确保它是唯一的。例如,给定以下测试文件:
【To match a single test with a pattern, you can prefix it with all its ancestor test names separated by space, to ensure it is unique. For example, given the following test file:】
describe('test 1', (t) => {
it('some test');
});
describe('test 2', (t) => {
it('some test');
}); 使用 --test-name-pattern="test 1 some test" 启动 Node.js 将仅匹配 test 1 中的 some test。
【Starting Node.js with --test-name-pattern="test 1 some test" would match
only some test in test 1.】
测试名称模式不会更改测试运行程序执行的文件集。
【Test name patterns do not change the set of files that the test runner executes.】
如果同时提供了 --test-name-pattern 和 --test-skip-pattern,测试必须满足两个条件才能执行。
【If both --test-name-pattern and --test-skip-pattern are supplied,
tests must satisfy both requirements in order to be executed.】