# Expectations and confirmations Check for expected values, outcomes, and asynchronous events in tests. ## Overview Use ``expect(_:_:sourceLocation:)`` and ``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 . Use a ``Confirmation`` to confirm the occurrence of an asynchronous event that you can't check directly using an expectation. For more information, see . ### 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. ```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:)`` fails. To stop the test when the code doesn't satisfy a requirement, use ``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:)-5l63q`` throws an instance of ``ExpectationFailedError`` when your code fails to satisfy the requirement. ## Topics ### Checking expectations - ``expect(_:_:sourceLocation:)`` - ``require(_:_:sourceLocation:)-5l63q`` - ``require(_:_:sourceLocation:)-6w9oo`` ### Checking that errors are thrown - - ``expect(throws:_:sourceLocation:performing:)-1hfms`` - ``expect(throws:_:sourceLocation:performing:)-7du1h`` - ``expect(_:sourceLocation:performing:throws:)`` - ``require(throws:_:sourceLocation:performing:)-7n34r`` - ``require(throws:_:sourceLocation:performing:)-4djuw`` - ``require(_:sourceLocation:performing:throws:)`` ### Checking how processes exit - - ``expect(processExitsWith:observing:_:sourceLocation:performing:)`` - ``require(processExitsWith:observing:_:sourceLocation:performing:)`` - ``ExitStatus`` - ``ExitTest`` ### Confirming that asynchronous events occur - - ``confirmation(_:expectedCount:isolation:sourceLocation:_:)-5mqz2`` - ``confirmation(_:expectedCount:isolation:sourceLocation:_:)-l3il`` - ``Confirmation`` ### Retrieving information about checked expectations - ``Expectation`` - ``ExpectationFailedError`` ### Representing source locations - ``SourceLocation`` - ``sourceLocation()``