<!--
{
  "documentType" : "article",
  "framework" : "Testing",
  "identifier" : "/documentation/Testing/Expectations",
  "metadataVersion" : "0.1.0",
  "role" : "collectionGroup",
  "title" : "Expectations and confirmations"
}
-->

# Expectations and confirmations

Check for expected values, outcomes, and asynchronous events in tests.

## Overview

Use [`expect(_:_:sourceLocation:)`](/documentation/Testing/expect(_:_:sourceLocation:)) and
[`require(_:_:sourceLocation:)`](/documentation/Testing/require(_:_:sourceLocation:)-5l63q) macros to validate expected
outcomes. To validate that an error is thrown, or *not* thrown, the
testing library provides several overloads of the macros that you can
use. For more information, see [Testing for errors in Swift code](/documentation/Testing/testing-for-errors-in-swift-code).

Use a [`Confirmation`](/documentation/Testing/Confirmation) to confirm the occurrence of an
asynchronous event that you can’t check directly using an expectation.
For more information, see [Testing asynchronous code](/documentation/Testing/testing-asynchronous-code).

### Validate your code’s result

To validate that your code produces an expected value, use
[`expect(_:_:sourceLocation:)`](/documentation/Testing/expect(_:_:sourceLocation:)). This macro captures the
expression you pass, and provides detailed information when the code doesn’t
satisfy the expectation.

```swift
@Test func calculatingOrderTotal() {
  let calculator = OrderCalculator()
  #expect(calculator.total(of: [3, 3]) == 7)
  // Prints "Expectation failed: calculator.total(of: [3, 3]) == 7"
}
```

Your test keeps running after [`expect(_:_:sourceLocation:)`](/documentation/Testing/expect(_:_:sourceLocation:)) fails. To stop
the test when the code doesn’t satisfy a requirement, use
[`require(_:_:sourceLocation:)`](/documentation/Testing/require(_:_:sourceLocation:)-5l63q) instead:

```swift
@Test func returningCustomerRemembersUsualOrder() throws {
  let customer = try #require(Customer(id: 123))
  // The test runner doesn't reach this line if the customer is nil.
  #expect(customer.usualOrder.countOfItems == 2)
}
```

[`require(_:_:sourceLocation:)`](/documentation/Testing/require(_:_:sourceLocation:)-5l63q) throws an instance of
[`ExpectationFailedError`](/documentation/Testing/ExpectationFailedError) when your code fails to satisfy the requirement.

## Topics

### Checking expectations

[`expect(_:_:sourceLocation:)`](/documentation/Testing/expect(_:_:sourceLocation:))

Check that an expectation has passed after a condition has been evaluated.

[`require(_:_:sourceLocation:)`](/documentation/Testing/require(_:_:sourceLocation:)-5l63q)

Check that an expectation has passed after a condition has been evaluated
and throw an error if it failed.

[`require(_:_:sourceLocation:)`](/documentation/Testing/require(_:_:sourceLocation:)-6w9oo)

Unwrap an optional value or, if it is `nil`, fail and throw an error.

### Checking that errors are thrown

[Testing for errors in Swift code](/documentation/Testing/testing-for-errors-in-swift-code)

Ensure that your code handles errors in the way you expect.

[`expect(throws:_:sourceLocation:performing:)`](/documentation/Testing/expect(throws:_:sourceLocation:performing:)-1hfms)

Check that an expression always throws an error of a given type.

[`expect(throws:_:sourceLocation:performing:)`](/documentation/Testing/expect(throws:_:sourceLocation:performing:)-7du1h)

Check that an expression always throws a specific error.

[`expect(_:sourceLocation:performing:throws:)`](/documentation/Testing/expect(_:sourceLocation:performing:throws:))

Check that an expression always throws an error matching some condition.

[`require(throws:_:sourceLocation:performing:)`](/documentation/Testing/require(throws:_:sourceLocation:performing:)-7n34r)

Check that an expression always throws an error of a given type, and throw
an error if it does not.

[`require(throws:_:sourceLocation:performing:)`](/documentation/Testing/require(throws:_:sourceLocation:performing:)-4djuw)

[`require(_:sourceLocation:performing:throws:)`](/documentation/Testing/require(_:sourceLocation:performing:throws:))

Check that an expression always throws an error matching some condition, and
throw an error if it does not.

### Checking how processes exit

[Exit testing](/documentation/Testing/exit-testing)

Use exit tests to test functionality that might cause a test process to exit.

[`expect(processExitsWith:observing:_:sourceLocation:performing:)`](/documentation/Testing/expect(processExitsWith:observing:_:sourceLocation:performing:))

Check that an expression causes the process to terminate in a given fashion.

[`require(processExitsWith:observing:_:sourceLocation:performing:)`](/documentation/Testing/require(processExitsWith:observing:_:sourceLocation:performing:))

Check that an expression causes the process to terminate in a given fashion
and throw an error if it did not.

[`ExitStatus`](/documentation/Testing/ExitStatus)

An enumeration describing possible status a process will report on exit.

[`ExitTest`](/documentation/Testing/ExitTest)

A type describing an exit test.

### Confirming that asynchronous events occur

[Testing asynchronous code](/documentation/Testing/testing-asynchronous-code)

Validate whether your code causes expected events to happen.

[`confirmation(_:expectedCount:isolation:sourceLocation:_:)`](/documentation/Testing/confirmation(_:expectedCount:isolation:sourceLocation:_:)-5mqz2)

Confirm that some event occurs during the invocation of a function.

[`confirmation(_:expectedCount:isolation:sourceLocation:_:)`](/documentation/Testing/confirmation(_:expectedCount:isolation:sourceLocation:_:)-l3il)

Confirm that some event occurs during the invocation of a function.

[`Confirmation`](/documentation/Testing/Confirmation)

A type that can be used to confirm that an event occurs zero or more times.

### Retrieving information about checked expectations

[`Expectation`](/documentation/Testing/Expectation)

A type describing an expectation that has been evaluated.

[`ExpectationFailedError`](/documentation/Testing/ExpectationFailedError)

A type describing an error thrown when an expectation fails during
evaluation.

### Representing source locations

[`SourceLocation`](/documentation/Testing/SourceLocation)

A type representing a location in source code.



---

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)