如何模拟模块python,补丁没有找到属性
How to mock modules python, patch does not find the attribute
在一个函数中,我正在使用我想要修补的 uuid1。
def myFunction():
my_value = uuid4.int
smth else..
我希望能够模拟 my_value 所以它在我的单元测试中总是 returns 相同的数字,因为我需要它以供进一步使用。
我尝试这样做:
@patch('folder.myFunction.uuid4')
def test_myFunction(self, mock_value):
mock_value.return_value = 22222
但它抛出一个错误,指出 myFunction 没有 uuid4 作为属性。
如何模拟它的值?
这取决于你的导入。假设您有一个名为 module.py
的模块,并且您有一个像这样的导入:
from uuid import uuid4
这意味着在这个模块中我们现在有一个名为 uuid4
的变量。这是要嘲笑的东西。
@patch('path.to.module.uuid4.int')
你得到的错误是正确的。您的函数没有 uuid4
属性。
我在字里行间假设 uuid4
is a method of the uuid
模块通常会生成随机 uuid。
测试时你说你希望它总是 return 相同的值。为此,您可以用 unittest.mock.Mock
代替 uuid.uuid4
。
In [36]: uuid_mock = Mock(return_value=uuid.UUID('77f1df52-4b43-11e9-910f-b8ca3a9b9f3e'))
In [37]: uuid_mock()
Out[37]: UUID('77f1df52-4b43-11e9-910f-b8ca3a9b9f3e')
类似这样的东西用于测试以下功能 (f
)
import uuid, unittest
from unittest.mock import Mock, patch
def f():
z = uuid.uuid4()
return z.int
补丁的目标是 uuid
方法 - uuid.uuid4
。为 补丁 的 new
参数指定具有固定 return 值的 unittest.mock.Mock
。在测试期间,Mock 将被替换为 uuid.uuid4
class TestF(unittest.TestCase):
uuid_mock = Mock(return_value=uuid.UUID('77f1df52-4b43-11e9-910f-b8ca3a9b9f3e'))
good_uuid = uuid.UUID('77f1df52-4b43-11e9-910f-b8ca3a9b9f3e').int
bad_uuid = uuid.UUID('77f1df52-4b43-11e9-910f-b8ca3a9b5a31').int
@patch(target='uuid.uuid4', new=TestF.uuid_mock)
def test_myFunction_True(self):
self.assertEqual(f(), self.good_uuid)
@patch(target='uuid.uuid4', new=TestF.uuid_mock)
def test_myFunction_False(self):
self.assertNotEqual(f(), self.bad_uuid)
if __name__ == '__main__':
unittest.main()
结果:
..
----------------------------------------------------------------------
Ran 2 tests in 0.001s
OK
如果您想测试依赖于 f
的 return 值的函数,并且您希望 f
在测试期间始终 return 相同的值,那么使f
补丁的目标。
def g():
return f() == uuid.UUID('77f1df52-4b43-11e9-910f-b8ca3a9b9f3e').int
class TestG(unittest.TestCase):
good_uuid_mock = Mock(return_value=uuid.UUID('77f1df52-4b43-11e9-910f-b8ca3a9b9f3e').int)
bad_uuid_mock = Mock(return_value=uuid.UUID('77f1df52-4b43-11e9-910f-b8ca3a9b5a31').int)
@patch(target='__main__.f', new=TestG.good_uuid_mock)
def test_myFunction_True(self):
self.assertTrue(g())
@patch(target='__main__.f', new=TestG.bad_uuid_mock)
def test_myFunction_False(self):
self.assertFalse(g())
在一个函数中,我正在使用我想要修补的 uuid1。
def myFunction():
my_value = uuid4.int
smth else..
我希望能够模拟 my_value 所以它在我的单元测试中总是 returns 相同的数字,因为我需要它以供进一步使用。 我尝试这样做:
@patch('folder.myFunction.uuid4')
def test_myFunction(self, mock_value):
mock_value.return_value = 22222
但它抛出一个错误,指出 myFunction 没有 uuid4 作为属性。
如何模拟它的值?
这取决于你的导入。假设您有一个名为 module.py
的模块,并且您有一个像这样的导入:
from uuid import uuid4
这意味着在这个模块中我们现在有一个名为 uuid4
的变量。这是要嘲笑的东西。
@patch('path.to.module.uuid4.int')
你得到的错误是正确的。您的函数没有 uuid4
属性。
我在字里行间假设 uuid4
is a method of the uuid
模块通常会生成随机 uuid。
测试时你说你希望它总是 return 相同的值。为此,您可以用 unittest.mock.Mock
代替 uuid.uuid4
。
In [36]: uuid_mock = Mock(return_value=uuid.UUID('77f1df52-4b43-11e9-910f-b8ca3a9b9f3e'))
In [37]: uuid_mock()
Out[37]: UUID('77f1df52-4b43-11e9-910f-b8ca3a9b9f3e')
类似这样的东西用于测试以下功能 (f
)
import uuid, unittest
from unittest.mock import Mock, patch
def f():
z = uuid.uuid4()
return z.int
补丁的目标是 uuid
方法 - uuid.uuid4
。为 补丁 的 new
参数指定具有固定 return 值的 unittest.mock.Mock
。在测试期间,Mock 将被替换为 uuid.uuid4
class TestF(unittest.TestCase):
uuid_mock = Mock(return_value=uuid.UUID('77f1df52-4b43-11e9-910f-b8ca3a9b9f3e'))
good_uuid = uuid.UUID('77f1df52-4b43-11e9-910f-b8ca3a9b9f3e').int
bad_uuid = uuid.UUID('77f1df52-4b43-11e9-910f-b8ca3a9b5a31').int
@patch(target='uuid.uuid4', new=TestF.uuid_mock)
def test_myFunction_True(self):
self.assertEqual(f(), self.good_uuid)
@patch(target='uuid.uuid4', new=TestF.uuid_mock)
def test_myFunction_False(self):
self.assertNotEqual(f(), self.bad_uuid)
if __name__ == '__main__':
unittest.main()
结果:
..
----------------------------------------------------------------------
Ran 2 tests in 0.001s
OK
如果您想测试依赖于 f
的 return 值的函数,并且您希望 f
在测试期间始终 return 相同的值,那么使f
补丁的目标。
def g():
return f() == uuid.UUID('77f1df52-4b43-11e9-910f-b8ca3a9b9f3e').int
class TestG(unittest.TestCase):
good_uuid_mock = Mock(return_value=uuid.UUID('77f1df52-4b43-11e9-910f-b8ca3a9b9f3e').int)
bad_uuid_mock = Mock(return_value=uuid.UUID('77f1df52-4b43-11e9-910f-b8ca3a9b5a31').int)
@patch(target='__main__.f', new=TestG.good_uuid_mock)
def test_myFunction_True(self):
self.assertTrue(g())
@patch(target='__main__.f', new=TestG.bad_uuid_mock)
def test_myFunction_False(self):
self.assertFalse(g())