unittest

TDD seems to be the fad these days and I can see why,  so now would be a good time to learn up on automated testing and CI. How TDD works is that you write the unit test, the test should fail when you run it and then you write the piece of code to make the test pass. It gets pretty annoying to debug the code after each deployment, save yourself the trouble and write tests for them. There are a lot of testing frameworks (some with a more simple syntax than unittest) out there for Python, but I'll be sticking to unittest.

Let's write tests for our addition module from the last post.

Create a new file called test_addition.py in the same folder as the addition module.

import unittest
import addition

class TestAddition(unittest.TestCase):

    def test_add2(self):
        self.assertEqual(addition.add2(2,3),5)

    def test_add3(self):
        self.assertEqual(addition.add3(1,2,3),6)

if __name__ == '__main__':
    unittest.main()

In the first two lines you need to import unittest  and the module that is to be tested.

To create test cases you first need to create a class extending unittest's TestCase class, usually it's one test class for each class.

Next write methods to test each function, the method name has to start with test so the test runner knows it's a test.

Here we use the assertEqual() to make sure that the value from the function we're testing is the expected one. There are other functions we can use such as assertTrue() or assertFalse() to check if the output of the function is correct.

We add the unittest.main() at the end to run all the tests in the file. To run the test we do run the following from the command line.

 python test_addition.py

If everything goes well then we should get something like

..
----------------------------------------------------------------------
Ran 2 tests in 0.000s

OK

There are other neat things you can do with unittest, more to read over here https://docs.python.org/3/library/unittest.html