Unit testing a function that returns a generator object

前端 未结 2 872
故里飘歌
故里飘歌 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 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, ...))
    

提交回复
热议问题