简体   繁体   English

unittest.mock.patch一个类实例,并设置方法返回值

[英]unittest.mock.patch a class instance, and set method return values

I've got an Alarm object that uses a Sensor object. 我有一个使用Sensor对象的Alarm对象。 In my test I'd like to patch Sensor with a Stub. 在我的测试中,我想用一个Stub修补Sensor。 The code below works, but I have to explicitly pass in the stub to the Alarm constructor: 下面的代码可以工作,但我必须明确地将存根传递给Alarm构造函数:

#tire_pressure_monitoring.py
from sensor import Sensor

class Alarm:

    def __init__(self, sensor=None):
        self._low_pressure_threshold = 17
        self._high_pressure_threshold = 21
        self._sensor = sensor or Sensor()
        self._is_alarm_on = False

    def check(self):
        psi_pressure_value = self._sensor.sample_pressure()
        if psi_pressure_value < self._low_pressure_threshold or self._high_pressure_threshold < psi_pressure_value:
            self._is_alarm_on = True

    @property
    def is_alarm_on(self):
        return self._is_alarm_on

#test_tire_pressure_monitoring.py
import unittest
from unittest.mock import patch, MagicMock, Mock

from tire_pressure_monitoring import Alarm
from sensor import Sensor

class AlarmTest(unittest.TestCase):

    def test_check_with_too_high_pressure(self):
        with patch('tire_pressure_monitoring.Sensor') as test_sensor_class:
            test_sensor_class.instance.sample_pressure.return_value=22
            alarm = Alarm(sensor=test_sensor_class.instance)
            alarm.check()
            self.assertTrue(alarm.is_alarm_on)

What I'd like to do, but can't seem to find a way to achieve, is to replace the Sensor instance with a stub, without passing anthing to the Alarm constructor. 我想做的,但似乎无法找到实现的方法,是用存根替换Sensor实例,而不将Anthing传递给Alarm构造函数。 This code looks to me like it should work, but doesn't: 这段代码看起来像我应该工作,但不是:

    def test_check_with_too_high_pressure(self):
    with patch('tire_pressure_monitoring.Sensor') as test_sensor_class:
        test_sensor_class.instance.sample_pressure.return_value=22
        alarm = Alarm()
        alarm.check()
        self.assertTrue(alarm.is_alarm_on)

The Alarm instance gets an instance of MagicMock, but the 'sample_pressure' method doesn't return 22. Basically I want to know if there is a way to use unittest.mock to test the Alarm class without needing a constructor that takes a Sensor instance as argument. Alarm实例获取一个MagicMock实例,但'sample_pressure'方法不返回22.基本上我想知道是否有办法使用unittest.mock来测试Alarm类而不需要一个带有Sensor实例的构造函数作为论点。

When you call test_sensor_class.instance you are using test_sensor_class as a property holder, adding a Mock property instance to which you add a Mock property sample_pressure . 当您调用test_sensor_class.instance您使用test_sensor_class作为属性持有者,添加一个Mock属性instance ,您可以向其添加Mock属性sample_pressure Your patch is not used at all, your code is in fact equivalent to: 你的补丁根本就没用过,你的代码实际上相当于:

def test_check_with_too_high_pressure(self):
    instance = MagicMock()
    instance.sample_pressure.return_value=22
    alarm = Alarm(sensor=instance)
    alarm.check()
    self.assertTrue(alarm.is_alarm_on)

What you want to do it patch the call to Sensor() . 您想要做什么修补对Sensor()的调用。

Using your code, you simply need to set return value of the mocked class test_sensor_class to a preset mock of Sensor . 使用您的代码,您只需将模拟类test_sensor_class返回值设置为Sensor的预设模拟。

def test_check_with_too_high_pressure(self):
    with patch('tire_pressure_monitoring.Sensor') as test_sensor_class:
        mockSensor = MagicMock()
        mockSensor.sample_pressure.return_value = 22
        test_sensor_class.return_value = mockSensor
        alarm = Alarm()
        alarm.check()
        self.assertTrue(alarm.is_alarm_on)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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