简体   繁体   中英

Mocking a method outside of a class

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?

 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM