The class unittest.TestCase provides a range of assertion methods. As well as the general forms assertTrue() and assertFalse() more specific forms such as assertGreaterEquals() and assertNotIn() are provided. By using the more specific forms it is possible to get more precise and informative failure messages in the event of a test failing. This can speed up the debugging process.

Replace all calls to assertTrue() and assertFalse() that do not provide a custom failure message with a more specific variant. Alternatively, provide a tailored failure message using the assertTrue(condition, message) form.

In this example, assertTrue() and assertFalse() are used.

This will make it more difficult to determine what has gone wrong when self.assertTrue(1 in []) fails. The failure message "AssertionError: False is not true" is not very helpful.

A more useful error message can be generated by changing the asserts to the more specific forms as in the following example.

In this case, the failure message "AssertionError: 1 not found in []" is much more informative.

  • Python library reference: TestCase.assertEqual.