Use AssertIn when performing inclusion tests

assertIn and assertNotIn provide more helpful failure messages than assertTrue or assertFalse.

When performing inclusion checks it's better to use the correct tool for the job: assertIn and assertNotIn are provided explicitly for this task.

The difference is assertion methods such as assertTrue will report the message:

AssertionError: False is not True

While assertIn will provided a much more helpful message:

AssertionError: 'foo' not found in 'items'

Good quality failure messages allows the failure mode to be understood rapidly and therefore the problem to be fixed more quickly.

If our GitHub code review bot spots this issue in your pull request it gives this advice:

code-review-doctorbotsuggested changes just now
tests.py
1
+
class TestFeature(unittest.TestCase):
2
+
    def test_feature(self):
3
+
        self.assertTrue(value in values)

assertIn and assertNotIn provide more helpful failure messages than assertTrue or assertFalse.

Read more
Suggested changes
-
        self.assertTrue(value in values)
+
        self.assertIn(value, values)
Commit suggestion
Update tests.py

Instantly check if you have this issue for free

    Works with tools you use

    Read about how it works.