Hooks
Hooks 允许你在测试或测试套件执行之前或之后运行初始化和清理逻辑。
beforeAll
- 类型:
(fn: (ctx: SuiteContext) => void | Promise<void>, timeout?: number) => void
在当前套件的所有测试之前运行。
beforeAll 也支持返回一个函数,在所有测试之后运行,用于清理操作(等价于 afterAll):
afterAll
- 类型:
(fn: (ctx: SuiteContext) => void | Promise<void>, timeout?: number) => void
在当前套件的所有测试之后运行。
beforeEach
- 类型:
(fn: () => void | Promise<void>, timeout?: number) => void
在当前套件的每个测试之前运行。
beforeEach 也支持返回一个函数,在每个测试之后运行,用于清理操作(等价于 afterEach):
afterEach
- 类型:
(fn: (ctx: { task: { result: Readonly<TestResult> } }) => void | Promise<void>, timeout?: number) => void
在当前套件的每个测试之后运行。
onTestFinished
- 类型:
(fn: (ctx: { task: { result: Readonly<TestResult> } }) => void | Promise<void>, timeout?: number) => void
测试运行完成后调用,无论测试成功或失败。可用于执行清理操作。该 hook 会在 afterEach 之后执行。
需要注意的是,当你在并发测试中使用 onTestFinished hook 时,你应当从测试上下文中获取该 hook。这是因为 Rstest 在并发测试中无法准确追踪来自全局的 onTestFinished hook 所属的具体测试。
onTestFailed
- Type:
(fn: (ctx: { task: { result: Readonly<TestResult> } }) => void | Promise<void>, timeout?: number) => void
测试运行失败后调用。
需要注意的是,当你在并发测试中使用 onTestFailed hook 时,你应当从测试上下文中获取该 hook。这是因为 Rstest 在并发测试中无法准确追踪来自全局的 onTestFailed hook 所属的具体测试。