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.
If you assert multiple conditions in a single assert statement, you only make the job harder. This is because you cannot determine which one of the conditions caused the bug. If you want to push it, you can assert multiple conditions similar to how you check multiple conditions in an if statement.
The assert keyword is used when debugging code. The assert keyword lets you test if a condition in your code returns True, if not, the program will raise an AssertionError. You can write a message to be written if the code returns False, check the example below.
Python's assert statement is a debugging aid, not a mechanism for handling run-time errors. The goal of using assertions is to let developers find the likely root cause of a bug more quickly. An assertion error should never be raised unless there's a bug in your program.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With