22.8 · Swift Testing in depth: arguments, traits, errors, confirmations, isolation and scoping
The first pass named Swift Testing’s ideas; these questions run them: how arguments multiply, what traits do, how errors and known issues are asserted, how confirmations count, and what parallel by default means for shared state.
Mental model. Swift Testing treats each test, each argument and each suite as a node you can annotate with traits, and runs the nodes in parallel unless told otherwise.
Declaring many cases
Argument combinations and the traits that skip, tag and bound tests.
- With
@Test(arguments:), how are several argument collections combined, and how is a failure reported?
One argument collection runs the body once per element. Two collections give the Cartesian product: arguments: [1, 2], ["a", "b", "c"] runs six cases, which… - What are traits in Swift Testing, and how do you skip, tag, bound and annotate tests with them?
A trait is a value passed to @Test or @Suite that changes how a test is run or reported. .disabled("reason") skips the test and records the reason (the check…
Asserting outcomes
Errors, known issues and counted events.
- How do you assert thrown errors in Swift Testing, and what do
withKnownIssueandIssue.recordadd?
#expect(throws: SomeError.self) { try work() } passes only if the closure throws an error of that type; #expect(throws: Boom(code: 2)) (an Equatable error)… - How does
confirmationcount events in Swift Testing, and what can it not do?
await confirmation("name", expectedCount: n) { confirm in … } hands the closure a function to call each time the event happens, and when the closure returns it…
Isolation
Parallel execution, serial suites and per-test scoped state.
- Swift Testing runs tests in parallel by default. What does
.serializedchange, and what does it not fix?
Swift Testing runs tests concurrently, in no guaranteed order, and each test gets a fresh suite instance. Three tests that each sleep 400 ms were alive at the… - How do you give each test its own environment without globals, using
TestScopingand task locals?
A trait that conforms to TestScoping (together with TestTrait and/or SuiteTrait) implements provideScope(for:testCase:performing:), which wraps the execution…
More in Testing
- 22.1 · The pyramid, and what not to test
- 22.2 · XCTest and Swift Testing
- 22.3 · Async tests, expectations and controlling time
- 22.4 · Fakes, mocks and the inputs that make tests random
- 22.5 · Snapshot tests and UI tests
- 22.6 · Testing SwiftUI, tvOS focus, and watchOS logic
- 22.7 · Flakes, hermetic CI, and a quality system for a large team
- 22.9 · XCTest lifecycle and expectations, Sendable doubles, and the main actor in tests