Swift Testing is the most recent and best testing framework from Apple. It was constructed by Apple Engineers to assist Swift builders construct unit checks in a contemporary, quick, easy and expressive means.
This new framework replaces the previous unit take a look at framework XCTest, that was constructed for Goal-C initiatives and was bridged for use with Swift.
Whereas XCTest works sufficiently, Swift Testing builds upon it by having a extra trendy syntax and new, helpful constructs.
On this tutorial, you’ll find out about:
- Tips on how to migrate unit checks written with XCTest to Swift Testing.
- How Swift Testing handles most standard take a look at eventualities coated by XCTest.
- Helpful talents particular to Swift Testing.
Moreover, you’ll additionally discover ways to leverage Xcode’s Agentic Coding that will help you migrate unit checks.
You’ll study all about this by engaged on SwiftBrew, an app for ordering various kinds of espresso and maintaining monitor of your orders.
So seize your favourite cup, as a result of it’s time to brew some code!
Getting Began
Obtain the venture supplies by clicking the Obtain Supplies button on the prime or backside of this tutorial. Open SwiftBrew.xcodeproj contained in the Starter folder.
SwiftBrew is an app the place customers choose their espresso order from a premium collection of brews, submit their order and brew it. It retains receipts of orders and calculates the grand complete of all of your objects.
Construct and run the venture to test it out.

It’s a easy app that leverages SwiftUI to construct an intuitive UI that’s easy and quick to make use of. Nevertheless, you’ll be focusing solely on the unit checks and gained’t be altering the appliance code that defines the UI and logic of the app.
Updating Your First Unit Take a look at
You’ll begin by engaged on the checks for the code that handles the consumer’s orders, OrderModel. Contained in the SwiftBrewTests group, open OrderModelTests.swift and assessment its code.
This class makes use of XCTest to check OrderModel, the view mannequin that handles the logic of the order view. It checks a number of issues equivalent to the power of the thing so as to add orders, calculate the entire and replace the UI.
Significance of Unit Testing
XCTest was launched in 2013 by Apple as a framework that allowed builders to put in writing unit checks for his or her apps. Unit checks examine the useful correctness of small models of code. It’s a standard apply for a lot of sorts of builders, together with iOS builders, to put in writing checks to cowl as a lot conduct of their app as doable to safeguard in opposition to later code adjustments breaking present performance.
XCTest was initially written to check Goal-C code and was later tailored to work with Swift. The framework served its goal for a few years and remains to be usable; however, with the large adoption of Swift, a brand new Swift-ier testing framework, one making the most of it syntax and language options, was sure to come back alongside. Enter Swift Testing, a brand new framework launched by Apple in 2024 that leverages Swift to permit builders to put in writing unit checks in a easy and idiomatic means.
You’ll discover ways to migrate all of the unit checks of this venture from XCTest to Swift Testing.
Updating OrderModel
Nonetheless inside OrderModelTests.swift, run the entire take a look at file by clicking the diamond button on the road of the category definition.

Xcode will run all of the unit checks of this file and you’ll see the outcomes of every unit take a look at beneath the Take a look at Navigator within the left panel:

You too can see every particular person take a look at outcome within the Swift file indicated by a inexperienced examine within the line of the take a look at methodology, as an alternative of the diamond button.

