Write Your First Assertion

Now that we have a home for tests, let’s go over how to use the testing mechanism. How does a test communicate success or failure? What does Xcode show you when a test fails? What does it show when a test succeeds?

The way a test reports a failure to XCTest is through assertions. Let’s start with the simplest assertion. Add the following method to the AssertYourselfTests class:

 func​ ​test_fail​() {
 XCTFail​()
 }

First, what makes this function a test?

  • It lives within a subclass of XCTestCase.
  • It isn’t declared private.
  • Its name starts with test.
  • It takes no parameters.
  • It has no return value.

Why the underscore in the test name? This goes ...

Get iOS Unit Testing by Example now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.