0Pricing
Swift Academy · Lesson

Async Tests and Performance Measurement

Testing async functions with async/await and measuring with XCTMeasureOptions.

Testing Async Functions

Declare test methods as async throws to use await directly inside test bodies without extra boilerplate.

final class FetcherTests: XCTestCase {
  func testFetchReturnsUser() async throws {
    let user = try await UserService().fetchUser(id: 1)
    XCTAssertEqual(user.name, "Alice")
  }
}

Async setUp and tearDown

Override setUp() async throws to perform async initialization before each test.

override func setUp() async throws {
  try await super.setUp()
  sut = await AsyncService.make()
}

All lessons in this course

  1. XCTestCase Setup, Teardown and Test Methods
  2. XCTAssert Family and Throwing Assertions
  3. Protocol-Based Mocking and Dependency Injection
  4. Async Tests and Performance Measurement
← Back to Swift Academy