The Case of the Crimson Test Suite


An Introduction to Swift Testing

The Case of the Crimson Test Suite

"The Case of the Crimson Test Suite" is an introduction to the modern Swift Testing framework.

Testing your packages and apps for Apple platforms has never been easier. The new Swift Testing framework makes it easy for you to write tests that are clean and clear with easy-to-read feedback when a test fails. In this book we begin with tests that run as freestanding functions. Later, we gather some of them up into a suite so that we can run a test individually, as part of a suite, or as one of the entire tests in the current test plan.

You'll write expectations and requirements to test the result of calling your production code and you'll verify methods that should and do throw an error. You'll test asynchronous code and, in those rare cases when its needed, write serialized tests. By default Swift Testing runs your tests in parallel in random order for both improved performance and to catch hidden dependencies. You'll create display names and use test description to improve the test output and introduce tags so that you can run a subset of your tests without suites or across many suites.

Requires Xcode 16 (beta 5 or later) and Swift 6.

Contents


Chapter 1: Getting Started

The Agile Detective

A Project with Tests

Adding a Test Target to a Project

A Package with Tests

Adding a Test Target to a Package

Road Map

Credits

Chapter 2: The Basics of Swift Testing

A Clean Slate

Testing an init()

Testing a Computed Property

Multiple Expectations

Display Names

Tags

Testing the Public Interface

Observable

Chapter 3: Test Organization and Flow

The Calculator Project

Test Driving the Stack

Testing Errors

#expect(throws:)

#require()

Group the Tests

Suite

Suite in a Suite

Chapter 4: The Finer Points

Serial and Parallel

More Serial Tests

Cross-Contamination

Testing Async Code

Unary Operators

Binary Operators

Parametrized Tests

More Parametrized Tests

Test Description

Related


Swift Testing Training from Dim Sum Thinking