Activities

Now it’s time for you to put this chapter into action. Read through this list, pick one of the activities, and do it. It’s only in doing that we actually learn.

  1. Read Apple’s documentation of test assertions so you know what your other options are.

  2. Is there any production code you can begin testing today? Look for low-hanging fruit—functions that use only their input arguments to calculate a return value. This includes failable initializers: write one test checking for a nil return value and another for non-nil. (Pro tip: Any time you add a new test, make sure you see it fail by temporarily breaking the production code.)

  3. If your code already has some unit tests, then do the following:

    1. Read through the tests you have.

    2. Select a simulator, and press -U to run your own tests. Make sure they all pass. If there are any test failures, delete those tests.

    3. Is each test using the best assertion for the job? Improve any you can.

    4. Check calls to XCTAssertEqual to see if the argument order is consistent. Try to stick to a consistent order for actual/expected.

    5. Look for any XCTAssertEqual assertions that compare floating-point numbers. Do they use the accuracy parameter? Add any that are missing.

    6. Consider whether there are any optional assertion messages you can delete because they’re redundant.

    7. Add descriptive messages to any XCTFail assertions that are missing them.

    8. If you’ve changed any assertions or messages, make sure their failure output is helpful. You can check this by introducing temporary errors in either the test code or the production code. Afterward, don’t forget to remove these errors, then run the tests to make sure they pass.