简体   繁体   中英

Function is not called using mock.patch

I am trying to test the Class BluetoothClient which connects to a BluetoothSocket. To avoid using real sockets I just want to test that the connect() method from the socket is called with the right parameters. Using mock.patch to replace the imported bluetooth module in my bluetooth_control module doesn't work out like expected.

As I see it, the connect() method is called but the assertion tells me otherwise.

Code:

Unit Under Test (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))

Test (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()

Output:

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)

After looking at the problem again two days later I found the stupid mistakes I made. I had to patch the actual method and remove the falsely added brackets at the assertion.

I am not going to delete this question so that maybe it will help someone to avoid these mistakes.

Test

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM