Introduction

Without a comprehensive suite of automated tests, software becomes hard to change. This becomes a serious problem in multi-year, professional software projects. Thus software developer are expected to develop tests in parallel to the production code they are writing.

In this tutorial you will learn the very basics of this kind of test automatation.

Hello, World

When we have the following production code:

arithmetic.py
"""
See https://en.wikipedia.org/wiki/Multiplication
"""
def multiply(multiplicand, multiplier):
    return multiplicand * multiplier

"""
See https://en.wikipedia.org/wiki/Division_(mathematics)
"""
def divide(dividend, divisor):
    return round(dividend / divisor)

Then we can write test code for it like this, with the testing framework which is build-in into the Python language:

arithmetic.test.py
import unittest
from arithmetic import *

class ArithmeticTest(unittest.TestCase): (1)

    def test_multiplication(self): (2)
        self.assertEqual(multiply(2, 2), 4)

    def test_divsion(self):
        self.assertEqual(divide(10, 3), 3.3333333333333333)

if __name__ == '__main__':
    unittest.main()
1 The test class is derived from TestCase.
2 Each test method starts with the prefix test_.

The test file can then be executed as any other Python script. When a test fails, it will be reported to the programmer like this:

$ python3 example-project/arithmetic.test.py
F.
======================================================================
FAIL: test_divsion (__main__.ArithmeticTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "example-project/arithmetic.test.py", line 10, in test_divsion
    self.assertEqual(divide(10, 3), 3.3333333333333333)
AssertionError: 3 != 3.3333333333333335

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

FAILED (failures=1)

Assertion methods

For checking that the production code fulfills all the expected requirements, there are various assertion methods available. The following code demonstrates their usage.

assertions_examples.test.py
import unittest

class AssertExamplesTestTest(unittest.TestCase):

    """
    https://docs.python.org/3/library/unittest.html#unittest.TestCase.assertEqual
    """
    def test_compare_strings_expected_to_be_equal(self):
        self.assertEqual('abcde', 'abcde')

    """
    https://docs.python.org/3/library/unittest.html#unittest.TestCase.assertNotEqual
    """
    def test_compare_strings_expected_to_be_not_equal(self):
        self.assertNotEqual('abc', 'def')

    """
    https://docs.python.org/3/library/unittest.html#unittest.TestCase.assertTrue
    """
    def test_assert_true_boolean_expression(self):
        self.assertTrue(len("abcde") > 2)

    """
    https://docs.python.org/3/library/unittest.html#unittest.TestCase.assertFalse
    """
    def test_assert_false_boolean_expression(self):
        self.assertFalse(len("abcde") < 2)

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

Resources