Frédéric’s suggestion to use:
self.assertTrue(3 <= myInt <= 8)
results in test output like this:
AssertionError: False is not True
which gives the developer no clue as to what the problematic value of myInt actually was. It is better to be more long-winded:
self.assertGreaterEqual(myInt, 3)
self.assertLessEqual(myInt, 8)
because then you get helpful test output like this:
AssertionError: 21 not less than or equal to 8
If you find yourself using this idiom a lot, then you can write your own assertion method:
def assertBetween(self, value, min, max):
"""Fail if value is not between min and max (inclusive)."""
self.assertGreaterEqual(value, min)
self.assertLessEqual(value, max)
Note that if the value to be tested is known to be an integer, then you can use assertIn together with range:
self.assertIn(myInt, range(3, 9))
which results in test output like this:
AssertionError: 21 not found in range(3, 9)