XCTAssert Family and Throwing Assertions
Using XCTAssertEqual, XCTAssertThrowsError and XCTUnwrap safely.
XCTAssert Family and Throwing Assertions is a free Swift Academy lesson on CoddyKit. This is lesson 2 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, and your progress syncs across the web and the CoddyKit app. The Swift Academy course includes 4 lessons in total.
XCTAssert and XCTAssertTrue
XCTAssert and XCTAssertTrue fail if the condition is false.
XCTAssert(user.isLoggedIn)
XCTAssertTrue(cart.isEmpty, "Cart should be empty after clear()")XCTAssertFalse
XCTAssertFalse fails if the condition is true.
XCTAssertFalse(session.isExpired, "Fresh session should not be expired")XCTAssertEqual
XCTAssertEqual fails if the two values are not equal. Both values must be Equatable.
XCTAssertEqual(calculator.add(2, 3), 5)
XCTAssertEqual(user.name, "Alice", "Name should match the fixture")XCTAssertNotEqual
XCTAssertNotEqual fails if the two values are equal.
XCTAssertNotEqual(originalHash, updatedHash)XCTAssertNil and XCTAssertNotNil
Test optionals: fail if the value is not nil / is nil respectively.
XCTAssertNil(service.error, "No error expected after successful fetch")
XCTAssertNotNil(result, "Result should be non-nil on success")XCTAssertThrowsError
Fail if the expression does NOT throw an error. Optionally inspect the thrown error.
XCTAssertThrowsError(try parser.parse("invalid json")) { error in
XCTAssertEqual(error as? ParseError, .invalidInput)
}XCTAssertNoThrow
Fail if the expression DOES throw an error.
XCTAssertNoThrow(try parser.parse(validJSON))XCTUnwrap
XCTUnwrap unwraps an Optional and fails the test if it is nil — cleaner than guard let + XCTFail.
let user = try XCTUnwrap(service.currentUser)
XCTAssertEqual(user.name, "Bob")XCTAssertEqual with Accuracy
For floating-point comparisons, specify an accuracy value to account for precision.
XCTAssertEqual(result, 3.14159, accuracy: 0.0001)Custom Failure Messages
All XCTAssert functions accept an optional message parameter for clearer failure output.
XCTAssertEqual(items.count, 3, "Expected 3 items after insert, got \(items.count)")XCTExpectFailure
Mark expected failures so they are reported as successes in CI — useful for known but unfixed bugs.
XCTExpectFailure("Known issue: rdar://12345") {
XCTAssertEqual(brokenFunction(), expectedValue)
}Quick Check
Which XCTest function safely unwraps an Optional and fails the test if it is nil?
Lesson Recap
XCTest assertion family: XCTAssert/True/False, XCTAssertEqual/NotEqual, XCTAssertNil/NotNil, XCTAssertThrowsError/NoThrow, XCTUnwrap. Always add descriptive messages. Use XCTExpectFailure for tracked known failures.
Learn Swift with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 122
- Lessons
- 409
Frequently Asked Questions
Is the “XCTAssert Family and Throwing Assertions” lesson free?
Yes — any 3 lessons of the Swift Academy learning path, including “XCTAssert Family and Throwing Assertions”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Swift Academy course includes 4 lessons in total.
What will I learn in “XCTAssert Family and Throwing Assertions”?
Using XCTAssertEqual, XCTAssertThrowsError and XCTUnwrap safely. You practise Swift Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Swift Academy?
No prior experience is required. Swift Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.
How long does the “XCTAssert Family and Throwing Assertions” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Swift Academy lesson?
Yes. Every Swift Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- XCTestCase Setup, Teardown and Test Methods
- XCTAssert Family and Throwing Assertions
- Protocol-Based Mocking and Dependency Injection
- Async Tests and Performance Measurement