Python Test Driven Development
Test driven development is a software development practice known to be rediscovered by an American software engineer named Kent Beck. Its a software development practice where much importance is given to testing during the development phase.
If you are not familiar with the concepts of unit testing and coverage, it is a good idea to revisit my previous articles on those topics before further reading..
Before we dive in to the details of TDD, lets look at some facts that will motivate us to take TDD seriously.
‘We do TDD’ is a community for TDD practitioners and companies that promote TDD to their teams and employees. You can get reliable data about which companies are promoting TDD, the facts on this platform are a result of direct interviews with people in leading positions in those companies, thanks to the efforts of the founders in bringing up the entire TDD community and resources to one place. In addition to that, the historic first International Conference on TDD was held around mid 2021. In spite of usual controversies that rise up for new ideologies, TDD is slowly getting the attention of many developers and companies, so its worth investing some of your time to get started with TDD.
Its now time for us to look into what TDD really is. TDD basically has just one main rule which says “ Not a single piece of code must be written to solve a problem before the test case is written and in place”. Below is a clear demonstration of the steps involved in TDD.
The very first step of TDD is to write a test case, so remember that you are not allowed to write a single piece of code unless you have a test case in place. Once you have written the test case, obviously your test would fail since you do not have the solution yet. Now is the time to write your solution, make the test case pass and the do some refactoring. One cycle of TDD is completed. You can go for more iterations if you need to update your test cases.
Lets look at a simple problem to understand the process of TDD clearly.
Suppose you are required to write a function that takes in two numbers and returns the sum depending on their signs.
Get Navaratnarajah Suman’s stories in your inbox
Join Medium for free to get updates from this writer.
So once you are assigned with the task you should analyze the problem and follow the steps below to solve it in a TDD based approach.
STEP 1: ______ Write a failing test case______
The third test case is intentionally made wrong for demonstration purposes, the expected value is supposed to be ‘1’ not ‘-1’
If you not familiar with writing unit tests using pytest, I would recommend to revisit my previous article Unit testing with pytest.
STEP 2: ______ Write a Solution ______
Once you have the test case in place, you can go ahead and code the solution, you can write the code confidently and faster because you already have a test case in place which will give you the opportunity to get immediate feedback.
Writing the function is upto you, the goal is to make the test case pass.
STEP 3: ______ Refactor and continue ______
After the test passes you may refactor and continue iterations until you are satisfied, you may update the test cases and make the test stronger and stronger at each iteration, similarly at each iteration the solution can be made more and more robust.
For the above solution the test summary would be as follows.
As expected the third test case fails, so in the next iteration you can update the test and run the test again and make sure all test cases pass.
Also remember while iterating you can take into consideration the coverage as well, just making the tests pass is not sufficient, you must make sure that the code and tests are both optimized and no unwanted tests and no unwanted pieces of code exists, these things can be handled in the refactoring stage.
This concludes the introduction to test driven development in python. Please comment if you have any questions/discussions..







