ID: py/imprecise-assert Kind: problem Severity: recommendation Precision: very-high Tags: - maintainability - testability Query suites: - python-security-and-quality.qls
unittest.TestCase provides a range of assertion methods. As well as the general forms
assertFalse() more specific forms such as
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
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,
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.
Python library reference: TestCase.assertEqual.