What is TDD (Test Driven Development)? | How to do TDD with Example

🚀 What is TDD?

  • Iterative development process.
  • Every iteration starts with a set of tests written for a new piece of functionality.
  • Test cases are created before code is written
  • TDD instructs developers to write new code only if an automated test has failed.

🚀 What are the main benefits of TDD?

  • Small Regression Suite
  • We are doing Test First, Reduction in Bugs
  • TDD is used to make the code clearer, simple and bug-free.
  • Avoids duplication of code
  • Refactoring improves the code
  • TDD drives the code design and approach
  • Unit test cases are covered early.

Example fo TDD

  • Create a Maven Project.
  • Add Testcases in Test class.
package com.thetestingacademy;import static com.thetestingacademy.App.isValidUserName;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import com.thetestingacademy.App.*;
import org.junit.Test;
/**
* Unit test for simple App.
*/
public class AppTest
{
/**
* Rigorous Test :-)
*/
@Test
public void shouldAnswerWithTrue() {
String userName = "PramodDutta";
String userName1 = "Pramod Dutta";
String userName2 = "Pramod@123";
String userName3 = "Pramod_Dutta";
String userName4 = "Pramod/Dutta";
String userName5 = "Pramod#Dutta";
assertTrue(isValidUserName(userName));
assertFalse(isValidUserName(userName1));
assertFalse(isValidUserName(userName2));
assertFalse(isValidUserName(userName3));
assertFalse(isValidUserName(userName4));
assertFalse(isValidUserName(userName5));
}
}
  • Now Execute it, It will fail
  • Fix them one by one
  • Repeat
package com.thetestingacademy;/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
System.out.println( "Hello World!" );
String userName = "PramodDutta";
String userName1 = "Pramod Dutta";
isValidUserName(userName);
// What is Valid UserName
}
static boolean isValidUserName(String name){
if(name.contains("/") || name.contains("#") || name.contains(" ") || name.contains("@") || name.contains("_")){
return false;
}
return true;
}
}

--

--

--

Lead Automation Expert | Blogger |Health Enthusiast https://www.youtube.com/c/thetestingacademy

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

UMBRELLA NETWORK: A COMMUNITY OWNED ORACLE

Releasing New Analytics Every Second

Lab 6: Capacitive Sensing with Piezo

Why you should read more blogs as a developer

Oracle DB Transactions — Lost Updates

What is a Charizard? An introduction to Inheritance, Abstract Classes and Interfaces.

Run Fn Project on your Raspberry Pi

Programming evolves, privilege reigns

A screenshot of a Commodore 64 program printing hello world.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Pramod Dutta

Pramod Dutta

Lead Automation Expert | Blogger |Health Enthusiast https://www.youtube.com/c/thetestingacademy

More from Medium

Best Practices for Version Controlling and NoSQL

The SOLID Principles

Am I testing my code or is my code testing me ?

Top 10 Automated Testing Software