【问题标题】:Mock a Python class __init__ partially?部分模拟 Python 类 __init__?
【发布时间】:2020-09-23 05:35:03
【问题描述】:

我有一个初始化复杂的 Python 类。我想模拟类初始化以避免编写过多的脚手架代码。我想测试它的非模拟方法。

一个简单的例子:

class Person:

    def __init__(self, x, y, z):
        self.x = x
        self.y = y
        self.z = z

    def do_work(self):
        return self.x + self.y

有一个答案显示了如何完成它并且它有效 - https://stackoverflow.com/a/21771920/3346915

这是一个通过测试:

from unittest.mock import patch
with patch.object(Person, '__init__', lambda self: None):
    person = Person()
    person.x = 3
    person.y = 4
    assert person.do_work() == 7

不过,我想知道是否可以将xy 作为Person 初始化的一部分传递以避免在构造之后分配字段以减少代码量?

我想知道这是否可能?

from unittest.mock import patch
with patch.object(Person, '__init__', lambda self, x, y: None):
    person = Person(x=3, y=4)
    assert person.do_work() == 7

这当然行不通,因为xy 值没有分配给person 实例。

【问题讨论】:

    标签: python unit-testing mocking python-unittest patch


    【解决方案1】:

    lambdas 不支持赋值,但你不必使用lambda 作为第三个参数 - 普通(命名)函数也可以,所以你可以这样做:

    class Person:
    
        def __init__(self, x, y, z):
            self.x = x
            self.y = y
            self.z = z
    
        def do_work(self):
            return self.x + self.y
    
    from unittest.mock import patch
    def newinit(self, x, y):
        self.x = x
        self.y = y
    with patch.object(Person, '__init__', newinit):
        person = Person(x=3, y=4)
        assert person.do_work() == 7
    

    (在 Python 3.7.3 中测试)

    【讨论】:

    • 怀疑我需要一个最小的函数来加载,感谢您确认!
    猜你喜欢
    • 2015-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-10
    • 2012-06-16
    • 2014-03-26
    相关资源
    最近更新 更多