Contents

addTeardownBlock(_:)

Registers a block of teardown code to run after the current test method ends.

Declaration

func addTeardownBlock(_ block: @escaping  @Sendable () async throws -> Void)

Parameters

  • block:

    A block of code that cleans up resources after a test run.

Discussion

Call this method during a test method’s execution to register a block of code to be called when the test method ends. The block of code may contain asynchronous teardown logic; the test system waits until the asynchronous teardown is complete before proceeding.

Registered teardown blocks are called before the tearDown(), tearDownWithError(), or tearDown(completion:) instance methods for their associated test case are executed. Teardown blocks are run on the main thread, but can be registered from any thread. Each registered block is run once, in last-in, first-out order, executed serially.

Use teardown blocks to write test-specific teardown code alongside associated setup code. For example, if a test method needs to create a resource that must be deleted when the test completes, write the code to create the resource, followed immediately by code that registers a teardown block to delete the resource.

See Also

Customizing Test Setup and Teardown