python - Avoid executing __init__ of mocked class -
python - Avoid executing __init__ of mocked class -
i have class expensive __init__
function. don't want function called tests.
for purpose of example, made class raises exception in __init__
:
class classwithcomplexinit(object): def __init__(self): raise exception("complex!") def get_value(self): homecoming 'my value'
i have sec class constructs instance of classwithcomplexinit
, uses it's function.
class systemundertest(object): def my_func(self): foo = classwithcomplexinit() homecoming foo.get_value()
i trying write unit tests around systemundertest#my_func()
. problem having no matter how seek mock classwithcomplexinit
, __init__
function gets executed , exception raised.
class testcasewithoutsetup(unittest.testcase): @mock.patch('mypackage.classwithcomplexinit.get_value', return_value='test value') def test_with_patched_function(self, mockfunction): sut = systemundertest() result = sut.my_func() # fails, executes classwithcomplexinit.__init__() self.assertequal('test value', result) @mock.patch('mypackage.classwithcomplexinit') def test_with_patched_class(self, mockclass): mockclass.get_value.return_value = 'test value' sut = systemundertest() result = sut.my_func() # seems not execute classwithcomplexinit.__init__() self.assertequal('test value', result) # still fails # assertionerror: 'test value' != <magicmock name='classwithcomplexinit().get_value()' id='4436402576'>
the sec approach above 1 got this similar q&a didn't work either. seemed not run __init__
function assertion fails because result ends beingness mock instance opposed value.
i tried configure patch
instance in setup
function, using start
, stop
functions the docs suggest.
class testcasewithsetup(unittest.testcase): def setup(self): self.mockclass = mock.magicmock() self.mockclass.get_value.return_value = 'test value' patcher = mock.patch('mypackage.classwithcomplexinit', self.mockclass) patcher.start() self.addcleanup(patcher.stop) def test_my_func(self): sut = systemundertest() result = sut.my_func() # seems not execute classwithcomplexinit.__init__() self.assertequal('test value', result) # still fails # assertionerror: 'test value' != <magicmock name='mock().get_value()' id='4554658128'>
this seems avoid __init__
function value set get_value.return_value
isn't beingness respected , get_value()
still returning magicmock
instance.
how can mock class complicated __init__
instantiated code under test? ideally, solution works many unit tests within testcase class (e.g. not needing patch
every test).
i using python version 2.7.6
.
first, need utilize same name patching create foo
, is,
class systemundertest(object): def my_func(self): foo = mypackage.classwithcomplexinit() homecoming foo.get_value()
second, need configure right mock object. configuring classwithcomplexinit.get_value
, unbound method, need configure classwithcomplexinit.return_value.get_value
, mock
object called foo.get_value()
.
@mock.patch('mypackage.classwithcomplexinit') def test_with_patched_class(self, mockclass): mockclass.return_value.get_value.return_value = 'test value' sut = systemundertest() result = sut.my_func() # seems not execute classwithcomplexinit.__init__() self.assertequal('test value', result) # still fails
python python-2.7 mocking python-unittest python-mock
Comments
Post a Comment