I'm having difficulty testing python functions that return iteration, like functions that are inferior or functions that just return iterable, like return imap(f, some_iter) or return permutations([1,2,3]) .
So, with an example of permutations, I expect the output of the function to be [(1, 2, 3), (1, 3, 2), ...] . So, I'm starting to test my code.
def perm3(): return permutations([1,2,3])
This will not work, since perm3() is iterable, not a list. Therefore, we can correct this specific example.
def test_perm3(): assertEqual(list(perm3()), [(1, 2, 3), (1, 3, 2), ...])
And it works great. But what if I have nested iterations? That is, iterations giving iterations? As the expressions say product(permutations([1, 2]), permutations([3, 4])) . Now this is probably not useful, but itβs clear that it will be (when to deploy the iterators) something like [((1, 2), (3, 4)), ((1, 2), (4, 3)), ...] . However, we cannot just wrap the list around our result, as this will turn the iterable<blah> into [iterable<blah>, iterable<blah>, ...] . Well of course, I can do map(list, product(...)) , but this only works for nesting level 2.
So, does the python testing community have any solution for the problem when testing iterations? Naturally, some iterations cannot be tested in such a way as if you want an infinite generator, but nevertheless this question should be widespread enough for someone to think about it.