WWDC23 SALE: Save 50% on all my Swift books and bundles! >>

How to test throwing functions

Swift version: 5.6

Paul Hudson    @twostraws   

One of the many brilliant features of Swift’s error handling system is its ability to throw errors during tests and have them considered as failures. That is, if you mark your test using throws you run any throwing code inside that test and if it throws an error the test will be marked as a failure.

For example, if I have an ImageGenerator struct that has a throwing method called generateImages(), I could test it out using code like this:

func testFailingExample() throws {
    let generator = ImageGenerator()
    let result = try generator.generateImages()
    XCTAssertTrue(result, "Image generation should complete successfully.")

That creates an instance of the struct, attempts to run its generateImages() method, then asserts that the result of the method was true. If generateImages() throws an error it won’t be caught inside the test – there’s no do/catch blocks in there – so instead it will bubble up to the XCTestCase, which will automatically mark the test as being failed.

Although this approach works well for individual throwing methods like you see above, I don’t think you should use it for more complex tests because you can mask failures too easily. If you have three throwing function calls inside a single test, it’s a better idea to wrap them individually in do/catch blocks so you can deal with the error inline by calling XCTFail() at the point of failure.

Save 50% in my WWDC23 sale.

SAVE 50% To celebrate WWDC23, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

Available from iOS

Similar solutions…

About the Swift Knowledge Base

This is part of the Swift Knowledge Base, a free, searchable collection of solutions for common iOS questions.

Buy Pro Swift Buy Pro SwiftUI Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Coding Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Advanced iOS Volume Three Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with macOS Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Beyond Code

Was this page useful? Let us know!

Average rating: 1.0/5

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.