TDD Framework

Introduction:

The growth of Agile development has proposed many pragmatic practices for delivering quality software at high speed. Test-Driven Development (TDD) is one such practice that is now classical as an accomplished process that drives positive results.

What is TDD: 

Test Driven Development (TDD) is a software development process that focuses on creating unit test cases before developing the actual code. It is an iterative process that altogether connects programming, the creation of unit tests, and refactoring.

TDD is an iterative development process. In TDD, developers write enough tests before they write production code to

write a test before they write just enough production code to satisfy that test and the subsequent refactoring. Developers use the specifications and they will first write a test describing how the code actually should behave.

How to perform TDD Test?

The following steps define how to perform the TDD test,

1.      Add a test.

2.      Run all tests and check the result and if there is any failure

3.      Write some code.

4.      Run tests and Refactor code.

5.      Repeat.

Perform TDD Test

TDD cycle defines

1. Write a test

2. Make it run.

3. if fails then make changes i.e., Refactor.

4. Repeat process

TDD Vs. Traditional Testing:

  • In the traditional testing approach, a successful test run finds one or more defects ensuring that our system actually meets the requirements which are defined for it. It helps to build confidence in your system.
  • In TDD our main focus is on production code which verifies whether testing will work properly. In traditional testing, our main focus is on test case design. Whether the test will show the proper/improper execution of the application in order to fulfill the need.
  • TDD achieved 100% test coverage. each code of line is tested, unlike traditional testing.
  • The combination of traditional testing and Test-Driven Development leads to the importance of testing the system rather than the perfection of the system.

What is acceptance TDD and Developer TDD:

Acceptance TDD:

With Acceptance TDD you write a single acceptance test. here the test will fulfill the requirement of the specification. After that write just adequate production/functionality code to accomplish that acceptance test. The acceptance test will mainly focus on the overall behavior of the system. Acceptance Test Driven Development also known as Behavioral Driven Development (BDD).

Developer TDD:

With Developer TDD we write a single developer test which can be known as a unit test and then just enough production code to fulfill that test. The unit test will cover each and every functionality of the system. Developer TDD is simply known as TDD. The main objective of ATDD and TDD is to specify detailed, executable requirements for your solution on a JIT (just in time) basis. 

Developer TDD

Example of TDD:

Here in this Test Driven Development example, we will define a class Username. For this class, we will try to satisfy the following conditions.

A condition for Username acceptance:

  • The Username should be between 5 to 10 characters.

First, in this Test Driven Development example, we write the code that fulfills all the above-specified requirements.

example

Scenario 1: To run the test, we create class UserName();

tdd

We will run above class UserNameTest ;

Output is PASSED as shown below;

Scenario 2 : Here we can see in method UserNameTest() there is no need of creating an object of class username.so we remove that. 

tdd

We will remove UserName userName=new UserName() from UserName class ;  We can call the isValid () method directly by UserName. IsValid (“Deepali”).

But we are unable to do it because we cannot call a non-static method without creating an object of the class. To call this method we have to Refactor the code as below:

Here we have added a static keyword before a method name then call the UserNameTest class and check the result.

tdd

But we are unable to do it because we cannot call a non-static method without creating an object of the class. To call this method we have to Refactor the code as below:

Here we have added a static keyword before a method name then call the UserNameTest class and check the result.

tdd 8
tdd 5
tdd 6

below we can check the result after refactoring the code:

9 tdd

After making changes to class UseName() if we run the test then the output will be PASSED as shown above

Advantages of TDD:

  •          TDD emboldened the development of easy, clean, and extensile code. The regimentation of following TDD would naturally develop habits that lead to better code as part of developers’ everyday practice.

  •     If you refactor the code, there can be a probability of breaks in the code. So having a set of automated tests you can correct those breaks before release. A Proper alert will be given if breaks are found when automated tests are used.

  • Using Test Driven Development will result in faster, more extendible code with a small number of bugs that can be updated with minimal risks.

  • In TDD, In the absence of any team member, other team members can easily pick up and work on the code. It also adds knowledge sharing, thus making the team more influential overall.

Conclusion:

Test Driven Development’s test-first approach also helps reduce delicate bottlenecks that clog the quality and delivery of software. Based on the constant feedback, bug fixes, and enhancement of new features, the system evolves to ensure that everything works as intended. Test Driven Development speeds up collaboration between team members from both the development and QA teams as well as with the client. Additionally, as the tests are created in advance, teams don’t need to spend time recreating comprehensive test scripts.

Related Post

Leave a Comment

© Copyright 2024 QACraft Pvt. Ltd. All rights reserved.

Contact : +91 9157786796