可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I am doing some unit testing and at some point I need to mock a super
call to throw an error, for example:
@classmethod def myfunc(cls, *args, **kwargs) try: super(MyClass, cls).my_function(args, kwargs) except MyException as e: #...
I am using the mocker library to mock my objects in general but I haven't found a way to mock this.
回答1:
With the mock library I would do something like this.
In your class definition:
from somelib import ASuperClass class MyClass(ASuperClass): def my_cool_method(self): return super(MyClass, self).my_cool_method()
In the module where your calling MyClass:
from mock import patch from mymodule import MyClass @patch("mypackage.mymodule.ASuperClass.my_cool_method") def call_with_mock(mocked_super): myinstance = MyClass() myinstance.my_cool_method() # do stuff with `mocked_super` call_with_mock()
回答2:
I found a way, sort of hacky but it works, I'll explain with my example, this is based on this response so thanks @kindall:
def my_test(self): import __builtin__ from mocker import Mocker, KWARGS, ARGS mymocker = mocker.mock() mymocker.my_function(ARGS, KWARGS) mocker.throw(MyException) def mysuper(*args, **kwargs): if args and issubclass(MyClass, args[0]): return mymocker return original_super(*args, **kwargs) __builtin__.original_super = super __builtin__.super = mysuper with mocker: MyClass.myfunc()
so essentially what I do is check if the super
call is from the class I want to mock, else just do a normal super
.
Hope this helps someone :)
回答3:
Well, then you need to mock the my_function
method of the superclass of MyClass
to blow up.
回答4:
@Markus is looking in the right place. So long as you're unit testing (i.e. there's only one call to super
), you can mock __builtin__.super
as in:
with mock.patch('__builtin__.super') as mock_super: mock_super.side_effect = TypeError with self.assertRaises(TypeError): obj.call_with_super()