如何模拟内置方法
How to mock a builtin method
我有一个对象,它定义了一个只有先实例化才能访问的方法:
>>> import select
>>> select.poll().poll
<built-in method poll of select.poll object at 0x7fcf2a94bd80>
>>> select.poll.poll
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'builtin_function_or_method' object has no attribute 'poll'
>>>
因此我在模拟它时遇到了麻烦("poll" 方法)。
>>> from unittest import mock
>>> import select
>>> with mock.patch('select.poll.poll') as m:
... pass
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.4/unittest/mock.py", line 1197, in __enter__
original, local = self.get_original()
File "/usr/local/lib/python3.4/unittest/mock.py", line 1171, in get_original
"%s does not have the attribute %r" % (target, name)
AttributeError: <built-in function poll> does not have the attribute 'poll'
有什么推荐吗?
如评论中所述,您最好嘲笑select.poll
。您无需进一步了解该模块。您正在测试外部模块的行为,以了解您的代码将如何对其做出反应。因此,记住这一点你可以做的是:
import unittest
import select
from mock import mock, Mock
def my_method():
select.poll()
class Testor(unittest.TestCase):
@mock.patch('select.poll', return_value=Mock())
def test_it(self, m_poll):
m_poll.side_effect = Exception
with self.assertRaises(Exception):
my_method()
if __name__ == '__main__':
unittest.main()
因此,您模拟补丁 select.poll
,然后当您调用 poll()
时,您将设置 side_effect 以引发异常。使用 self.assertRaises
检查调用 my_method
时是否引发异常。
或者,如果你真的想在 poll 中模拟一个额外的方法,你可以这样做:
import unittest
import select
from mock import mock, Mock
def my_method():
s = select.poll()
s.poll()
class Testor(unittest.TestCase):
@mock.patch('select.poll', return_value=Mock())
def test_it(self, m_poll):
m_poll.return_value.poll.side_effect = Exception
with self.assertRaises(Exception):
my_method()
if __name__ == '__main__':
unittest.main()
从m_poll.return_value
的return值,得到poll
的side_effect
到return你的Exception
。
我有一个对象,它定义了一个只有先实例化才能访问的方法:
>>> import select
>>> select.poll().poll
<built-in method poll of select.poll object at 0x7fcf2a94bd80>
>>> select.poll.poll
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'builtin_function_or_method' object has no attribute 'poll'
>>>
因此我在模拟它时遇到了麻烦("poll" 方法)。
>>> from unittest import mock
>>> import select
>>> with mock.patch('select.poll.poll') as m:
... pass
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.4/unittest/mock.py", line 1197, in __enter__
original, local = self.get_original()
File "/usr/local/lib/python3.4/unittest/mock.py", line 1171, in get_original
"%s does not have the attribute %r" % (target, name)
AttributeError: <built-in function poll> does not have the attribute 'poll'
有什么推荐吗?
如评论中所述,您最好嘲笑select.poll
。您无需进一步了解该模块。您正在测试外部模块的行为,以了解您的代码将如何对其做出反应。因此,记住这一点你可以做的是:
import unittest
import select
from mock import mock, Mock
def my_method():
select.poll()
class Testor(unittest.TestCase):
@mock.patch('select.poll', return_value=Mock())
def test_it(self, m_poll):
m_poll.side_effect = Exception
with self.assertRaises(Exception):
my_method()
if __name__ == '__main__':
unittest.main()
因此,您模拟补丁 select.poll
,然后当您调用 poll()
时,您将设置 side_effect 以引发异常。使用 self.assertRaises
检查调用 my_method
时是否引发异常。
或者,如果你真的想在 poll 中模拟一个额外的方法,你可以这样做:
import unittest
import select
from mock import mock, Mock
def my_method():
s = select.poll()
s.poll()
class Testor(unittest.TestCase):
@mock.patch('select.poll', return_value=Mock())
def test_it(self, m_poll):
m_poll.return_value.poll.side_effect = Exception
with self.assertRaises(Exception):
my_method()
if __name__ == '__main__':
unittest.main()
从m_poll.return_value
的return值,得到poll
的side_effect
到return你的Exception
。