top of page
hand-businesswoman-touching-hand-artificial-intelligence-meaning-technology-connection-go-

TDD (Test Driven Development) in Python

As a apart from technical education and professional background, still I am trying to write my fourth blog on Test Driven Development (TDD). We will come to understand in the end of article i.e What is TDD, Why is the TDD important, and how does the TDD work in Python with unittest module framework.

Introduction Here before introduce the TDD, I want to give an example for shopping clothes. we try the outfit or dress first in fitting room for how does the dress look, does it fit perfectly or not before buy a dress.

Same in Software Development, We run the each unit test cases according to our test plan to write before the actual code to confirmation of functionality the actual code. ‘’Test Driven Development is an approach in which we build a test first, then fail the test and finally refactor our code to pass the test.’’

Importance

We can lower the risk of upcoming significant problems at production level with TDD approach. Tests also gives us confidence as we catch the bugs when we begin to refactor code during test execution.

How Does the TDD Work There is following workflow of TDD which repeats from refactor to Write the failing test.: 1.Write Failing Test 2.Run and Fail Test 3.Write Code to pass 4.Run and Pass the test 5.Refactor

Unittest Module as TDD in Python A unittest is a module in python which provides multiple tools for constructing and running the tests. Following are the main methods which we use with python unit testing:

Method : Checks That assertEqual(a,b) : a == b assertNotEqual(a,b) : a != b assertTrue(x) : bool(x) is True assertFalse(x) : bool(x) is False assertIs(a,b) : a is b assertIsNot(a,b) : a is not b assertIsNone(x) : x is None assertIsNotNone(x) : x is not None assertIn(a,b) : a in b assertNotIn(a,b) : a not in b assertIsInstance(a,b) : isinstance(a, b) assertnotIsInstance(a,b) : not isinstance(a, b)

Here I am creating two files i.e. testfile.py and calculator.py.

testfile.py : It is a python file in which we run the test cases with unittest module. In testfile we are importing the unittest module from django library and Calculator class from calculator.py. Here we are defining the CalculatorTest as a subclass of unittest.TestCase. We define four test cases i.e. test_add, test_minus, test_multiple and test_divide.

calculator.py : It is a python file in which we have our actual code of functionality of calculator.


Code

testfile.py:


import unittest from calculator import Calculator class CalculatorTest(unittest.TestCase): calculator = Calculator() def test_add(self): self.assertEqual(4, self.calculator.add(2,2)) self.assertEqual(3.2, self.calculator.add(1,2.2)) def test_minus(self): self.assertEqual(2, self.calculator.minus(3,1)) self.assertEqual(-2, self.calculator.minus(1,3)) def test_multiple(self): self.assertEqual(15, self.calculator.multiple(3,4)) self.assertEqual(13.5, self.calculator.multiple(3,4.5)) def test_divide(self): self.assertEqual(3, self.calculator.divide(9,3)) if __name__ == "__main__": unittest.main()


calculator.py:

class Calculator: def __init__(self): pass def add(self, x, y): return x + y def minus(self, x, y): return x - y def multiple(self, x, y): return x * y def divide(self, x, y): return x/y


Output: C:\Users\PycharmProjects\untitled\py37venv\Scripts\python.exe “C:\Program Files\JetBrains\PyCharm Community Edition 2019.3.3\plugins\python-ce\helpers\pycharm\_jb_unittest_runner.py” — target testfile.CalculatorTest Launching unittests with arguments python -m unittest testfile.CalculatorTest in C:\Users\PycharmProjects\untitled\unittest-calc Ran 4 tests in 0.019s FAILED (failures=1) 12 != 15 Expected :15 Actual :12

Note: When I run the testfile.py in Django, an error came with one test case failed after all test cases run. As we see in testfile.py code, In Multiple Test Case we failed the testcase i.e. 15 of multiply of two number 4 and 3. Now We are going to pass the test case in multiply test case to run the test.

import unittest from calculator import Calculator class CalculatorTest(unittest.TestCase): calculator = Calculator() def test_add(self): self.assertEqual(4, self.calculator.add(2,2)) self.assertEqual(3.2, self.calculator.add(1,2.2)) def test_minus(self): self.assertEqual(2, self.calculator.minus(3,1)) self.assertEqual(-2, self.calculator.minus(1,3)) def test_multiple(self): self.assertEqual(12, self.calculator.multiple(3,4)) self.assertEqual(13.5, self.calculator.multiple(3,4.5)) def test_divide(self): self.assertEqual(3, self.calculator.divide(9,3)) if __name__ == "__main__": unittest.main()


After run the testfile.py, our output is as follows:

Output: Testing started at 4:06 PM … C:\Users\PycharmProjects\untitled\py37venv\Scripts\python.exe “C:\Program Files\JetBrains\PyCharm Community Edition 2019.3.3\plugins\python-ce\helpers\pycharm\_jb_unittest_runner.py” — target testfile.CalculatorTest Launching unittests with arguments python -m unittest testfile.CalculatorTest in C:\Users\PycharmProjects\untitled\unittest-calc Ran 4 tests in 0.005s OK Process finished with exit code 0

As we see in above code, first we write a fail test case and then run the fail test case, then we pass the testcase and then we run the pass the test case. This is a workflow of TDD.

 Conclusion:

I hope, This article will help you to understand the Test Driven Development (TDD) in python Django. For given code, please visit my GitHub.



277 views0 comments

+1 (302) 200-8320

NumPy_Ninja_Logo (1).png

Numpy Ninja Inc. 8 The Grn Ste A Dover, DE 19901

© Copyright 2022 by NumPy Ninja

  • Twitter
  • LinkedIn
bottom of page