Unit testing a function that returns a generator object

前端 未结 2 873
故里飘歌
故里飘歌 2021-02-13 13:59

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: gene

相关标签:
2条回答
  • 2021-02-13 14:57

    Use next() on the generator object.

    assertEqual(next(generator_object), (1, 2, ...))
    
    0 讨论(0)
  • 2021-02-13 15:04
    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, ...))
    
    0 讨论(0)
提交回复
热议问题