2017-07-28 85 views
0

我想做一個單元測試來檢查這個python函數(調度)是否將正確的參數傳遞給deal_with_result。Python單元測試;如何獲取函數調用時傳遞的參數?

當在調度中調用函數deal_with_result時,是否有辦法「劫持」輸入參數?
我沒有修改調度功能中的代碼的權利。

這裏是想預覽我想在我的單元測試:

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) 

謝謝。

回答

0

我在找工作時一直在尋找答案,沒有找到任何答案。我建議你和老闆談談讓你修改基本功能,這樣你就不會浪費寶貴的時間和資源。

相關問題