Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit testing a function that returns a generator object

Tags:

python

The title pretty much sums it up: I tried to use assertEqual to test a function that returns a generator object, but that results in:

AssertionError: generator object genexpr> at 0x2e70c80> != (1,2,...)

Is there a clever way to do this other than iterating over the function and running assertEqual for each individual output?

like image 487
John Peters Avatar asked Oct 08 '12 05:10

John Peters


1 Answers

assertEqual(tuple(generator_object), (1, 2, ...))

if it's an infinite generator, or you just wish to look at the first n results for some reason, you can combint this with itertools.islice

assertEqual(tuple(islice(generator_object, n)), (1, 2, ...))
like image 84
John La Rooy Avatar answered Oct 23 '22 15:10

John La Rooy