TEST DRIVEN DEVELOPMENT
Test-Driven Development (TDD)
Test-driven development (TDD) is a software development approach that emphasizes writing tests before writing the actual code. This technique helps developers create software that's more reliable, maintainable, and better aligned with the desired functionality. In TDD, the development process follows a cycle: write a test, make it pass, and then refactor as needed.
The TDD Cycle:
Write a Test: In TDD, you begin by writing a test that defines a specific code's desired behaviour. These tests are typically short, focused, and describe what the code is supposed to do.
Run the Test: When you run the Test, it should fail because you still need to implement the functionality. This "red" status indicates that the code doesn't yet meet the requirements.
Write the Code: It's time to implement the code that passes the test. This means you're creating the minimum code required to satisfy the Test.
Run the Test Again: After writing the code, you re-run the Test. If it passes, it turns "green," which means your code successfully meets the Test's criteria.
Refactor (If Needed): If the code works as expected, you can refactor it to make it cleaner, more efficient, or more maintainable. You do this while ensuring that the Test continues to pass.
Let's see a code as an example to demonstrate TDD
Write a Test: We will write test cases in your TestCalc class. These tests define the expected behaviour of the performSub function in the source file. The tests check if the subtraction function works correctly in different scenarios.
# test.py
import unittest
import source
class TestCalc(unittest.TestCase):
def testSub1(self):
self.assertEqual(1, source.performSub(2, 1), "Bug in implementation. Results should be 1.")
def testSub2(self):
self.assertEqual(10, source.performSub(20, 10), "Bug in implementation. Results should be 1.")
def testDivZero(self):
self.assertEqual("Put in correct divisor", source.performSub(20, 0), "Bug in implementation.")
if __name__ == '__main__':
unittest.main()
Run the Test: When you execute these tests, they will fail because the performSub function hasn't been implemented yet. This is the "red" phase of TDD.
Write the Code: Now, you implement the performSub function in your source file. Your goal is to make the tests pass. For example:
#source.py
def performSub(x, y):
return x - y
4. Run the Test Again: Re-run your test suite. If you've correctly implemented the performSub function, the tests should pass. The "green" phase indicates that your code meets the specified requirements.
5. Refactor (If Needed): If your code is clean and the tests pass, you might consider refactoring to improve readability or performance.
Following the TDD process ensures that your code functions as expected, even as you make changes and enhancements over time. It also provides documentation in the form of tests, which helps you and others understand the code's intended behavior. TDD is a powerful approach for building reliable and maintainable software.