Swift Testing: Getting Started

In 2021, Apple released Swift concurrency to an adoring audience — finally, developers could write Swift code to implement concurrency in Swift apps! At WWDC 2024, developers got another game changer — Swift Testing. By Audrey Tam.

Leave a rating/review
Download materials
Save for later
Share
You are currently viewing page 3 of 3 of this article. Click here to view the first page.

Tagging Tests

You can assign custom tags to tests that have common characteristics, even if they're in different suites or files, and they appear in the Test navigator. For example, you might tag all tests that validate a specific feature or subsystem. You can assign a tag to a test function or to a test suite:

@Suite(.tags(.formatting))
struct MetadataPresentation {
    let video = Video(fileName: "By the Lake.mov")

    @Test func rating() async throws {
        #expect(video.contentRating == "G")
    }

    @Test func formattedDuration() async throws {
        let videoLibrary = try await VideoLibrary()
        let video = try #require(await videoLibrary.video(named: "By the Lake"))
        #expect(video.formattedDuration == "0m 19s")
    }
}

You can run all the tests with a particular tag and filter them in the Test Report. This can be helpful if it shows that multiple tests with the same tag are failing.

Swift Testing Is Open Source and Cross-Platform

Swift Testing is open source and soon will transition to the recently announced swiftlang organization. It works on all Apple operating systems that support Swift Concurrency, as well as on Linux and Windows. It has a common codebase across all these platforms, so your tests behave much more consistently when moving between platforms, and there will be better functional parity between them.

Swift Testing has an open feature-proposal process, and Apple wants you to get involved by writing or participating in feature proposals, improving the documentation, or filing bug reports.

Download the final projects by clicking the Download Materials button at the top or bottom of this article.

In this article, you learned about Swift Testing building blocks:

  • @Test
  • #expect and #require
  • Suites
  • Traits

The key Apple links are:

I hope you're excited about Swift Testing and will soon have complete test coverage for all your apps!

Contributors

Adriana Kutenko

Illustrator

Gina De La Rosa

Team Lead

Over 300 content creators. Join our team.