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.

  1. With @Test(arguments:), how are several argument collections combined, and how is a failure reported? 22.8.1 · L3
    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…
  2. What are traits in Swift Testing, and how do you skip, tag, bound and annotate tests with them? 22.8.2 · L3
    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.

  1. How do you assert thrown errors in Swift Testing, and what do withKnownIssue and Issue.record add? 22.8.3 · L3
    #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)…
  2. How does confirmation count events in Swift Testing, and what can it not do? 22.8.4 · L5
    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.

  1. Swift Testing runs tests in parallel by default. What does .serialized change, and what does it not fix? 22.8.5 · L5
    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…
  2. How do you give each test its own environment without globals, using TestScoping and task locals? 22.8.6 · L5
    A trait that conforms to TestScoping (together with TestTrait and/or SuiteTrait) implements provideScope(for:testCase:performing:), which wraps the execution…

More in Testing