CodeQL documentation

Imprecise assert

ID: py/imprecise-assert
Kind: problem
Severity: recommendation
Precision: very-high
Tags:
   - maintainability
   - testability
Query suites:
   - python-security-and-quality.qls

Click to see the query in the CodeQL repository

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.

Recommendation

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.

Example

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

from unittest import TestCase

class MyTest(TestCase):
    
    
    def testInts(self):
        self.assertTrue(1 == 1)
        self.assertFalse(1 > 2)
        self.assertTrue(1 in []) #This will fail

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.

from unittest import TestCase

class MyTest(TestCase):
    
    
    def testInts(self):
        self.assertEqual(1, 1)
        self.assertLessEqual(1, 2)
        self.assertIn(1, []) #This will fail

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

References