0Pricing
Swift Academy · Lesson

XCTAssert Family and Throwing Assertions

Using XCTAssertEqual, XCTAssertThrowsError and XCTUnwrap safely.

XCTAssert Family and Throwing Assertions is a free Swift Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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.

Frequently asked questions

Is the “XCTAssert Family and Throwing Assertions” lesson free?

Yes — the full text of “XCTAssert Family and Throwing Assertions” is free to read here on the web, and the Swift Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Swift Academy course, upgrade to CoddyKit PRO.

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; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

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

  1. XCTestCase Setup, Teardown and Test Methods
  2. XCTAssert Family and Throwing Assertions
  3. Protocol-Based Mocking and Dependency Injection
  4. Async Tests and Performance Measurement
← Back to Swift Academy