free-tech

What is Test Driven Development(TDD)?


Test-Driven Development starts with designing and developing tests for every small functionality of an application. TDD framework instructs developers to write new code only if an automated test has failed. This avoids duplication of code. The TDD full form is Test-driven development.
What is Test Driven Development(TDD)?
The simple concept of TDD is to write and correct the failed tests before writing new code (before development). This helps to avoid duplication of code as we write a small amount of code at a time in order to pass tests. (Tests are nothing but requirement conditions that we need to test to fulfill them).
Test-Driven development is a process of developing and running automated test before actual development of the application. Hence, TDD sometimes also called as Test First Development.
How to perform TDD Test

Following steps define how to perform TDD test,
  1. Add a test.
  2. Run all tests and see if any new test fails.
  3. Write some code.
  4. Run tests and Refactor code.
  5. Repeat.
What is Test Driven Development(TDD)?

TDD cycle defines
  1. Write a test
  2. Make it run.
  3. Change the code to make it right i.e. Refactor.
  4. Repeat process.
Some clarifications about TDD:
TDD Vs. Traditional Testing

Below is the main difference between Test driven development and traditional testing:
TDD approach is primarily a specification technique. It ensures that your source code is thoroughly tested at confirmatory level.

What is acceptance TDD and Developer TDD

There are two levels of TDD
  1. Acceptance TDD (ATDD): With ATDD you write a single acceptance test. This test fulfills the requirement of the specification or satisfies the behavior of the system. After that write just enough production/functionality code to fulfill that acceptance test. Acceptance test focuses on the overall behavior of the system. ATDD also was known as Behavioral Driven Development (BDD).
  2. Developer TDD: With Developer TDD you write single developer test i.e. unit test and then just enough production code to fulfill that test. The unit test focuses on every small functionality of the system. Developer TDD is simply called as TDD.The main goal of ATDD and TDD is to specify detailed, executable requirements for your solution on a just in time (JIT) basis. JIT means taking only those requirements in consideration that are needed in the system. So increase efficiency.
What is Test Driven Development(TDD)?
Scaling TDD via Agile Model Driven Development (AMDD)

TDD is very good at detailed specification and validation. It fails at thinking through bigger issues such as overall design, use of the system, or UI. AMDD addresses the Agile scaling issues that TDD does not.
Thus AMDD used for bigger issues.
The lifecycle of AMDD

What is Test Driven Development(TDD)?
In Model-driven Development (MDD), extensive models are created before the source code is written. Which in turn have an agile approach?
In above figure, each box represents a development activity.
Envisioning is one of the TDD process of predicting/imagining tests which will be performed during the first week of the project. The main goal of envisioning is to identify the scope of the system and architecture of the system. High-level requirements and architecture modeling is done for successful envisioning.
It is the process where not a detailed specification of software/system is done but exploring the requirements of software/system which defines the overall strategy of the project.

Iteration 0: Envisioning

There are two main sub-activates.
  1. Initial requirements envisioning.It may take several days to identify high-level requirements and scope of the system. The main focus is to explore usage model, Initial domain model, and user interface model (UI).
  2. Initial Architectural envisioning. It also takes several days to identify architecture of the system. It allows setting technical directions for the project. The main focus is to explore technology diagrams, User Interface (UI) flow, domain models, and Change cases.

Iteration modeling

Here team must plan the work that will be done for each iteration.

Model storming

This is also known as Just in time Modeling.

Test Driven Development (TDD)


Reviews

Test Driven Development (TDD) Vs. Agile Model Driven Development (AMDD)

TDDAMDD
TDD shortens the programming feedback loop | AMDD shortens modeling feedback loop.
TDD is detailed specification | AMDD works for bigger issues
TDD promotes the development of high-quality code | AMDD promotes high-quality communication with stakeholders and developers.
TDD speaks to programmers | AMDD talks to Business Analyst, stakeholders, and data professionals.
TDD non-visually oriented | AMDD visually oriented
TDD has limited scope to software works | AMDD has a broad scope including stakeholders. It involves working towards a common understanding
Both support evolutionary development | ——————————————–
Now, let’s learn Test Driven Development by example.
Example of TDD

Here in this Test Driven Development example, we will define a class password. For this class, we will try to satisfy following conditions.
A condition for Password acceptance:
First in this TDD example, we write the code that fulfills all the above requirements.
What is Test Driven Development(TDD)?
Scenario 1: To run the test, we create class PasswordValidator ();
What is Test Driven Development(TDD)?
We will run above class TestPassword ();
Output is PASSED as shown below;
Output:
What is Test Driven Development(TDD)?
Scenario 2: Here we can see in method TestPasswordLength () there is no need of creating an instance of class PasswordValidator. Instance means creating an object of class to refer the members (variables/methods) of that class.
What is Test Driven Development(TDD)?
We will remove class PasswordValidator pv = new PasswordValidator () from the code. We can call the isValid () method directly by PasswordValidator. IsValid (“Abc123”). (See image below)
So we Refactor (change code) as below:
What is Test Driven Development(TDD)?
Scenario 3: After refactoring the output shows failed status (see image below) this is because we have removed the instance. So there is no reference to non –static method isValid ().
What is Test Driven Development(TDD)?
So we need to change this method by adding “static” word before Boolean as public static boolean isValid (String password). Refactoring Class PasswordValidator () to remove above error to pass the test.
What is Test Driven Development(TDD)?
Output:
After making changes to class PassValidator () if we run the test then the output will be PASSED as shown below.
What is Test Driven Development(TDD)?
Advantages of TDD

Following are the main advantages of Test Driven Development in Software Engineering:
Summary: