Python 单元测试与模拟

Python 单元测试与模拟,python,unit-testing,mocking,Python,Unit Testing,Mocking,我正在用Python中的unittest进行测试,一切正常。现在,我介绍了mock,我需要解决一个问题。这是我的代码: from mock import Mock import unittest class Matematica(object): def __init__(self, op1, op2): self.op1 = op1 self.op2 = op2 def adder(self): return self.op1

我正在用Python中的
unittest
进行测试,一切正常。现在,我介绍了
mock
,我需要解决一个问题。这是我的代码:

from mock import Mock
import unittest


class Matematica(object):
    def __init__(self, op1, op2):
        self.op1 = op1
        self.op2 = op2
    def adder(self):
        return self.op1 + self.op2
    def subs(self):
        return abs(self.op1 - self.op2)
    def molt(self):
        return self.op1 * self.op2
    def divid(self):
        return self.op1 / self.op2

class TestMatematica(unittest.TestCase):
    """Test della classe Matematica"""
    def testing(self):
        """Somma"""
        mat = Matematica(10,20)
        self.assertEqual(mat.adder(),30)
        """Sottrazione"""
        self.assertEqual(mat.subs(),10)

class test_mock(object):
    def __init__(self, matematica):
        self.matematica = matematica
    def execute(self):
        self.matematica.adder()
        self.matematica.adder()
        self.matematica.subs()


if __name__ == "__main__":
    result = unittest.TextTestRunner(verbosity=2).run(TestMatematica('testing'))
    a = Matematica(10,20)    
    b = test_mock(a)
    b.execute()
    mock_foo = Mock(b.execute)#return_value = 'rafa')
    mock_foo()
    print mock_foo.called
    print mock_foo.call_count
    print mock_foo.method_calls

此代码在功能上是有效的,
打印的结果是:
True
1
[]
。现在,我需要计算调用了多少次
self.matematica.adder()
self.matematica.subs()

from mock import Mock
from mock import MagicMock  # import MagicMock
from collections import Counter
import unittest


class Matematica(object):
    def __init__(self, op1, op2):
        self.op1 = op1
        self.op2 = op2
    def adder(self):
        return self.op1 + self.op2
    def subs(self):
        return abs(self.op1 - self.op2)
    def molt(self):
        return self.op1 * self.op2
    def divid(self):
        return self.op1 / self.op2

class TestMatematica(unittest.TestCase):
    """Test della classe Matematica"""
    def testing(self):
        """Somma"""
        mat = Matematica(10,20)
        self.assertEqual(mat.adder(),30)
        """Sottrazione"""
        self.assertEqual(mat.subs(),10)

class test_mock(object):
    def __init__(self, matematica):
        self.matematica = matematica
    def execute(self):
        self.matematica.adder()
        self.matematica.adder()
        self.matematica.subs()


if __name__ == "__main__":
    result = unittest.TextTestRunner(verbosity=2).run(TestMatematica('testing'))
    a = Matematica(10,20)    
    b = test_mock(a)
    b.execute()
    mock_foo = Mock(b.execute)#return_value = 'rafa')
    mock_foo()
    print mock_foo.called
    print mock_foo.call_count
    print mock_foo.method_calls

    # added from here
    c = MagicMock()
    d = test_mock(c)
    d.execute()
    method_count = Counter([str(method) for method in c.method_calls])
    print c.method_calls
    print method_count
结果如下:

OK
True
1
[]
[call.adder(), call.adder(), call.subs()]
Counter({'call.adder()': 2, 'call.subs()': 1})
就是这样,方法_count现在包含了每个方法被调用的次数


关于

,到目前为止您尝试了什么?你遇到了什么问题?我想知道:数一数有多少次被称为self.matematica.adder()和self.matematica.subs()。对不起,还有一个问题。例如,如果在我的代码中,matematica.adder有参数adder(a,b),我如何知道我传递的a和b的值?感谢您必须模拟adder()方法,以便跟踪传递给函数的参数。Mock定义一个和一个属性,您可以在其中访问它。