Search code examples
pythontestingboto3patchpython-mock

How to patch a function imported from another module


Not able to patch the function.

I am trying to patch a function from another module in my test function. But I am not able to do that.

response.py

import boto3
lambda = boto3.client('lambda')

def response(event,context):
      s = boto3.client('s')



test_response.py

class lambda_handler(unittest.Testcae):
      @patch('response.boto3')
      test_handler():
       #doing the necessary assertions.

But the above mentioned one gives me the following error

botocore.exceptions.NoRegionError: You must specify a region.

The error is shown for the boto3.client declared outside the response function. Please help me in resolving this issue.


Solution

  • First of all fix the typo in here class lambda_handler(unittest.Testcae): on Testcase Also boto3.client needs a parameter region. i.e.:

    import boto3
    lambda = boto3.client('lambda', region_name='us-west-2')
    
    def response(event,context):
          s = boto3.client('s')