Python testing framework uses Python’s built-in assert() function which tests a particular condition. If the assertion fails, an AssertionError will be raised. The testing framework will then identify the test as Failure. Other exceptions are treated as Error.
The following three sets of assertion functions are defined in unittest module:
- Basic Boolean Asserts
- Comparative Asserts
- Asserts for Collections
Basic assert functions evaluate whether the result of an operation is True or False. All the assert methods accept a msg argument that, if specified, is used as the error message on failure.
Methods
1)assertEqual(arg1, arg2, msg = None)
Test that arg1 and arg2 are equal. If the values do not compare equal, the test will fail.
2)assertNotEqual(arg1, arg2, msg = None)
Test that arg1 and arg2 are not equal. If the values do compare equal, the test will fail.
3)assertTrue(expr, msg = None)
Test that expr is true. If false, test fails
4)assertFalse(expr, msg = None)
Test that expr is false. If true, test fails
5)assertIs(arg1, arg2, msg = None)
Test that arg1 and arg2 evaluate to the same object.
6)assertIsNot(arg1, arg2, msg = None)
Test that arg1 and arg2 don’t evaluate to the same object.
7)assertIsNone(expr, msg = None)
Test that expr is None. If not None, test fails
8)assertIsNotNone(expr, msg = None)
Test that expr is not None. If None, test fails
9)assertIsNotNone(expr, msg = None)
Test that arg1 is in arg2
10)assertIn(arg1, arg2, msg = None)
Test that arg1 is not in arg2
11)assertIsInstance(obj, cls, msg = None)
Test that obj is an instance of cls
12)assertNotIsInstance(obj, cls, msg = None)
Test that obj is not an instance of cls
Example
Output
Ran 2 tests in 0.000s
OK