0

I think something like this sort of answers my question, I just don't know how to implement it.

I wrote this function:

def print_max(list_name,k):
        for i in range(len(list_name)-(k-1)):
                yield (max(list_name[i:i+k]))


print(print_max([10,5,2,7,8,7],3))

I want to add this to a pytest. This is usually how i run pytest:

@pytest.marks.parametrize('input1','output1',[[10,5,2,7,8,7], X]:
def test_fun(input1, output1):
    assert print_max(input1) == output1

What would I change X to in this pytest to allow for the function to have a yield output? Or what's the best way to do this pytest, given the function is a yield?

Slowat_Kela
  • 1,377
  • 2
  • 22
  • 60
  • I think you don't understand what `print_max` is doing, it returns a *generator* which is an iterator. You are simply comparing the generator object to `output1`... this really has nothing to do with Pytest. – juanpa.arrivillaga Sep 09 '20 at 20:29

1 Answers1

1

When you use yield you're producing an iterator, so you need to iterate the results before comparing.

You need something like this:

assert [print_max(input1)] == output1   # create list from iterator
Mike67
  • 11,175
  • 2
  • 7
  • 15