Monday 10 April 2023

Test-driven development (TDD) in Python

Test-driven development (TDD) is a software development approach where tests are written before any code is implemented. The idea is to write tests that cover all possible use cases for a particular feature or functionality, and then implement the code to satisfy those tests. TDD is an iterative and incremental process, where the code is continuously refactored to improve its design and maintainability.

In this article, we will explore TDD in depth and demonstrate how to use TDD to write better software. We will be using Python and the unittest framework to demonstrate the TDD process.

The TDD Process

The TDD process consists of three main steps:

  1. Write a failing test case
  2. Write the code to make the test pass
  3. Rerun the tests

Let's walk through these steps in more detail.

Step 1: Write a failing test case

In TDD, we start by writing a test case that tests the behavior of a function or module. This test case should initially fail since the code to implement the behavior has not yet been written. For example, let's say we want to write a function that returns the discount on purchase amount. 

We could start by writing a test case that tests the def calculate_discount function for inputs Input same amount and calculate discount based on the amount and given discount rate in Python. 

 The discount rates are: 

 Amount            Discount 

 0-5000                5% 

 5000-15000       12% 

 15000-25000     20% 

 above 25000 30%

import unittest

def calculate_discount(amt):


class test_discount_calculator(unittest.TestCase): def test_for_1000(self): self.assertEqual(calculate_discount(1000), 50.0) def test_for_5000(self): self.assertEqual(calculate_discount(5000), 250.0) def test_for_10000(self): self.assertEqual(calculate_discount(10000), 1200.0) def test_for_15000(self): self.assertEqual(calculate_discount(15000), 1800.0) def test_for_25000(self): self.assertEqual(calculate_discount(25000), 5000.0) if __name__ == '__main__': unittest.main()

In this example, we have written a test case that tests the calculate_discount function for an input. However, the calculate_discount function has not yet been implemented, so this test case should fail.

Step 2: Write the code to make the test pass

Next, we write the code to make the test pass. In this example, we can implement the calculate_discount function as follows:

def calculate_discount(amt): disc=0 if(amt>0): if amt<=5000: disc = amt*0.05 elif amt<=15000: disc=amt*0.12 elif amt<=25000: disc=0.2 * amt else: disc=0.3 * amt return disc

This implementation should make the test_discount_calculator all the test cases pass.

Step 3: Rerun the tests

Finally, we rerun the tests to ensure that all the test cases pass. In this example, we can rerun the tests using unittest:

$ python -m unittest


Ran 5 test in 0.000s


The 5 test case defined for calculate_discount should now pass.

Advantages of TDD

TDD is widely used in the Python community, and has several advantages, including:

Faster feedback: By writing tests first, we get immediate feedback on whether our code is correct or not. This helps catch bugs earlier in the development cycle, when they are easier and less expensive to fix.

Increased code quality: TDD promotes writing small, modular, and testable code, which leads to higher code quality and easier maintenance. By focusing on writing tests, we are forced to think about the requirements and design of our code upfront, leading to more robust and reliable software.

Easier refactoring: Since we have a comprehensive suite of tests, we can refactor our code with confidence, knowing that we won't introduce new bugs. This makes it easier to improve the design of our code over time, and leads to more maintainable software.

Documentation: Tests serve as living documentation of our code, showing how it is intended to be used and what it is supposed to do. This makes it easier for developers to understand and work with the codebase, and reduces the risk of miscommunication or misunderstandings.

More efficient development: While writing tests may seem like an extra step, it can actually save time in the long run. By catching bugs earlier and reducing the need for manual testing, TDD can help developers work more efficiently and deliver software faster.

In addition to these advantages, TDD also promotes a culture of testing and quality, which can improve team collaboration and reduce the risk of defects and errors in the software.

Overall, TDD is a powerful technique for improving the quality and reliability of software, and has many advantages for Python developers. By writing tests first, we can catch bugs earlier, increase code quality, and work more efficiently, leading to better software and happier customers.


TDD is a powerful development process that can help to improve code quality, reduce debugging time, and make code easier to maintain. By following the three steps of TDD (write a failing test case, write the code to make the test pass, and rerun the tests), developers can ensure that their code is well tested and meets the requirements.

Please subscribe my youtube channel for latest python tutorials and this article

No comments:

Post a Comment