Chapter 17. Unit Testing

Unit tests are instrumental in maintaining the integrity of your code. Unit testing is a way of validating the runtime behavior of your code at build time. In addition to writing your application's classes, you also write one or more unit tests that exercise those classes and verify that they perform as expected. These tests are not part of your application and live in a separate unit test bundle. You can elect to have these tests performed on your code whenever you build your product, insuring not only that your code compiles and links but that it behaves correctly as well.

Unit testing is a fundamental part of Test Driven Development (TDD), a development philosophy popularized by the Extreme Programming movement. In TDD, you develop the test for your function or class first; and then write your function to meet the expectations of the test. In essence, this is what rigorous designers have done for years. They first develop a detailed specification of exactly what a function should do, write the function, and then verify that the function behaves as designed. The quantum leap provided by unit tests is that the "specification" is now an automated test that verifies the design goals of the code rather than a paper description that must be interpreted and verified by the programmer or quality assurance engineer. Because the test is automated, it can be run every time the application is built, ensuring that every function still conforms to its specification ...

Get Beginning Xcode® 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.