Swift Testing

Testing Mid-level Swift 6.0+ iOS 17+ Reviewed By Alok Singh

Fill the blank

Fill the blank with the Swift Testing check.

import Testing

@Test("Total includes tax")
func totalIncludesTax() {
    let cart = Cart(items: [Item(price: 25)], taxRate: 0.2)
    ____(cart.total == 30)
}
Options
Hint 1 of 1

Which macro checks without stopping the test?

Choose an answer, then open Show answer below to check it.

Show answer

Answer: A #expect

Correct. It records a failure with the values involved and keeps running.

Why not the others?
  • B XCTAssertTrue. XCTest assertions don't report properly inside Swift Testing tests.
  • C #require. #require throws, so it needs try and is for preconditions that should stop the test.
  • D assert. assert crashes in debug builds and does nothing in release; it isn't a test check.

30-second answer

Swift Testing is the newer testing framework that ships with Swift 6 and Xcode 16. Tests are functions marked @Test, grouped in @Suite types, and checked with #expect and #require. It runs tests in parallel by default, supports parameterized tests with arguments:, and adds traits like tags, time limits and conditions. It can live alongside XCTest in the same target.

Practise saying it · 45 s. No recording, just a timer

Explain Swift Testing.

Say your answer out loud, then check which of these points you covered:

  • @Test and @Suite
  • #expect and #require
  • Parallel and parameterized
  • Coexists with XCTest

Deep dive

#require stops the test when a precondition fails, and unwraps optionals.

Each test gets a fresh suite instance, so init works as setUp.

Use confirmation for events that must happen a number of times.

UI tests and performance tests still use XCTest.

Common mistakes

  • Calling XCTAssert functions inside @Test functions.
  • Assuming tests run serially.

Senior take & follow-ups

Seniors know when to adopt it and what still needs XCTest.

Follow-up questions

Think of your answer first, then open the hint.

  1. How do you run one test with many inputs?

    Show hint

    @Test(arguments:).

  2. What replaces setUp?

    Show hint

    The suite's init; deinit replaces tearDown for classes.

Suggest a fix if something here is wrong or out of date.