Search code examples
mockingpython-unittestpython-mock

unittest mock function output


I just found a mock library in Python2.7 and would like to unittest my function. Whatever tutorial I read it is always about classes. I can't get mocking function to work. The project is structured in the way that I have a few helping functions to be used in one function gathering and parsing data from the database. I want to mock database function and query function. For simple case it looks like this:

import unittest
import mock

def queryFnc(arg=0):
    # imitate returned result from a query
    if arg == 0:
        return "queryFunc 0"
    else: return "queryFunc 1"

def parsingFunc():
    # function will parse result returned from a query
    myString = queryFnc().upper()
    return myString

class Test(unittest.TestCase):
    def test_queryFunc0(self):
        self.assertEquals("queryFunc 0", queryFnc(arg=0))
    def test_queryFunc1(self):
        self.assertEquals("queryFunc 1", queryFnc(arg=1))
    @mock.patch('.queryFnc', return_value='queryMock')
    def test_queryMock(self, queryFnc):
        self.assertEquals('queryMock', queryFnc())

    def test_parsingFunc(self):
        self.assertEquals('QUERYFUNC 0', parsingFunc())
    @mock.patch('.queryFnc', return_value='queryMock')
    def test_parsingFuncMock(self):
        self.assertEquals('QUERYMOCK', parsingFunc())

if __name__ == "__main__":
    unittest.main()

I expected that the @mock.patch will replace the function on the call but I can get it working. I have this errors:

======================================================================
ERROR: test_queryMock (__main__.Test)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "\site-packages\mock\mock.py", line 1297, in patched
    arg = patching.__enter__()
  File "\site-packages\mock\mock.py", line 1353, in __enter__
    self.target = self.getter()
  File "\site-packages\mock\mock.py", line 1523, in <lambda>
    getter = lambda: _importer(target)
  File "\site-packages\mock\mock.py", line 1206, in _importer
    thing = __import__(import_path)
ValueError: Empty module name

is there a way to use different function result in another function? I am constrained to use Python2.7


Solution

  • The basic principle is that you patch where an object is looked up. You have a few possibilities using mock assignment, with contest manager and @mock.patch decorator. I attached a code for a few cases with included print statement showing you when the function looks for original and where for mock object.

    # CASE 1 taking query as mock inside test_ function
    def test_queryMock1(self):
        queryFnc = mock.Mock(return_value = 'case1')
        print("Case 1 inside test_ after mock assignemnt", queryFnc(), queryFnc(1))
        self.assertEquals('case1', queryFnc(1))
    print("Case1 after outside", queryFnc(), queryFnc(1))
    
    # CASE 2 contest management with
    def test_queryMock2(self):
        print("Case 2 inside test", queryFnc(), queryFnc(1))
        with mock.patch('__main__.queryFnc', return_value='case2'):
            print("Case 2 after with", queryFnc(), queryFnc(1))
            self.assertEquals('case2', queryFnc())
    print("Case 2 outside", queryFnc(), queryFnc(1))  
    
    # CASE 3 patching
    @mock.patch('__main__.queryFnc', return_value='case3')
    def test_queryMock3(self, *args):
        self.assertEquals('case3',queryFnc())
    
    # CASE 4 using contest management for nested function
    def test_InsideWith(self):
        print("Case 4 inside test_", queryFnc(), queryFnc(1)) 
        #with mock.patch('__main__.queryFnc', side_effect=mockText) as mock_function_obj:
        with mock.patch('__main__.queryFnc', return_value='case4'):
            print("Case 4 inside with", queryFnc(), queryFnc(1))
            self.assertEquals('CASE4', parsingFunc())
    print("Case 4 outside", queryFnc(), queryFnc(1))
    
    # CASE 5 using patch decorator
    @mock.patch('__main__.queryFnc', return_value='case5')
    def test_Patch(self, *args):
        print("Case 5 inside test_ after patch", queryFnc(), queryFnc(1))
        self.assertEquals('CASE5', parsingFunc())
        print("Case 5 inside test_, assert", queryFnc(), queryFnc(1))
    print("Case 5 outside", queryFnc(), queryFnc(1))
    

    Please note that in patch the module is specified as __main__ where you want to patch. If you experiment with module name you can see how it changes the patch behavior.