In this article we will look at the various unittest methods for creating assertions. This will not teach you the basics of unittest
like how to create a test class, you can check that in the link below:
In testing we use assertions to verify that the outcomes of a given test are true. For example, we can use assertion to verify that a value yielded by an operation is equal to a known value. If the outcome is correct, the test is considered to have passed, otherwise it is considered a failure.
The assert
statement, is the most basic tools for creating assertions. Consider the following example:
In the above example, the assertion that 10 + 20 = 30
is correct, hence nothing is done.
In the above case, the assertion fails leading to the AssertionError
exception being raised.
Basically, unittest
assertion methods works as specialized and functional versions of the assert
statement. These methods are inherited by test classes which subclasses the unittest.TestCase
class.
For example, the logic in the previous examples can be expressed using the .assertEqual
method.
The assertion methods
In this section we will discuss some of the assertion methods in unittest
. Most of these methods have a negating counterpart, which will pass where it fails, e.g assertEqual
and assertNotEqual
. We will look at a negating pair together.
Almost all of the methods takes an optional extra argument i.e msg
which is the message that will be shown in the exception if the test fails.
assertTrue and assertFalse
When we use the assertTrue()
method, we are telling unittest
to verify that the result of an expression has a boolean value of True
.
assertTrue(exp)
copy
Where exp
is the expression to be evaluated.
The following example shows how we can use assertTrue
in a test.
The assertFalse
is the counter-method of assertTrue
, it verifies that the outcome of an expression has a boolean value of False
.
assertEqual and assertNotequal
The assertEqual
method checks that the two values given as argument are in fact equal. The test passes if they are equal and fails if not.
assertEqual(a, b)
copy
Consider the following example:
The assertNotEqual
method will verify that two values are not equal. It will pass where assertEqual
fails and vice verse.
assertAlmostEqual and assertNotAlmostEqual
Due to how floating point values are represented in memory, testing for equality between them may be troublesome and error prone. Consider the following example:
In the above assertion:
- The expression to be tested
((3.0 **0.5) ** 2.0)
, (3.0 ** 0.5)
is the square root of3.0
.- By squaring the returned value, we expect to get
3.0
again. - The returned value is
2.9999999999999996
which is close enough to be considered3
. - Howevere, the test fails because the
assertEqual
uses the==
operator which returnsFalse
.
To avoid the issues with floating values, unittest
provides the assertAlmostEqual
and assertAlmostNotEqual
methods. The two methods will allow vagueness in the values up to a certain level of precision
As shown above, when we use the assertAlmostEqual
method, the test passes.
The basic syntax of the two methods is shown below:
assertAlmostEqual(self, a, b, places=None) assertNotAlmostEqual(self, a, b, places=None)
copy
Where:
a
andb
are the values to be compared.places
is the number of decimal places that the values will be rounded to before they are compared. It defaults to7
decimal places.
assertRaises
Sometimes we may want to test that a piece of code raises an exception under certain conditions.
The assertRaises
method tests that an expected exception is actually raised. It passes if the exception is raised and fails otherwise. Its syntax is as shown below:
assertRaises(expected_exception, function, *args, **kwargs)
copy
The method runs the function(*args, **kwargs)
and checks that it raises the expected_exception.
import unittest def area(r): '''calculates area of a circle''' if r < 0: raise ValueError("Radius can't be negative.") area = 3.14 * r * r return area class TestExample(unittest.TestCase): def test_area(self): self.assertRaises(ValueError, area, -7) if __name__ == '__main__': unittest.main()
copy
Output:
.
----------------------------------------------------------------------
Ran 1 test in 0.000s
OK
In the above example, we defined a function area()
for calculating the area of a circle given its radius. We expect the function to raise a ValueError
if the given radius is negative. Since the test passed, we can be certain that the exception was raised.
assertRaises
also has a context managers interface, this means that we can use it in a with
statement, as in:
with assertRaises(expected_exception): do_something()
copy
Consider the following example:
We can inspect the raised exception by capturing it through the context manager object, as shown below.
The Complete Cheat Sheet
In the previous sections, we have looked at some of the assertion methods, however, there are more that we haven't looked at yet. In the following table we summarize all of the methods and what operation each performs in the background.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for unhashable objects |
|
All key/value pairs in |
Force tests to fail
When we want a test to fail under some conditions regardless of whether assertions pass or not, we can resort to the fail()
method. When this method is called, the test immediately fails.
Consider the following example: