简体   繁体   中英

Unittest and mock

I'm testing with unittest in Python and it's ok. Now, I have introduced mock and I need to resolve a question. This is my code:

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

This code is functionally and result of print is: True , 1 , [] . Now, I need to count how many times are called self.matematica.adder() and self.matematica.subs() .

you could just run again your code but the second time with a MagicMock object.

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

Results are:

OK
True
1
[]
[call.adder(), call.adder(), call.subs()]
Counter({'call.adder()': 2, 'call.subs()': 1})

There it is, method_count contains now how many times each method was called.

Regards

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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