Introduction to unit testing in Python
There are multiple ways to run tests in Python. One, as we have seen above, a bit crude, is to execute code with multiple asserts. A common one is the standard library unittest
.
Python unittest
unittest
is a module included in the Python standard library. It is based on the concept of creating a testing class that groups several testing methods. Let's write a new file with the tests written in the proper format, called test_unittest_example.py
.
import unittest
from tdd_example import parameter_tdd
class TestTDDExample(unittest.TestCase):
def test_negative(self):
self.assertEqual(parameter_tdd(-1), 0)
def test_zero(self):
self.assertEqual(parameter_tdd(0), 0)
def test_five(self):
self.assertEqual(parameter_tdd(5), 25)
def test_seven(self):
# Note this test is incorrect
self.assertEqual(parameter_tdd(7), 0)
def test_ten(self):
self.assertEqual(parameter_tdd(10...