Test-Driven Development Intro

by David Janzen


TDD Explained

Test-driven development (TDD) is a disciplined development approach that gained popularity with the eXtreme Programming agile method. TDD reverses the traditional programmers workflow. Traditionally programmers write code (e.g. methods and classes) then test the code manually or automatically with additional test code. With TDD, programmers write an automated test then write the code to make the test pass. The tests are small, usually testing only one method call, or the construction of an object. The tests are written with a unit testing framework such as JUnit. Once a test passes, the code might need to be improved (refactored) to make it as clean as possible.

TDD Misconceptions

TDD does not mean "write all the tests, then build a system that passes the tests." With TDD, you write one test, then write the code to make that one test pass. This should take no longer than about 15 or 20 minutes.

TDD with JUnit

Installation

Eclipse IDE: no install required. Open the JUnit test class in the Package Explorer and open the test class. Just add a JUnit test,right click on the test and choose Run As JUnit test.

JGrasp IDE

jCreator IDE

Text UI: Download JUnit 4.4. Add it to your class path (e.g. javac -cp junit-4.4.jar source.java), and run AllTests (see Hello World example below).

Hello World

See Hello World example for a very simple test, source, and test runner.