jeudi 5 mars 2015

Python + Testing + Mock

I am trying to mock an import during testing


Test.py looks like



# Store original __import__
orig_import = __import__

b_mock = mock.Mock()

def import_mock(name, *args):
if name == 'B':
return b_mock
return orig_import(name, *args)

with mock.patch('__builtin__.__import__', side_effect=import_mock):
import A


A.py looks like



import B

def a():
return B.func()


Now



b_mock.func.return_value = 'spam'


Therefore, A.a() should return 'spam' However, A.a() returns the entire mock object b_mock like this: < Mock name='mock.B.func()' id='139854736039632' >


Everytime B.func() is called, how do I get the return value which I have set(i.e 'spam') instead of getting the entire mock object?


Aucun commentaire:

Enregistrer un commentaire