In this post, we will discuss test-driven development approach. First of all, Test Driven Development (TDD) is a development methodology where developers create software by first writing a unit test, then writing the actual code to make the unit test pass. Also, the unit test can be viewed as a specification on how the system should behave. Writing it first, helps the developer to focus on writing code to make the test pass. In simple terms, the developer writes the unit tests prior to the code under test. As a result, the programmer can immediately execute the tests after they are written.
TDD involves writing automated tests of a software program’s individual units. A unit is the smallest possible testable software component. Developers implement drivers and stubs to support the execution of the unit tests. A driver is a piece of code, which calls other functions. A stub is a prototype skeleton of a method without any code.
Automated testing involves writing unit tests as code and placing this code in a test harness or framework such as JUnit. Automated unit testing frameworks minimize the effort of testing, reducing a large number of tests to a click of a button.
TDD follows Red, Green, Refactor cycle as shown in the below picture. Red refers to a failing test – the test you write first will not pass because you have not yet written any code for it. Green refers to writing just enough code to make your unit test pass. The Test runner’s UI ( JUnit/ TestNG ) will now show that test passing with a green icon. Refactor refers to refactoring the code so it is tighter, cleaner, and more flexible. This cycle is repeated constantly throughout.
Create a test and make it fail. Create the new code stub. Write just enough code so that it compiles. Run the test. It should fail. This is a meaningful failure, and we expect it to fail.
Make the test pass. Write the code to make the test pass. If the written code makes the test pass as intended, we are done. If new functionality is needed, then another test is needed. Make this one test pass and continue.
Change the code to remove duplication in your project and to improve the design while ensuring that all tests still pass. After refactoring, rerun all the tests to ensure that they all pass. Repeat the cycle.
Test-driven development uses the unit tests to design application code. The application is better designed and maintained easily. The application developer gains confidence that the software is working, by running the tests.
TestNG Tutorials on this website can be found at:
For more details on the TestNG Framework, visit the official website of TestNG at: