gpt4 book ai didi

python - 未使用mock.patch调用函数

转载 作者:太空宇宙 更新时间:2023-11-03 18:09:07 24 4
gpt4 key购买 nike

我正在尝试测试连接到BluetoothSocket 的BluetoothClient 类。为了避免使用真实的套接字,我只想测试是否使用正确的参数调用套接字中的 connect() 方法。使用mock.patch替换我的bluetooth_control模块中导入的蓝牙模块的效果并不像预期的那样。

据我所知,connect() 方法被调用,但断言告诉我情况并非如此。

代码:

被测单元(bluetooth_control.py):

import bluetooth

class BluetoothClient(object):
def __init__(self):
self.address="98:D3:31:B2:EF:32"
self.port=1

def establishConnection(self):
self.createSocket()
self.connect()

def createSocket(self):
self.sock=bluetooth.BluetoothSocket( bluetooth.RFCOMM )

def connect(self):
print "connect: sock="+str(self.sock)
self.sock.connect((self.address, self.port))

测试(bluetooth_control_test.py):

import unittest
import mock
import bluetooth_control
import bluetooth

class TestShelf(unittest.TestCase):

def setUp(self):
unittest.TestCase.setUp(self)
self.bc = bluetooth_control.BluetoothClient()
print "setUp"

def tearDown(self):
self.shelf = None
print "tearDown"

@mock.patch('bluetooth_control.bluetooth')
def testEstablishConnection(self,mock_bluetooth):
self.bc.establishConnection()
print "testEstablishConnection sock="+str(self.bc.sock)
mock_bluetooth.connect().assert_called_with(self.bc.sock,("98:D3:31:B2:EF:32",1))


if __name__ == "__main__":
unittest.main()

输出:

setUp
connect: sock=<MagicMock name='bluetooth.BluetoothSocket()' id='140433322111504'>
testEstablishConnection sock=<MagicMock name='bluetooth.BluetoothSocket()' id='140433322111504'>
FtearDown

======================================================================
FAIL: testEstablishConnection (__main__.TestShelf)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/usr/lib/python2.7/site-packages/mock.py", line 1201, in patched
return func(*args, **keywargs)
File "bluetooth_control_test.py", line 21, in testEstablishConnection
mock_bluetooth.connect().assert_called_with(self.bc.sock,("98:D3:31:B2:EF:32",1))
File "/usr/lib/python2.7/site-packages/mock.py", line 831, in assert_called_with
raise AssertionError('Expected call: %s\nNot called' % (expected,))
AssertionError: Expected call: mock(<MagicMock name='bluetooth.BluetoothSocket()' id='140433322111504'>, ('98:D3:31:B2:EF:32', 1))
Not called

----------------------------------------------------------------------
Ran 1 test in 0.003s

FAILED (failures=1)

最佳答案

两天后再次查看问题时,我发现了我犯的愚蠢错误。我必须修补实际方法并删除断言处错误添加的括号。

我不会删除这个问题,这样也许它会帮助别人避免这些错误。

测试

import unittest
import mock
import bluetooth_control
import bluetooth

class TestShelf(unittest.TestCase):

def setUp(self):
unittest.TestCase.setUp(self)
self.bc = bluetooth_control.BluetoothClient()
print "setUp"

def tearDown(self):
self.shelf = None
print "tearDown"

@mock.patch('bluetooth_control.bluetooth.BluetoothSocket.connect')
def testEstablishConnection(self,mock_connect):
self.bc.establishConnection()
print "testEstablishConnection sock="+str(self.bc.sock)
mock_connect.assert_called_with(("98:D3:31:B2:EF:32",1))


if __name__ == "__main__":
unittest.main()

关于python - 未使用mock.patch调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26296716/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com