Test Driven Development – Making the most out of your testing team
Dileep Marway, BrowserStack Champion - July 4, 2023
The most efficient testing teams are those where testing is shifted left and there is a close collaboration between engineering and their testing counterparts.
Based on my experience, Test-Driven Development (TDD) can make a massive difference to the quality of output from the engineering team.
What is TDD?
Test-Driven Development (TDD) is another key word which I have heard used by engineering leaders and also by some members in the c-suite.
It is a software development process where the engineer writes tests before writing the actual code. The tests are written to detail the desired behaviour of the code and the code is then written with the intention to pass the tests.
Why should you follow TDD?
Some Benefits of TDD Include:
- Better documentation – the tests serve as a form of documentation and this makes it easier for the whole team to understand how the code works via a test. This documentation can also be stored for future reference, thus if a new member of the team wants to understand how a code block works, the best way to do so is to understand the test.
- Finding defects is quicker – given that tests are written first it means that we can find defects quickly. Having basic forms of tests written also means that it forces the testing team to think outside the box and improve their testing approach.
- Cheaper to find defects – the earlier we find defects the cheaper it is.
- Fast feedback – Tests are written before the code, this provides early feedback and forces working behaviour.
- Increased code quality – Writing tests at the start pushes the developer to think about the functionality and structure of their code. This results in cleaner, more organised code.
Adding more tests earlier in the development lifecycle has been proven to have a positive effect on improving the quality of the software product output.
Challenges of TDD:
As with any new initiative there can be challenges. My advice would be to start small, sell the value and then take the whole team on the journey of change. There may be tweaks and changes that need to be made along the way.
- Initially it can feel like you are slowing down the process – the process of writing tests before writing code can feel slow and cumbersome. In time you will see the speed increase and the level of output quality go up.
- Stick with it and you will reap the rewards – TDD requires a lot of discipline and a change in approach to development.
- TDD can be challenging for complex systems or for developers who are not experienced in writing tests. Invest in the training, and pairing so that you can overcome this obstacle.
Getting started with TDD:
- Write a test:
Write a test for a small piece of the desired behaviour. - Run the test:
Run the test and it will fail, as the code is not there. - Write the code:
Write the minimum amount of code needed to make the test pass. - Refactor the code:
Refactor the code, making it cleaner and more effective, without changing the outcome. - Repeat:
Repeat the process until the behaviour has been fully tested and all the tests pass.
TDD code example
To begin writing tests in Python we will use the unit test module that comes with Python.
To do this we create a new file “mytests.py”, which will contain all of our tests.
Let’s begin with our friend “hello world”:
import unittest
from mycode import *
class MyFirstTests(unittest.TestCase):
def test_hello(self):
self.assertEqual(hello_world(), ‘hello world’)
Notice that we are importing helloworld() function from mycode file. In the file mycode.py we will initially just include the code below, which creates the function but doesn’t return anything at this stage:
def hello_world():
Running this will give the following message:
FAIL: test_hello (__main__.MyFirstTests)
Try two:
self.assertEqual(hello_world(), ‘hello world’)
AssertionError: None != ‘hello world’
——————————————————————–
Ran 1 test in 0.000s
FAILED (failures=1)
This clearly indicates that the test failed, which was expected.
To ensure the code passes, lets change mycode.py to the following:
def hello_world():
return ‘hello world’
Running python mytests.py again we get the following output in the command line:
Ran 1 test in 0.000s
We have now written our first test – well done!
Conclusion
TDD is a valuable software development process that can improve code quality, increase confidence in the code, and facilitate collaboration among team members.
TDD can be more powerful in agile environments, as it allows for rapid amendments and more frequent releases, especially when it is integrated into your CI/CD pipeline.
Though TDD is not a simple and easy fix to your quality problems. As with anything, it requires discipline and practice to be used effectively. Good luck!