All unit checks of OrderModelTests.swift are already passing. You’ll replace this file to make use of Swift Testing and also you’ll rework every take a look at methodology one after the other.
First, import the brand new framework on the prime of the file:
import Testing
Subsequent, exchange the declaration of the category with the next:
// 1
@Suite
// 2
struct OrderModelTests {
Right here’s a breakdown of this alteration:
- Provides
@Suite(_:)to the kind declaration, making this kind a collection of checks - Modifications
OrderModelTestsfrom a category to a struct.
Understanding @Suite
@Suite(_:) is a brand new macro that you simply use to group a set of associated take a look at features. Right here, you’re including this macro to the kind definition and making OrderModelTests a take a look at suite.
Additionally discover that XCTestCase can solely be applied by class sorts however you’ll be able to add @Suite(_:) to any sort, not simply courses, permitting you to alter OrderModelTests from a category to a struct.
Subsequent, discover the next methodology declaration:
func test_adding_coffee_updates_items_and_total() {
And exchange for the next:
@Take a look at func addingCoffeeUpdatesItemsAndTotal() {
Understanding @Take a look at
@Take a look at(_:) is a brand new macro that’s the middle of Swift Testing. It declares a single unit take a look at. Right here, you’re including @Take a look at(_:) to the strategy declaration making it a technique that Swift Testing could name to run a take a look at.
Discover that you simply’re additionally renaming the strategy title to make use of camel case. With XCTest, the framework required every take a look at methodology to start out with test_, in any other case the framework wouldn’t acknowledge the strategy as a take a look at methodology. And, following coding conference, take a look at methodology names could be written utilizing snake case.
Nevertheless, with Swift Testing, @Take a look at(_:) is all you’ll want to make a technique a unit take a look at, permitting you to call it nevertheless you want. Moreover, coding conference stays the identical as utility code, the place camel case is used to call features. That’s easier!
Click on the diamond button on the road of the strategy declaration to run this take a look at methodology.

Xcode runs the unit take a look at and reviews it as successful. That’s as a result of Swift Testing interoperability means that you can run new unit checks with code from XCTest. Even with out altering the interior code of the strategy, Swift Testing runs the take a look at as standard.
Updating Assertion Capabilities
Now, you’ll exchange the code that really checks the mannequin to make use of the brand new expectation macro.
Nonetheless inside OrderModelTests.swift, discover the next code:
XCTAssertTrue(mannequin.isOrderEmpty)
XCTAssertTrue(mannequin.actionsAreDisabled)
And exchange with the next:
#anticipate(mannequin.isOrderEmpty == true)
#anticipate(mannequin.actionsAreDisabled == true)
#anticipate(_:_:sourceLocation:) is a brand new macro from Swift Testing used to say a selected expression. On this case, it checks that isOrderEmpty and actionsAreDisabled are true.
Subsequent, discover this piece of code:
XCTAssertFalse(mannequin.actionsAreDisabled == false)
XCTAssertFalse(mannequin.isOrderEmpty)
XCTAssertEqual(mannequin.objects.rely, 2)
XCTAssertEqual(mannequin.complete, 7.75, accuracy: 0.001)
XCTAssertNil(mannequin.errorDescription)
XCTAssertEqual(
mannequin.objects,
[
Coffee(kind: .latte, size: .large),
Coffee(kind: .espresso, size: .small)
]
)
And exchange for:
// 1
#anticipate(mannequin.actionsAreDisabled == false)
#anticipate(mannequin.isOrderEmpty == false)
// 2
#anticipate(mannequin.objects.rely == 2)
// 3
#anticipate(mannequin.complete == 7.75)
// 4
#anticipate(mannequin.errorDescription == nil)
// 5
#anticipate(
mannequin.objects ==
[
Coffee(kind: .latte, size: .large),
Coffee(kind: .espresso, size: .small)
]
)
Right here’s a breakdown of the code:
- Right here, you exchange
XCTAssertFalse(_:_:file:line:)by#anticipate(_:_:sourceLocation:)and assert thatisOrderEmptyandactionsAreDisabledarefalse - Subsequent, use
anticipate(_:_:sourceLocation:)as an alternative ofXCTAssertEqual(_:_:_:file:line:)to say the rely of orders to be 2 - Right here, you additionally use
anticipate(_:_:sourceLocation:)to examine the grand complete of the order. Discover thatXCTAssertEqual(_:_:_:file:line:)has a parameter to examine the accuracy ofcomplete. Nevertheless, that’s not wanted on the brand new macro - Then,
XCTAssertNil(_:_:file:line:)is changed by#anticipate(_:_:sourceLocation:)too, the place you simply evaluateerrorDescriptiontonil - Lastly, you utilize
#anticipate(_:_:sourceLocation:)to say thatmannequin.objectshas the right array of Espresso
Observe: Swift Testing doesn’t have a direct substitute for XCTAssertEqual(_:_:accuracy:_:file:line:) once you use the accuracy argument to examine a decimal. Normally anticipate(_:_:sourceLocation:) goes to be sufficient. Nevertheless, if you’ll want to evaluate two values to a selected accuracy you need to use isApproximatelyEqual() from the swift-numerics bundle.
Discover that in contrast to the previous assertion strategies, XCTAssertTrue(_:_:file:line:) and XCTAssertFalse(_:_:file:line:), you not want completely different strategies to examine various kinds of information. The #anticipate(_:_:sourceLocation:) macro already addresses all of that and you utilize easy Swift operators to precise your expectation, making the code cleaner and simpler to know.
XCTAssert features and what their equivalents are in Swift Testing. Most of them use the brand new #anticipate(_:_:sourceLocation:) macro.
Run the unit take a look at and ensure it’s passing.

Success! You simply migrated your first unit take a look at.
Subsequent, you’ll discover ways to migrate a take a look at methodology that checks an async methodology name.

