Mocking a method outside of a class

寵の児 提交于 2019-12-11 04:17:49

问题


I need to write a unit test for credential checking module looks something like below. I apologize I cannot copy the exact code.. but I tried my best to simplify as an example. I want to patch methodA so it returns False as a return value and test MyClass to see if it is throwing error. cred_check is the file name and MyClass is the class name. methodA is outside of MyClass and the return value checkedcredential is either True or False.

def methodA(username, password):
    #credential check logic here...
    #checkedcredential = True/False depending on the username+password combination
    return checkedcredential

class MyClass(wsgi.Middleware):
    def methodB(self, req): 
        username = req.retrieve[constants.USER]
        password = req.retrieve[constants.PW]
         if methodA(username,password):
            print(“passed”)
        else:
            print(“Not passed”)
            return http_exception...

The unit test I currently have looks like...

import unittest
import mock
import cred_check import MyClass

class TestMyClass(unittest.Testcase):
    @mock.patch('cred_check')
    def test_negative_cred(self, mock_A):
        mock_A.return_value = False
        #not sure what to do from this point....

The part I want to write in my unittest is return http_exception part. I am thinking of doing it by patching methodA to return False. After setting the return value, what would be the proper way of writing the unittest so it works as intended?


回答1:


What you need to do in your unittest to test http_exception return case is:

  1. patch cred_check.methodA to return False
  2. Instantiate a MyClass() object (you can also use a Mock instead)
  3. Call MyClass.methodB() where you can pass a MagicMock as request and check if the return value is an instance of http_exception

Your test become:

@mock.patch('cred_check.methodA', return_value=False, autospec=True)
def test_negative_cred(self, mock_A):
    obj = MyClass()
    #if obj is a Mock object use MyClass.methodB(obj, MagicMock()) instead
    response = obj.methodB(MagicMock()) 
    self.assertIsInstance(response, http_exception)
    #... and anything else you want to test on your response in that case



回答2:


 import unittest
 import mock
 import cred_check import MyClass

class TestMyClass(unittest.Testcase):
    @mock.patch('cred_check.methodA',return_value=False)
    @mock.patch.dict(req.retrieve,{'constants.USER':'user','constants.PW':'pw'})
    def test_negative_cred(self, mock_A,):
        obj=MyClass(#you need to send some object here)
        obj.methodB()

It should work this way.



来源:https://stackoverflow.com/questions/29018025/mocking-a-method-outside-of-a-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!