How can I migrate from XCTest to the new swift test package
Generated on 7/31/2024
1 search
To migrate from XCTest to the new Swift Testing package, you can follow these steps:
-
Add a Test Bundle Target: If you haven't already, add a test bundle target to your project. In Xcode, choose
File > New > Target
, then search forUnit Testing Bundle
in the test section. Swift Testing is now the default choice for this template in Xcode 16. -
Understand the Differences: Swift Testing has some similarities to XCTest but also important differences. For example, tests in XCTest are any method whose name begins with
test
, while Swift Testing uses the@test
attribute to denote them explicitly. Swift Testing supports more kinds of functions, including instance methods, static methods, and global functions. -
Translate Assertions: Avoid calling XCTest assertion functions from Swift Testing tests, or the opposite. Check out the documentation on migrating a test from XCTest for details on how to translate assertions and handle asynchronous waiting conditions.
-
Parameterize Tests: When migrating multiple XCTest methods with a similar structure, you can consolidate them into one parameterized test. Swift Testing allows you to use tags to organize and filter your tests and to use arguments to avoid duplicating the same test for multiple inputs.
-
Setup and Teardown: In Swift Testing, use the type's initializer for setup logic, which can be
async
orthrow
. For teardown logic, include a deinitializer, which can only be used when the suite type is an actor or class. -
Coexistence: XCTest and Swift Testing tests can coexist in a single target, so you can migrate incrementally without needing to create a new target first.
For more detailed guidance, you can refer to the session Meet Swift Testing (17:35).
Relevant Sessions
Meet Swift Testing
Introducing Swift Testing: a new package for testing your code using Swift. Explore the building blocks of its powerful new API, discover how it can be applied in common testing workflows, and learn how it relates to XCTest and open source Swift.
What’s new in Swift
Join us for an update on Swift. We’ll briefly go through a history of Swift over the past decade, and show you how the community has grown through workgroups, expanded the package ecosystem, and increased platform support. We’ll introduce you to a new language mode that achieves data-race safety by default, and a language subset that lets you run Swift on highly constrained systems. We’ll also explore some language updates including noncopyable types, typed throws, and improved C++ interoperability.
Platforms State of the Union
Discover the newest advancements on Apple platforms.