TDD - test-driven development
In OO unit testing TDD-style, test code is written before the class to be tested, and the developer writes unit testing code for nearly all production code. The basic rhythm is to write a little test code, then write a little production code, make it pass the test, then write some more test code, and so forth. Key Point: The test is written first, imagining the code to be tested is written. Advantages include: The unit tests actually get written Human (or at least programmer) nature is such that avoidance of writing unit tests is very common, if left as an afterthought. Programmer satisfaction leading to more consistent test writing This is more important than it sounds for sustainable, enjoyable testing work. If, following the traditional style, a developer first writes the production code, informally debugs it, and then as an afterthought is expected to add unit tests, it doesn't feel satisfying. This is test-last development, also known as Just-this-one-time-I'll-skip-writing-the-test development. It's human psychology. However, if the test is written first, we feel a worthwhile challenge and question in front of us: Can I write code to pass this test? And then, after the code is cut to pass the tests, there is some feeling of accomplishmentmeeting a goal. And a very useful goalan executable, repeatable test. The psychological aspects of development can't be ignoredprogramming is a human endeavor. Clarification of detailed interface and behavior This sounds subtle, but it turns out in practice to be a major value of TDD. Consider your state of mind if you write the test for an object first: As you write the test code, you must imagine that the object code exists. For example, if in your test code you write sale.makeLineItem(description, 3) to test the makeLineItem method (which doesn't exist yet), you must think through the details of the public view of the methodits name, return value, parameters, and behavior. That reflection improves or clarifies the detailed design. Provable, repeatable, automated verification Obviously, having hundreds or thousands of unit tests that build up over the weeks provides some meaningful verification of correctness. And because they can be run automatically, it's easy. Over time, as the test base builds from 10 tests to 50 tests to 500 tests, the early, more painful investment in writing tests starts to really feel like it's paying off as the size of the application grows. The confidence to change things In TDD, there will eventually be hundreds or thousands of unit tests, and a unit test class for each production class. When a developer needs to change existing codewritten by themselves or othersthere is a unit test suite that can be run[1] , providing immediate feedback if the change caused an error. Applying UML and Patterns: An Introduction to Object-Oriented Analysis and Design and Iterative Development, Third Edition By Craig Larman