mercredi 22 juin 2016

python testing - use magicmock to serial.Serial without affecting serial.Serial.write

I want to write a test for a program in which i am connecting to a device via serial interface. I receive and send several times over this interface with readline, read and write. In the test I am mocking serial.Serial.read(line) and serial.Serial.write with:

    self.read_patcher = patch('serial.Serial.read')
    self.serial_read_mock = self.read_patcher.start()

    self.readline_patcher = patch('serial.Serial.readline')
    self.serial_readline_mock = self.readline_patcher.start()

    self.write_patcher = patch('serial.Serial.write')
    self.serial_write_mock = self.write_patcher.start()

At this line

dev = "/dev/ttyO1"
d0 = serial.Serial(port=dev, baudrate=300, bytesize=7, parity='E',
                    stopbits=1, timeout=1.5)                        

i get this error when running the test:

OSError: [Errno 2] No such file or directory: '/dev/ttyO1'

So I try to use MagicMock() for serial.Serial with

    self.init_patcher = patch('serial.Serial')
    self.serial_init_mock = self.init_patcher.start()
    self.serial_init_mock.return_value = MagicMock()

but doing this all the other readline, read and write mocks seem to be "magicmocked" too. Now all tests fail like this:

eq_(15, self.serial_readline_mock.call_count)
AssertionError: 15 != 0

('echo is not same as send:', '/?!\r\n', ' vs ', <MagicMock  name='Serial().write()' id='139717235665168'>)

How do I use MagicMock() on serial.Serial without affecting serial.Serial.read, serial.Serial.readline etc?

Aucun commentaire:

Enregistrer un commentaire