Thursday, June 10, 2021 [Tweets] [Favorites]

How to Test Swift async/await Code With XCTest

Giovanni Lodi:

Because async/await is a feature at the Swift language level, to test an async function we can use the same approach we’d use to consume that code in production: call it with await.

[…]

To appreciate how neater this is than the approach we used before the introduction of async await, let me show you the same test but for a chopVegetables version using callbacks and Result.

[…]

Unfortunately the XCTAssertThrowsError and the other assertion APIs don’t support concurrency yet, so do catch is the only option to test the error path of async code.

Comments

Stay up-to-date by subscribing to the Comments RSS Feed for this post.

Leave a Comment