jeudi 20 juillet 2017

Python MagicMock attribute is static

Consider I have to unit-test the following function:

def f(A):
    tasks = []

    for i in range(0, 3):
        task = A.tasks.create()
        task.attr = i

        tasks.append(task)

    return tasks

When I mock A argument of f function, this happens:

>>> A = mock.MagicMock()
>>> tasks = f(A)

>>> for task in tasks:
>>>    print(task.attr)

Out: 2 2 2

I expected the following output: 0 1 2, why do I get that and how can I solve this? Thanks.

Aucun commentaire:

Enregistrer un commentaire