cheesysam cheesysam - 1 month ago 7
Python Question

Is there a python assert() method which checks between two boundaries?

In some unit testing I'm currently doing, I need to pass a test when a variable lies between two boundary conditions.

Something like -

def myTest(self):
myInt = 5
self.assertBetween(myInt,3,8)


would pass the test. Or if myInt lied outside of the range 3 to 8 it would fail.

I've looked down the list of assert methods in the python documentation and can't work out which one would give me this sort of functionality.

Thanks.

Answer

You can use assertTrue() for that purpose:

self.assertTrue(myInt >= 3 and myInt <= 8)

Or, using Python's comparison chaining idiom:

self.assertTrue(3 <= myInt <= 8)