Python单元测试;如何获取调用函数时传递的参数?
Python Unittest; How to get the parameters passed when the function is called?
我正在尝试进行单元测试以检查此 python 函数(调度)是否将正确的参数传递给 deal_with_result。
在dispatch调用函数deal_with_result时,有没有办法"hijack"输入参数?
我无权修改调度函数中的代码。
这是我在单元测试中想要的预览:
import maker
from operators import op_morph
import unittest
from unittest.mock import Mock
import cv2
img = cv2.imread("/img_tests/unitTestBaseImage.png")
def my_side_effect(*args, **kwargs):
global img
print(args)
print(kwargs)
if args!=None:
if len(args)>0:
if args[0] == img:
return 0
return 3
else:
return 2
return 1
class TestCalls(unittest.TestCase):
def test_dispatch(self):
global img
makerMock = Mock()
makerMock.deal_with_result.side_effect = my_side_effect
#calling the dispatch function
maker.dispatch(["json_example.json"])
#instead of passing this mock I want to get the real parameters passed
#when the function deal_with_result is called in dispatch.
temp=makerMock.deal_with_result("img")
print("image return code: "+str(temp))
if __name__ == '__main__':
unittest.main(exit=False)
谢谢。
我一直在寻找这个问题的答案,但 job.Haven 没有找到任何相关信息。我建议你说服你的老板让你修改基本功能,这样你就不会浪费宝贵的时间和资源。
我正在尝试进行单元测试以检查此 python 函数(调度)是否将正确的参数传递给 deal_with_result。
在dispatch调用函数deal_with_result时,有没有办法"hijack"输入参数?
我无权修改调度函数中的代码。
这是我在单元测试中想要的预览:
import maker
from operators import op_morph
import unittest
from unittest.mock import Mock
import cv2
img = cv2.imread("/img_tests/unitTestBaseImage.png")
def my_side_effect(*args, **kwargs):
global img
print(args)
print(kwargs)
if args!=None:
if len(args)>0:
if args[0] == img:
return 0
return 3
else:
return 2
return 1
class TestCalls(unittest.TestCase):
def test_dispatch(self):
global img
makerMock = Mock()
makerMock.deal_with_result.side_effect = my_side_effect
#calling the dispatch function
maker.dispatch(["json_example.json"])
#instead of passing this mock I want to get the real parameters passed
#when the function deal_with_result is called in dispatch.
temp=makerMock.deal_with_result("img")
print("image return code: "+str(temp))
if __name__ == '__main__':
unittest.main(exit=False)
谢谢。
我一直在寻找这个问题的答案,但 job.Haven 没有找到任何相关信息。我建议你说服你的老板让你修改基本功能,这样你就不会浪费宝贵的时间和资源。