Home > Software engineering >  How to mock a function within a Function. Is it possible to use two or more Patches to mock a functi
How to mock a function within a Function. Is it possible to use two or more Patches to mock a functi

Time:11-21

I need to mock a Function (funk) call that is dependent on the return values of two other functions that is called within this Function (funk). Is it possible to use more than 1 Patch to mock the return value of the two called functions.

API to be Tested/ Mocked:

from subprocess import check_output


def get_value_a(value_a: int):  
    return value_a  

def get_value_b(value_b: int):  
    return value_b  

def print_contents_of_cwd(val_a, val_b):  
    if val_a > 0:  
        return check_output(['ls']).split()  
    else:  
        return val_b  

I am trying to use two patches to mock the return value of both the functions (get_value_a and get_value_b) as below:

from Mack_Test_Practise import list_dir
from unittest import mock, TestCase


class TestExamples(TestCase):
    # Using Decorator Method

    @mock.patch('Mack_Test_Practise.list_dir.check_output',
                return_value=b'list_dir.py')
    @mock.patch('Mack_Test_Practise.list_dir.get_value_a',
                return_value=5)
    @mock.patch('Mack_Test_Practise.list_dir.get_value_b',
                return_value=3)
    def test_print_contents_of_file(self, mock_check_output,val_a, val_b):
        actual_result = list_dir.print_contents_of_cwd(val_a, val_b)
        print("actual_result", actual_result)

        expected_result = b'list_dir.py'
        self.assertIn(expected_result, actual_result)  
  

When I run the test the following error is thrown.

Error  
Traceback (most recent call last):  
  File "/usr/lib/python3.6/unittest/mock.py", line 1183, in patched  
    return func(*args, **keywargs)
  File  
 "/home/shyam/PycharmProjects/pythonProject/Mock_Test/test_list_dir.py", line 15, in test_print_contents_of_file
    actual_result = list_dir.print_contents_of_cwd(val_a, val_b)
  File "/home/shyam/PycharmProjects/pythonProject/Mack_Test_Practise/list_dir.py", line 13, in print_contents_of_cwd  
    if val_a > 0:  
**TypeError: '>' not supported between instances of 'MagicMock' and 'int'**  

My first question is, is it allowed to use multiple patches like I did ?
My Second question is why do we get the Type Error since it is a legal operator in Python.
Please suggest a solution.

Thanks a million and Best Regards!!

CodePudding user response:

It is legal to use multiple patches like this.

I think your issue is because you have patched a function, and are trying to compare the patched function to an integer. You set the return value in the mock, but you still need to call it to get that return value.

actual_result = list_dir.print_contents_of_cwd(val_a(), val_b())

Note the included parentheses on val_a() and val_b()

Now, since these are no longer values, but rather functions (technically, they are MagicMock objects, but they are replacing functions) I would suggest renaming them to get_val_a() and get_val_b() so that your intent is more clear.

  • Related