jeudi 1 septembre 2016

Python Assert Function Called With Generator Arguments

I am testing some Python code with the mock framework and am having trouble asserting that a function was called with generator arguments. Below is a sample of the code that I am trying to test.

def gen(self, r):
    for i in range(r):
        yield i

def count(self, generator):
    sum = 0
    for i in generator
        sum += i
    return sum

I am trying to write a test that looks something like this:

import mock
import Foo

@mock.patch('Foo.putting_it_together')
@mock.patch('Foo.count')
class TestFoo():

def test_putting_it_together(self, mock_count, mock_putting_it_together):
    mock_putting_it_together()
    expected_calls_to_count = [mock.call("what goes here?"),
                        mock.call("what goes here?")]
    mock_count.assert_has_calls(expected_calls_to_count)

I am really not sure what to put in the "what goes here?" parts. Also, what would I do if I wanted to assert that an empty generator was used as an argument?

Aucun commentaire:

Enregistrer un commentaire