Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.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使用不同的参数模拟函数,并使用返回值进行断言_Python_Python 3.x_Mocking - Fatal编程技术网

python使用不同的参数模拟函数,并使用返回值进行断言

python使用不同的参数模拟函数,并使用返回值进行断言,python,python-3.x,mocking,Python,Python 3.x,Mocking,这可能是一个简单的问题,但我对模拟测试还不熟悉,我无法理解如何让它工作 我有这样的代码 def check_item(item): <do some processing> return processed_item 不确定,如果我做得对(或正确的方式)。如何检查多个参数,每个参数的返回值不同?要测试是否调用了方法: check_item.assert_called_once_with("apple") 您应该首先调用函数: @patch("my_module.c

这可能是一个简单的问题,但我对模拟测试还不熟悉,我无法理解如何让它工作

我有这样的代码

def check_item(item):
    <do some processing>
    return processed_item

不确定,如果我做得对(或正确的方式)。如何检查多个参数,每个参数的返回值不同?

要测试是否调用了方法:

check_item.assert_called_once_with("apple")
您应该首先调用函数:

@patch("my_module.check_item")
def test_check_item(self, check_item):
   check_item("apple")
   check_item.assert_called_once_with("apple")
   check_item.return_value = 'processed_apple'
但是在代码中,我看不出有任何理由这样测试它,因为它只是测试模拟模块

如果要测试函数返回值,则不应模拟函数,而应保持原样,只针对不同的场景进行测试。例如:

def test_check_item(self):
   result = check_item("apple")
   self.assertEqual(result, "some predefined result")
单元测试的目的是测试方法/类的正确行为

def test_check_item(self):
   result = check_item("apple")
   self.assertEqual(result, "some predefined result")