Testing
Test models with XCTest and async code with expectations.
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.
import XCTest
final class LessonTests: XCTestCase {
func testShortLessonClassification() {
XCTAssertEqual(classify(minutes: 9), "short")
}
}Your turn
EasyWrite an assertion
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 inFinished reading?
Lock in what you just learned.
0%
Mark this lesson complete to save your place and move one step closer to the certificate.