Any way to reset a mocked method to its original state? - Python Mock - mock 1.0b1

SaiyanGirl picture SaiyanGirl · Jul 31, 2012 · Viewed 31.8k times · Source

I have the following simplified class I'm mocking:

class myClass(object):
    @staticmethod
    def A():
        #...

    def check(self):
        #code...
        value = self.A()
        #more code...

In my first test I mock only the method A

from django.test import TestCase
from mock import MagicMock
import myClass

class FirstTest(TestCase):

def setUp(self):
    myClass.A = MagicMock(return_value = 'CPU')

def test(self):
    #some tests 
    myClassObj = myClass()
    myClassObj.check()

Whereas in my second test I mock the entire check method:

from django.test import TestCase
from mock import MagicMock
import myClass

class SecondTest(TestCase):

def setUp(self):
    myClass.check = MagicMock(return_value = someObject)

def test(self):
    #some tests 
    myClassObj = myClass()
    myClassObj.check()

Now my assertions from my first test fail because, instead of calling check() and mocking A() inside check(), it calls the completely mocked check() from my second test.

Is there any way to clear and set the method to be 'normal' after the test? I tried myClass.check.reset_mock() already, but it doesn't seem to do anything. Moving the order of my tests doesn't do anything either.

I'm using mock 1.0b1 for python from http://pypi.python.org/pypi/mock/

Answer

ecatmur picture ecatmur · Aug 3, 2012

You can use mock.patch as a decorator or a context manager:

from mock import patch, MagicMock

@patch('myClass.A', MagicMock(return_value='CPU'))
def test(self):
    pass

or:

def test(self):
    with patch('myClass.A', MagicMock(return_value='CPU')):
        pass

If you don't supply a mock object to patch then it will provide an autospecced mock that you can modify:

@patch('myClass.A')
def test(self, mock_A):
    mock_A.return_value = 'CPU'
    pass

or:

def test(self):
    with patch('myClass.A') as mock_A:
        mock_A.return_value = 'CPU'
        pass

In all cases the original value will be restored when the decorated test function or context manager finishes.