Swift Testing is the modern macro-based testing framework that replaces XCTest as the default for new Swift projects, using @Test, #expect, and #require instead of dozens of assertion methods. The guide walks through writing and organizing tests with structs and nested types, parameterized tests to reduce boilerplate, exit tests (available since Swift 6.2 with Xcode 26) for testing fatalError/precondition code paths, attaching values like CSV data or images to test reports, and migrating incrementally from XCTest since UI automation and performance tests still require the older framework. It also mentions an AI Agent Skill the author built to teach coding agents these Swift Testing conventions.

9m read timeFrom avanderlee.com
Post cover image
Table of contents
Writing tests using Swift TestingOrganizing tests in Swift TestingTaking a closer look at the #expect macroUsing the #require macroReducing boilerplate code using Parameterized TestsTesting fatal errors with exit testsAttaching values to testsMigrating existing XCTests to Swift TestingWriting Swift Testing code with AI

Questions this post answers

How do I test fatalError or precondition crashes in Swift Testing?

Use an exit test with #expect(processExitsWith:), which runs the test body inside a new child process and validates how that process exits, letting you cover code paths that would otherwise crash the whole test run. Exit tests require Swift 6.2 and Xcode 26 or later, run on macOS and Linux but not iOS, and any captured value must conform to both Sendable and Codable. Developers hardening crash paths can track Swift Testing capabilities like this on daily.dev.

What is the difference between #expect and #require in Swift Testing?

#expect records a failure but continues running the test, while #require throws and stops the test immediately if the condition fails, similar to XCTUnwrap for unwrapping optionals. #require is typically used when a later assertion in the test depends on a value being present, such as unwrapping a struct before checking its properties. Teams migrating XCTest suites can compare macro behavior like this via daily.dev.

Can I still use XCTest alongside Swift Testing in the same project?

Yes, both frameworks can coexist in the same test target, so migration can happen incrementally by converting assertions first, then reorganizing suites, and finally adding parameterized tests. UI automation tests using XCUIApplication and performance tests using XCTMetric still require XCTest and are not expected to migrate. Anyone planning a gradual test-suite migration can follow updates like this on daily.dev.

663 Impressions