One of my unittests checks to see if a range is set up correctly after reading a log file, and I'd like to just test var == range(0,10)
. However, range(0,1) == range(0,1)
evaluates to False
in Python 3.
Is there a straightforward way to test the equivalence of ranges in Python 3?
In Python3, range
returns an iterable of type range
. Two range
s are equal if and only if they are identical (i.e. share the same id
.) To test equality of its contents, convert the range
to a list
:
list(range(0,1)) == list(range(0,1))
This works fine for short ranges. For very long ranges, Charles G Waldman's solution is better.
The first proposed solution - use "list" to turn the ranges into lists - is ineffecient, since it will first turn the range objects into lists (potentially consuming a lot of memory, if the ranges are large), then compare each element. Consider e.g. a = range(1000000), the "range" object itself is tiny but if you coerce it to a list it becomes huge. Then you have to compare one million elements.
Answer (2) is even less efficient, since the assertItemsEqual is not only going to instantiate the lists, it is going to sort them as well, before doing the elementwise comparison.
Instead, since you know the objects are ranges, they are equal when their strides, start and end values are equal. E.g.
ranges_equal = len(a)==len(b) and (len(a)==0 or a[0]==b[0] and a[-1]==b[-1])
Try assertItemsEqual
, (in the docs):
class MyTestCase(unittest.TestCase):
def test_mytest(self):
a = (0,1,2,3,4)
self.assertItemsEqual(a, range(0,4))
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