Logo for tanaschita.com

Developer guide on Swift Testing for iOS

Learn how use Swift Testing to write automated tests.

01 Jul 2024 · 5 min read

At WWDC 2024, Apple released Swift Testing - a new framework which allows us to write automated tests for iOS applications.

Starting with Xcode 16, Swift Testing is now the default Testing framework when we add a testing target to our iOS project.

Let's directly jump in and look at how test functions look in Swift Testing.

Sponsorship logo
Architecture & Design Patterns for iOS
This book is a practical guide to essential architectural principles and design patterns for iOS development. It covers strategies for building maintainable apps with Swift and SwiftUI, including dependency injection, navigation, common patterns, and modularization.
LEARN MORE

Test functions

An example of a test with Swift Testing looks as follows:

import Testing
@testable import ExampleApp
struct ExampleAppTests {
@Test func testExample() async throws {
...
expect(resultName == expectedName)
}
}

A test function follows these rules:

  • the @Test annotation indicates that a function is a test.
  • a test function can be a global function or a method in a type
  • a test function can be async
  • a test function can throw

Expectations

To validate expected conditions, we can use the #expect macro. It accepts ordinary expressions and operators, for example:

#expect(!array.isEmpty)
#expect(greeting == "Hello")

Alternatively, we can use the #require macro to throw an error and to stop the test if the condition is false. For example:

try #require(!result.isEmpty)

We can also use the #require macro to unwrap an optional:

let element = try #require(array.first)

Traits

We can add traits to the test, for example to describe a test or to customize how it behaves. For example:

Suits

To group related test functions we can use suites. A suite is implicitly created when we wrap test functions for example into a struct:

struct ExampleTests {
@Test func test1() async throws {
...
}
@Test func test2() {
...
}
}

With suites, we can use init and deinit functions for setup and teardown logic.

Parameterized testing

Swift Testing supports parameterized tests allowing us to run the same test logic multiple times with different input values:

@Test(arguments: ["a", "b", "c"])
func example(input: String) {
...
}

Migrating from XCTest

Swift Testing tests can coexist with XCTest in a single unit test target. This helps us migrate tests incrementally.

Swift Testing does currently not support Ul automation APls such as XCUIApplication or performance testing APls such as XCMetric.

Sponsorship logo
Preparing for a technical iOS job interview
Preparing for a technical iOS Job Interview with over 300 questions & answers. Covering Swift & Objective-C, SwiftUI & UIKit, Combine, HTTP Networking, iOS File System, Core Data, Concurrency with async/await, Security, Automated Testing, Dependency Management, AI & Machine Learning and more.
LEARN MORE
Sponsorship logo
Architecture & Design Patterns for iOS
This book is a practical guide to essential architectural principles and design patterns for iOS development. It covers strategies for building maintainable apps with Swift and SwiftUI, including dependency injection, navigation, common patterns, and modularization.
LEARN MORE
Sponsorship logo
Become a sponsor of tanaschita.com
By publishing an article on different iOS topics every week, tanaschita.com is constantly growing in the developer community and may provide a great audience for you as a sponsor.
CLICK TO LEARN MORE

Newsletter

Image of a reading marmot
Subscribe

Like to support my work?

Say hi

Related tags

Articles with related topics

storekit

testing

swift

ios

How to test in-app purchases locally using StoreKit

A practical guide to simulating in-app purchases and subscriptions in Xcode.

08 Apr 2026 · 6 min read

Latest articles and tips

© 2026 tanaschita.com

Privacy policy

Impressum