如何使用Python 3 unittest模拟从串口模拟数据?

时间:2017-09-29 15:30:28

标签: python unit-testing mocking

我想为Python 3方法创建单元测试,该方法从串行端口读取unicode文本字符串。我想测试方法对各种字符串的响应。我想模仿的代码行是:

comm_buffer = serial_port.readline().decode("utf-8").strip()

其中'serial_port'是传递给方法的串行端口的实例。我想使用unittest.mock模块将comm_buffer变量设置为unicode字符串,但我一整天都在努力,但没有成功。我第一次尝试使用模拟,而且我不够深入。

整个方法的代码是:

def wait_for_data(serial_port, comm_flag = ""):
"""Read lines of data from the serial_port

Receives optional comm_flag (single character to check for at beginning of string)"""
logging.debug("Start wait_for_data  " + comm_flag)
timeout_count = 0
while True:
    # Get a line from the buffer and convert to string and strip line feed
    logging.debug("Waiting for data…")
    comm_buffer = serial_port.readline().decode("utf-8").strip()
    if len(comm_buffer) == 0:
        timeout_count += 1
        logging.warning("Serial port timeout - no data received. Timeout count = " + str(timeout_count))
        if timeout_count > 10:
            raise TimeoutError(["Too many timeouts"])
    # If no id character was specified, just return the string
    elif comm_flag == "":
        logging.debug("Returning no comm_flag")
        return comm_buffer
    # If an id character was specified, return the string if it's present (strip id), otherwise wait for another string
    elif comm_buffer[0] == comm_flag:
        logging.debug("Returning with comm_flag")
        return comm_buffer[1:]

1 个答案:

答案 0 :(得分:1)

Serial_port不是串行端口的实例,而是具有readline()方法的对象。所以不要关心串口这样的东西,你的模拟对象是一个带有readline()方法的对象,它提供了你想要测试的那种值。 所以你只需要做类似的事情:

port = Mock()
port.readline = Mock(return_value="my string")

这是您通话的第一个参数。 所以,如果我在一个名为test.port的模块中复制你的函数,那么这个测试就可以了:

class TestWaitData(unittest.TestCase):

    def testFunc(self):
        port = mock.Mock()
        port.readline = mock.Mock(return_value="my string".encode('utf-8')) # as you use a decode call

        self.assertEqual(test.port.wait_for_data(port), "my string")

    def testBufferEmpty(self):
        port = mock.Mock()
        port.readline = mock.Mock(return_value="".encode('utf-8'))

        with self.assertRaises(TimeoutError):
            test.port.wait_for_data(port)

    def testWithFlag(self):
        port = mock.Mock()
        port.readline = mock.Mock(return_value="fmy string".encode('utf-8'))

        self.assertEqual(test.port.wait_for_data(port, 'f'), "my string")