I need to patch/mock a class instance attribute so that each time it is called it returns a different value. The class instance attribute is a managed list which can change every time its accessed due to other processes adding to it.
I believe I should be able to use patch and the side_effect option to return different values per call but this does not seem to work with attributes as they are not called. I have tried various versions of patch and tried providing different callable (new_callable=) such as PropertyMock and other custom functions but I cant seem to get the desired behaviour. Can anyone suggest what I should do or what I'm doing wrong?
Basic example code and test shown below.
Code under test
# klass.py
import multiprocessing as mp
from unittest.mock import patch
class Klass:
def __init__(self):
self.manager = mp.Manager()
self.finished_item_ids = self.manager.list()
def func(self, item_id):
return item_id in self.finished_item_ids
And the pytest test
# tests_klass.py
"""pytest test for Klass.func"""
from unittest.mock import patch
def test_Klass_func():
"""test Klass.func"""
# SETUP
k = Klass()
# TEST
with patch.object(k, "finished_item_ids", side_effect=[[101], [101, 102], [101, 102, 103]]):
assert k.func(101) is True
assert k.func(102) is True
assert k.func(103) is True
Thanks :)