logo
logo
AI Products 

What is Test Driven Development (TDD)? Tutorial with Example

avatar
Naveen Singh
What is Test Driven Development (TDD)? Tutorial with Example

One of the principles from a manifesto for agile software development states that – Continuous attention to technical excellence and good design enhances agility.


But how do we focus on technical excellence? Well, for that we need to explore a bunch of good and useful technical practices. One such practice is Test Driven Development or TDD.


In this article, I would walk you through a simple example of creating a Test-Driven code and elaborate a bit about the practice and associated advantages.


What is TDD?

TDD or Test-Driven Development is a common practice for developing code that is simple, maintainable, and well tested. The approach states that one should write “implementation code” only if there is a “failing test case”. It is an iterative approach for developing software products where –

  1. A failing test case is written
  2. Enough business code is created which makes the failing test case pass
  3. Then, if needed, the entire code is refactored.
  4. Finally, the entire process is repeated, creating more tests over a period of time.

iterative approach for developing software


Things to consider when creating tests:

  1. Only one assert per test.
  2. The test should be fast.
  3. It can be run independently.
  4. It can be repeated.
  5. It should be self-validating.

What are the Advantages of TDD

1. Faster feedback on the quality of the code.

2. Faster detection of defects.

3. Improved quality of code.

4. Code addition is incremental, thus supports emergent design and architecture.

5. Supports smaller and iterative changes to code, thus making it less error-prone.

6. Makes the implementation of new code easier.

7. Helps to ensure that previous code is not broken due to new changes.

8. Creates a habit of refactoring.

9. Support clean and maintainable code.

10. Lays the foundation for automation.


What are the Common Pitfalls While Doing TDD

1. Writing too many test cases at once.

2. Focus only on a happy path and code coverage.

3. Not thinking about different possible scenarios.

4. Too many assertions in one test case.

5. Testing too many things in a single test case.

6. Writing trivial test cases to improve code coverage.

7. Not executing the test suite often.

8. Not following the steps regularly. Sometimes, just implementing the code without writing the tests first.

9. Using asserts without actually testing anything.

10. Creating test cases that aren’t maintainable.


A TDD Example

Let us consider a user profile management system that allows users to create a username and password for future authentication. There are certain business rules which the username and password should follow to be valid.

For the sake of simplicity and brevity of the article, I will work with only one rule for the username and that would be to validate the minimum length of the username. Let us say that the minimum length for a username is 8 characters.


Now we will start developing our tests and code accordingly. I will be using Laravel and PHPUnit for the code examples.


To view the full blog and this blog "What is TDD?" is originally published on Agilemania website.


collect
0
avatar
Naveen Singh
guide
Zupyak is the world’s largest content marketing community, with over 400 000 members and 3 million articles. Explore and get your content discovered.
Read more