2017-08-03 105 views
0

在我FooClass对象我有self.mylist它由MyObject S和正在从一些数据库查询创建的列表。
到目前为止,在我的测试中,我一直在使用pytest夹具:嘲讽在Python

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

它工作得很好,当我不得不测试方法,在我需要的只是mylist“有些名单”。然而,该方法之一是依靠mylist并做一些检查,像

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... 

综上所述,我需要嘲笑MyObject测试mylist,但我想以某种方式定义一些嘲笑MyObject属性。

回答

0

准备对象事先:

@fixture 
def mock_mylist(): 
    foo = FooClass() 
    foo.mylist = MagicMock() 
    for el in foo.mylist: 
     el.MyObject.a = 1 
     el.MyObject.b = 2 
     # …and… 
    return foo