Swift·Swift from optionals to SwiftUI

Testing

Test models with XCTest and async code with expectations.

Beginner 13 min read Lesson 9 of 300 Challenge included
What you will be able to do
  • Test pure models without a simulator.
  • Use XCTAssertEqual and XCTAssertThrowsError.
  • Use async test methods for await.
  • Name tests by the behaviour they protect.

In plain English

Keep business logic in plain Swift types so tests are fast. XCTest supports async tests directly. Test behaviour and edge cases, not private implementation details.

LessonTests.swiftSwift
import XCTest

final class LessonTests: XCTestCase {
    func testShortLessonClassification() {
        XCTAssertEqual(classify(minutes: 9), "short")
    }
}
Your turn

Write an assertion

Easy

Assert that classify(minutes: 9) equals "short".

SWIFT · starter
XCTAssertEqual()
Check your understanding before moving on.
Knowledge checkQuestion 1

Which assertion checks equality in XCTest?

Select an answer to continue
Study tools

Make it yours.

Keep a private note for this lesson, then turn the key ideas into flashcards you can revisit.

Sign in to save notes and flashcards.Both are private to your account and stay with your progress.
Sign in
Finished reading?

Lock in what you just learned.

Mark this lesson complete to save your place and move one step closer to the certificate.

Next: Build a lessons app
Lesson 9 of 300 · 0% of course