Julien Lengrand-Lambert bio photo

Julien Lengrand-Lambert

French guy living in the Netherlands. IT Chapter Lead @ING during the day, CoFounder @Skyai at night.

Twitter LinkedIn Github

Python/Unittest : assertRaises raises Error

Hi all,

Today, a small hint about unit tests in Python I discovered while working on Tippy.

In order to get as reliable code as possible, I am currently experiencing Agile techniques, and especially TDD. I develop Tippy in Python, and test methods with the excellent unittest framework.One of (in my mind at least) the most important tips Agile provides is the “fail fast” rule. And to fit with this rule, all my methods check their inputs before performing anything else.

It implies a lot of assertRaises assertions in my tests. Here is an example of how it could be used :

Let’s say I want to create a (dum) function calculating the square value of a number. I will have to test that the input can really be multiplied.

  • First, here is the corresponding (still dum :p) function in file dum_function.py :
def square_value(a):
    """
    Returns the square value of a.
    """
    try:
        out = a*a
    except TypeError:
        raise TypeError("Input should be a string:")

    return out
  • Here is the test to be performed (only this test is inserted):
import dum_function as df # import function module
import unittest
class Test(unittest.TestCase):
    """
    The class inherits from unittest
    """
    def setUp(self):
        """
        This method is called before each test
        """
        self.false_int = "A"

    def tearDown(self):
        """
        This method is called after each test
        """
        pass
    #---
    ## TESTS
    def test_square_value(self):
        # assertRaises(excClass, callableObj) prototype
        self.assertRaises(TypeError, df.square_value(self.false_int))

if __name__ == "__main__":
    unittest.main()
  • We are now ready to test our function! Here is what happens when trying to run the test :
======================================================================
ERROR: test_square_value (__main__.Test)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "test_dum_function.py", line 22, in test_square_value
    self.assertRaises(TypeError, df.square_value(self.false_int))
  File "/home/jlengrand/Desktop/function.py", line 8, in square_value
    raise TypeError("Input should be a string:")
TypeError: Input should be a string:

----------------------------------------------------------------------
Ran 1 test in 0.000s

FAILED (errors=1)

The TypeError is actullay raised, and generates a test failure. The problem is that this is exactly the behavior we wanted :s.

To avoid this error, simply run the function using lambda in the test call :

self.assertRaises(TypeError, lambda: df.square_value(self.false_int))

The final output :

----------------------------------------------------------------------
Ran 1 test in 0.000s

OK

Perfect !

Note : For the purpose of this article, I did not work “backwards” (created test before the function). I think this would have been counter productive for the effect I wanted to highlight.