Been doing a little website updating work lately and not much iOS development, but getting ready to start my next project. I wanted to setup and do test-driven development when I started on ScoutTrail, but I didn’t really understand Apple’s suggestions. Then I ran into Saul Mora’s chapter in More iPhone Cool Projects, “Write Better Code and Save Time with Unit Testing”.
Why Unit Test?
The goal of unit testing is to prove that your smallest software components, or units, work correctly in isolation. Units are generally the individual functions or methods of a software class. There are several benefits:
Facilitates Change - The unit test establishes the expected behavior for the unit. If the developer has a future need to modify the unit, the change must still pass the test to prove that the expected result has not changed.
Simplified Integration - Proving that the units work correctly first eases the integration effort of the units into the completed software app.
Documentation - Unit testing provides a “living document” of the system. Most coders, especially this one, dislike coding then documenting the code, modifying the code then fixing the documentation to match the modifications, and so on. Unit testing establishes the API and shows the functionality of the unit.
Design - The unit tests are like a design document in that they specify the classes, methods and behavior. One advantage that test-driven development as design has over diagram-based design, such as UML, is in verifying adherence to design. The unit-tests themselves verify implementation adheres to design.
Since a class may use or reference other classes, testing a single unit can end up testing other units because of this coupling. To reduce or prevent close-coupling during unit-testing, items such as mock objects or method stubs are used. By design, a unit-test should not leave its own class boundary. When it does, determining cause of failure becomes more difficult.
I’ll be starting out CubPath with Saul’s techniques and suggestions, and I’ll keep you posted along the way.
