Unittest and mock

Posted by user1410756 on Stack Overflow See other posts from Stack Overflow or by user1410756
Published on 2012-11-15T16:58:19Z Indexed on 2012/11/15 16:59 UTC
Read the original article Hit count: 216

Filed under:
|
|

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() . THANKS

© Stack Overflow or respective owner

Related posts about python

Related posts about unit-testing