Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 设置返回值和测试方法_调用MagickMock_Python 3.x_Mocking_Python Unittest - Fatal编程技术网

Python 3.x 设置返回值和测试方法_调用MagickMock

Python 3.x 设置返回值和测试方法_调用MagickMock,python-3.x,mocking,python-unittest,Python 3.x,Mocking,Python Unittest,我需要使用magickmock模拟pyserial写入方法 class UnderTest (): def __init__(self): self.serialport = serial.Serial(port='dev/ttyACM0') def write(self, some_bytes): l = self.serialport.write(some_bytes) return l magickMock可以用于创建,但我无法找到如何模拟write方法来设

我需要使用magickmock模拟pyserial写入方法

class UnderTest ():
def __init__(self):

    self.serialport = serial.Serial(port='dev/ttyACM0')

def write(self, some_bytes):

    l = self.serialport.write(some_bytes)
    return l
magickMock可以用于创建,但我无法找到如何模拟write方法来设置返回值和检查方法调用中的调用。这是我想要的/

class Test(unittest.TestCase):
""" tests for init LockControler with no LC connected"""

def setUp(self):
    # no serial port so mock all functions of serial port
    self.serialmock = unittest.mock.MagicMock(spec=serial.Serial, name='serialmock')
    patcher1 = unittest.mock.patch('serial.Serial', new=self.serialmock)
    serialm = patcher1.start()
    self.undertest = UnderTest()

def test_write(self):

    writemock = self.serialmock().write()(return_value=2)
    a = unittest.mock.patch.object(self.undertest.serialport, 'write', new=writemock)
    self.undertest.write(b'\x50\x61\x73\x73')
    # I want to check here method_calls