jeudi 3 août 2017

Mocking list of objects in Python

In my FooClass I've got self.mylist which consists of MyObjects and is being created from some database query.
So far in my tests I've been using pytest fixture:

    @fixture
    def mock_mylist():
        foo = FooClass()
        foo.mylist = MagicMock()
        return foo

It worked fine, when I had to tests methods, in which I needed mylist just "be some list". However, one of the method is relying on mylist and do some checks like

for el in mylist:
    if el.MyObject.a == 1 and el.MyObject.b == 2 and...
        #compare el.MyObject.c to some external value and process...

To sum up, I need a test mylist with mocked MyObject, but I would like to define somehow some of mocked MyObject attributes.

Aucun commentaire:

Enregistrer un commentaire