单元测试和模拟

时间:2012-11-15 16:58:20

标签: 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 + 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

此代码功能正常,print的结果为:True1[]。现在,我需要计算调用self.matematica.adder()self.matematica.subs()的次数。

1 个答案:

答案 0 :(得分:1)

您可以再次运行您的代码,但第二次使用MagicMock对象。

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})

就是这样,method_count现在包含每个方法调用的次数。

此致