---
title: "addTeardownBlock(_:)"
framework: xctest
role: symbol
role_heading: Instance Method
path: "xctest/xctestcase/addteardownblock(_:)-2guon"
---

# addTeardownBlock(_:)

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

## Declaration

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

## Parameters

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

## Discussion

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. note: You can register a teardown block within a test case’s setUp(), setUpWithError(), or setUp(completion:) instance methods, but not from within its tearDown(), tearDownWithError(), or tearDown(completion:) instance methods, or from within another teardown block. Teardown blocks always execute before the test system calls the tearDown(), tearDownWithError(), or tearDown(completion:) instance methods. 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

- [Set Up and Tear Down State in Your Tests](xctest/set-up-and-tear-down-state-in-your-tests.md)
- [setUp()](xctest/xctestcase/setup().md)
- [addTeardownBlock(_:)](xctest/xctestcase/addteardownblock(_:)-5zw6c.md)
- [tearDown()](xctest/xctestcase/teardown().md)
