Test-Driven Development

Testing is an important part of sotware development and engineering. There are many components of testing, and it needs to be considered at every phase of the software development life cycle (SDLC).

Test-Driven Development (TDD) is a software development approach in which tests are written before the code they are intended to validate.

The TDD process typically follows a cycle known as the "Red-Green-Refactor" cycle.

  1. Red (Write a Failing Test)
    In this phase, a developer writes a test that defines a new piece of functionality or improvement to the existing codebase. Since the code for the functionality doesn't exist yet, the test initially fails, represented by the test output being in a " red" state.
  2. Green (Write the Minimum Code to Pass the Test)
    The next step is to write the minimum amount of code necessary to make the test pass. The primary goal is to make the test go from a failing state (red) to a passing state (green). This often involves writing only enough code to meet the requirements of the test and nothing more.
  3. Refactor (Improve Code Without Changing Its Behavior)
    Once the test is passing, the developer can refactor the code to improve its structure, readability, and efficiency. The key is to make improvements without changing the external behavior of the code. Throughout this process, the existing tests act as a safety net, ensuring that any refactoring doesn't introduce new bugs.

This cycle is repeated iteratively, with new tests added for additional functionality or changes. The goal of TDD is to produce well-designed, maintainable code that meets the requirements and has a suite of tests validating its correctness.

Using JUnit Testing Framework

The linked video resource below provides a good introduction to creating Java projects in Eclipse, and using the JUnit test framework.

video highlights
time topic
0:05 Eclipse
0:15 Create a new Java project
0:25 Create a new package
0:27 Create a new Java class
0:56 Adding a simple method to the class
1:33 Add another method to the class
4:25 Create a new JUnit test case
6:10 assertEquals()
6:44 Run the test(s)
7:00 Demonstrate a failed test
7:15 Add another test case
9:15 Combine test cases into a new test suite

 

top