简体   繁体   中英

How to mock return value to a function multiple times

I have func1() which calls func2() multiple times based on different inputs. I now want to write a unit test for func1() by mocking return value of func2(). func2() returns string based on the provided inputs. Details are as below:

input_list = ["1", "2", "3"]
def func1(input_list):
    if "1" in input_list:
        response = func2("1")
        // Do something based on response
    if "2" in input_list:
        response = func2("2")
        // Do something based on response
    if "3" in input_list:
        response = func2("3")
        // Do something based on response
    return True //Based on some logic provided by response variable.

Unit test looks like below:

def test_case1():
    expected_response = True
    sample_input = ["1","2"]
    assert func1(sample_input) // Here I want to mock func2(), but not sure how ?

I tried to search in multiple forums and I found side_effect can be used, but nor sure how to use it in my case.

You can assign a function to side_effect .

Useful for raising exceptions or dynamically changing return values.

Eg

example.py :



def func1(input_list):
    if "1" in input_list:
        return func2("1")
    if "2" in input_list:
        return func2("2")
    if "3" in input_list:
        return func2("3")
    return True


def func2(input):
    pass

test_example.py :

import unittest
from unittest.mock import patch
from example import func1


class TestExample(unittest.TestCase):
    @patch('example.func2')
    def test_func1(self, mock_func2):
        def side_effect(input):
            if input == '1':
                return 'a'
            if input == '2':
                return 'b'
            if input == '3':
                return 'c'

        mock_func2.side_effect = side_effect
        actual1 = func1(['1'])
        self.assertEqual(actual1, 'a')
        actual2 = func1(['2'])
        self.assertEqual(actual2, 'b')
        actual3 = func1(['3'])
        self.assertEqual(actual3, 'c')


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

unit test result:

⚡  coverage run /Users/dulin/workspace/github.com/mrdulin/python-codelab/src/stackoverflow/64679177/test_example.py && coverage report -m --include='./src/**'
.
----------------------------------------------------------------------
Ran 1 test in 0.001s

OK
Name                                         Stmts   Miss  Cover   Missing
--------------------------------------------------------------------------
src/stackoverflow/64679177/example.py           10      2    80%   10, 14
src/stackoverflow/64679177/test_example.py      22      0   100%
--------------------------------------------------------------------------
TOTAL                                           32      2    94%

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