Test Driven Development Approach
Introduction
Let’s explore the test-driven development approach in this post. Essentially, Test Driven Development (TDD) is a development method where developers write a unit test first, and then create the code to fulfill the requirements of the test. The unit test serves as a specification for the system’s behavior. By writing the test first, developers can concentrate on writing code that passes the test. In other words, the developer writes unit tests before writing the actual code. This allows the programmer to run the tests immediately after writing them.
Approach
TDD involves writing automated tests of a software program’s 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 testing effort, reducing many tests to a click of a button.
Red-Green-Refactor Cycle:
TDD follows the Red, Green, Refactor cycle as shown in the below picture. Red refers to a failing test – the first test you write will not pass because you have not yet written any code. 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.
Red
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.
Green
To ensure success, it is vital to write the necessary code to make the test pass. Once the test has been passed, the task is complete. If additional functionality is required, a new test must be created. Focus on making one test pass at a time and move forward accordingly.
Refactor
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 unit tests to design application code. The application is better designed and maintained easily. By running the tests, the application developer gains confidence that the software is working.
TestNG Tutorials on this website can be found at:
https://www.testingdocs.com/testng-framework-tutorial/
For more details on the TestNG Framework, visit the official website of TestNG at: