context.test([name][, options][, fn])
-
name
<string> 子测试的名称,在报告测试结果时显示。默认值:fn
的name
属性,如果fn
没有名称,则为'<anonymous>'
。¥
name
<string> The name of the subtest, which is displayed when reporting test results. Default: Thename
property offn
, or'<anonymous>'
iffn
does not have a name. -
options
<Object> 子测试的配置选项。支持以下属性:¥
options
<Object> Configuration options for the subtest. The following properties are supported:-
concurrency
<number> | <boolean> | <null> 如果提供了一个数字,那么许多测试将在应用线程中并行运行。如果是true
,它将并行运行所有子测试。如果是false
,它一次只会运行一个测试。如果未指定,则子测试从其父测试继承此值。默认值:null
。¥
concurrency
<number> | <boolean> | <null> If a number is provided, then that many tests would run in parallel within the application thread. Iftrue
, it would run all subtests in parallel. Iffalse
, it would only run one test at a time. If unspecified, subtests inherit this value from their parent. Default:null
. -
only
<boolean> 如果为真,并且测试上下文配置为运行only
测试,则将运行此测试。否则跳过测试。默认值:false
。¥
only
<boolean> If truthy, and the test context is configured to runonly
tests, then this test will be run. Otherwise, the test is skipped. Default:false
. -
signal
<AbortSignal> 允许中止正在进行的测试。¥
signal
<AbortSignal> Allows aborting an in-progress test. -
skip
<boolean> | <string> 如果为真,则跳过测试。如果提供了字符串,则该字符串将作为跳过测试的原因显示在测试结果中。默认值:false
。¥
skip
<boolean> | <string> If truthy, the test is skipped. If a string is provided, that string is displayed in the test results as the reason for skipping the test. Default:false
. -
todo
<boolean> | <string> 如果为真,则测试标记为TODO
。如果提供了字符串,则该字符串会显示在测试结果中作为测试为TODO
的原因。默认值:false
。¥
todo
<boolean> | <string> If truthy, the test marked asTODO
. If a string is provided, that string is displayed in the test results as the reason why the test isTODO
. Default:false
. -
timeout
<number> 测试失败的毫秒数。如果未指定,则子测试从其父测试继承此值。默认值:Infinity
。¥
timeout
<number> A number of milliseconds the test will fail after. If unspecified, subtests inherit this value from their parent. Default:Infinity
. -
plan
<number> 预期在测试中运行的断言和子测试的数量。如果测试中运行的断言数量与计划中指定的数量不匹配,则测试将失败。默认值:undefined
。¥
plan
<number> The number of assertions and subtests expected to be run in the test. If the number of assertions run in the test does not match the number specified in the plan, the test will fail. Default:undefined
.
-
-
fn
<Function> | <AsyncFunction> 被测试的函数。此函数的第一个参数是TestContext
对象。如果测试使用回调,则回调函数作为第二个参数传入。默认值:空操作函数。¥
fn
<Function> | <AsyncFunction> The function under test. The first argument to this function is aTestContext
object. If the test uses callbacks, the callback function is passed as the second argument. Default: A no-op function. -
返回:<Promise> 测试完成后,满足
undefined
。¥Returns: <Promise> Fulfilled with
undefined
once the test completes.
此函数用于在当前测试下创建子测试。此函数的行为方式与顶层的 test()
函数相同。
¥This function is used to create subtests under the current test. This function
behaves in the same fashion as the top level test()
function.
test('top level test', async (t) => {
await t.test(
'This is a subtest',
{ only: false, skip: false, concurrency: 1, todo: false, plan: 1 },
(t) => {
t.assert.ok('some relevant assertion here');
},
);
});