---
title: Expectations and confirmations
framework: testing
role: collectionGroup
role_heading: API Collection
path: testing/expectations
---

# Expectations and confirmations

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

## Overview

Overview Use expect(_:_:sourceLocation:) and require(_:_:sourceLocation:) 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. Use a 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. Validate your code’s result To validate that your code produces an expected value, use expect(_:_:sourceLocation:). This macro captures the expression you pass, and provides detailed information when the code doesn’t satisfy the expectation. @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:) fails. To stop the test when the code doesn’t satisfy a requirement, use require(_:_:sourceLocation:) instead: @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:) throws an instance of ExpectationFailedError when your code fails to satisfy the requirement.

## Topics

### Checking expectations

- [expect(_:_:sourceLocation:)](testing/expect(_:_:sourcelocation:).md)
- [require(_:_:sourceLocation:)](testing/require(_:_:sourcelocation:)-5l63q.md)
- [require(_:_:sourceLocation:)](testing/require(_:_:sourcelocation:)-6w9oo.md)

### Checking that errors are thrown

- [Testing for errors in Swift code](testing/testing-for-errors-in-swift-code.md)
- [expect(throws:_:sourceLocation:performing:)](testing/expect(throws:_:sourcelocation:performing:)-1hfms.md)
- [expect(throws:_:sourceLocation:performing:)](testing/expect(throws:_:sourcelocation:performing:)-7du1h.md)
- [expect(_:sourceLocation:performing:throws:)](testing/expect(_:sourcelocation:performing:throws:).md)
- [require(throws:_:sourceLocation:performing:)](testing/require(throws:_:sourcelocation:performing:)-7n34r.md)
- [require(throws:_:sourceLocation:performing:)](testing/require(throws:_:sourcelocation:performing:)-4djuw.md)
- [require(_:sourceLocation:performing:throws:)](testing/require(_:sourcelocation:performing:throws:).md)

### Checking how processes exit

- [Exit testing](testing/exit-testing.md)
- [expect(processExitsWith:observing:_:sourceLocation:performing:)](testing/expect(processexitswith:observing:_:sourcelocation:performing:).md)
- [require(processExitsWith:observing:_:sourceLocation:performing:)](testing/require(processexitswith:observing:_:sourcelocation:performing:).md)
- [ExitStatus](testing/exitstatus.md)
- [ExitTest](testing/exittest.md)

### Confirming that asynchronous events occur

- [Testing asynchronous code](testing/testing-asynchronous-code.md)
- [confirmation(_:expectedCount:isolation:sourceLocation:_:)](testing/confirmation(_:expectedcount:isolation:sourcelocation:_:)-5mqz2.md)
- [confirmation(_:expectedCount:isolation:sourceLocation:_:)](testing/confirmation(_:expectedcount:isolation:sourcelocation:_:)-l3il.md)
- [Confirmation](testing/confirmation.md)

### Retrieving information about checked expectations

- [Expectation](testing/expectation.md)
- [ExpectationFailedError](testing/expectationfailederror.md)

### Representing source locations

- [SourceLocation](testing/sourcelocation.md)

## See Also

### Behavior validation

- [Known issues](testing/known-issues.md)
