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

Mar*_*tin 4 python unit-testing mocking

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

comm_buffer = serial_port.readline().decode("utf-8").strip()
Run Code Online (Sandbox Code Playgroud)

其中“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:]
Run Code Online (Sandbox Code Playgroud)

Dar*_*ski 5

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

port = Mock()
port.readline = Mock(return_value="my string")
Run Code Online (Sandbox Code Playgroud)

这是您调用的第一个参数。因此,如果我将您的函数复制到名为 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")
Run Code Online (Sandbox Code Playgroud)