<!--
{
  "availability" : [
    "iOS: 13.0.0 -",
    "iPadOS: 13.0.0 -",
    "macCatalyst: 13.0.0 -",
    "tvOS: 13.0.0 -",
    "watchOS: 6.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "XCTest",
  "identifier" : "/documentation/XCTest/XCTestCase/addTeardownBlock(_:)-2guon",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "XCTest"
    ],
    "preciseIdentifier" : "s:So10XCTestCaseC0A0E16addTeardownBlockyyyyYaYbKcF"
  },
  "title" : "addTeardownBlock(_:)"
}
-->

# addTeardownBlock(_:)

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

```
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()`](/documentation/XCTest/XCTest/tearDown()), [`tearDownWithError()`](/documentation/XCTest/XCTest/tearDownWithError()), or [`tearDown(completion:)`](/documentation/XCTest/XCTest/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 ``doc://com.apple.xctest/documentation/XCTest/XCTest/setUp()``, ``doc://com.apple.xctest/documentation/XCTest/XCTest/setUpWithError()``, or ``doc://com.apple.xctest/documentation/XCTest/XCTest/setUp(completion:)`` instance methods, but not from within its ``doc://com.apple.xctest/documentation/XCTest/XCTest/tearDown()``, ``doc://com.apple.xctest/documentation/XCTest/XCTest/tearDownWithError()``, or ``doc://com.apple.xctest/documentation/XCTest/XCTest/tearDown(completion:)`` instance methods, or from within another teardown block. Teardown blocks always execute before the test system calls the ``doc://com.apple.xctest/documentation/XCTest/XCTest/tearDown()``, ``doc://com.apple.xctest/documentation/XCTest/XCTest/tearDownWithError()``, or ``doc://com.apple.xctest/documentation/XCTest/XCTest/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.

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)