Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 如何多次模拟函数的返回值_Python 3.x_Unit Testing_Mocking_Pytest - Fatal编程技术网

Python 3.x 如何多次模拟函数的返回值

Python 3.x 如何多次模拟函数的返回值,python-3.x,unit-testing,mocking,pytest,Python 3.x,Unit Testing,Mocking,Pytest,我有一个func1(),它根据不同的输入多次调用func2()。现在我想通过模拟func2()的返回值为func1()编写一个单元测试。 func2()根据提供的输入返回字符串。 详情如下: input_list = ["1", "2", "3"] def func1(input_list): if "1" in input_list: response = func2("1&quo

我有一个func1(),它根据不同的输入多次调用func2()。现在我想通过模拟func2()的返回值为func1()编写一个单元测试。 func2()根据提供的输入返回字符串。 详情如下:

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.
单元测试如下所示:

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

我尝试在多个论坛中搜索,发现可以使用副作用,但也不确定如何在我的案例中使用它。

您可以为
副作用指定一个函数

用于引发异常或动态更改返回值

例如

example.py


def func1(输入列表):
如果输入列表中的“1”:
返回函数2(“1”)
如果输入列表中的“2”:
返回函数2(“2”)
如果输入列表中的“3”:
返回函数2(“3”)
返回真值
def func2(输入):
通过
test\u example.py

导入单元测试
从unittest.mock导入修补程序
来自示例import func1
类TestExample(unittest.TestCase):
@修补程序('example.func2')
def测试功能1(自身、模拟功能2):
def副作用(输入):
如果输入='1':
返回“a”
如果输入='2':
返回“b”
如果输入='3':
返回“c”
模拟功能2.副作用=副作用
实际值1=func1(['1'])
self.assertEqual(实际为“a”)
实际值2=func1(['2'])
self.assertEqual(实际值2,'b')
实际值3=func1(['3'])
self.assertEqual(实际值3,'c')
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
unittest.main()
单元测试结果:

⚡  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%
带有补丁(“your.module.func2”,副作用=[“fake-output-for-input-1”,“fake-output-for-input-2”):断言func1(示例)
。如果将值列表传递给
副作用
,它将在每次函数调用时迭代。请参阅中的更多示例。