1

Basically I have written API tests using Python REST API test suite, below tests runs fine on my local environment ,here are the tests

import requests
import json

def test_post_headers_body_json():
 url = 'https://httpbin.org/post'

  # Additional headers.
 headers = {'Content-Type': 'application/json' } 

  # Body
 payload = {'key1': 1, 'key2': 'value2'}

  # convert dict to json by json.dumps() for body data. 
   resp = requests.post(url, data = json.dumps(payload,indent=4))       

  # Validate response headers and body contents, e.g. status code.
    assert resp.status_code == 200
    resp_body = resp.json()
 assert resp_body['url'] == url

    # print response full body as text
    print(resp.text)

Now to run tests at my local all I need to do is just open a command prompt and type pytest in the script folder, and you will get a test result as follows.

   pytest
   ================ test session starts =======================

But how can i run the same python test suite written above in AWS LAMBDA environment because AWS lambda has below handler code which is different from what i have above ,how can I include my code here in the AWS lambda handler code?

    import json

    def lambda_handler(event, context):
      # TODO implement
     return {
          'statusCode': 200,
           'body': json.dumps('Hello from Lambda!')
            }
2
  • Check out github.com/lambci/lambci, it's docker containers that will simulate running the Lambda as it were in AWS, where you can pass different types of events to it to test how it responds. AWS SAM project utilizes this as well. aws.amazon.com/serverless/sam Commented Feb 19, 2020 at 3:08
  • I need to run my tests in AWS Lambda and watch the output in Cloud watch,above project doesnt serves the purpose Commented Feb 19, 2020 at 3:11

1 Answer 1

2

In your Lambda function code, replace:

# TODO implement

with the following call:

test_post_headers_body_json()

The entire thing would look something like the following:

import json
import requests

def lambda_handler(event, context):
  test_post_headers_body_json()
  return {
      'statusCode': 200,
       'body': json.dumps('Hello from Lambda!')
        }

def test_post_headers_body_json():
  url = 'https://httpbin.org/post'

  # Additional headers.
  headers = {'Content-Type': 'application/json' } 

  # Body
  payload = {'key1': 1, 'key2': 'value2'}

  # convert dict to json by json.dumps() for body data. 
  resp = requests.post(url, data = json.dumps(payload,indent=4))       

  # Validate response headers and body contents, e.g. status code.
  assert resp.status_code == 200
  resp_body = resp.json()
  assert resp_body['url'] == url

  # print response full body as text
  print(resp.text)
Sign up to request clarification or add additional context in comments.

3 Comments

When the Lambda function runs, lambda_handler will be called, which in turn will call test_post_headers_body_json. So to "trigger this code" you would simply invoke the Lambda function...
Sorry for asking again ,why i am getting Unable to import error . START RequestId: 7cb8a650-24e4-4cee-969d-1527afdf3361 Version: $LATEST [ERROR] Runtime.ImportModuleError: Unable to import module 'lambda_function': No module named 'requests' END RequestId: 7cb8a650-24e4-4cee-969d-1527afdf3361 REPORT RequestId: 7cb8a650-24e4-4cee-969d-1527afdf3361 Duration: 1.63 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 55 MB Init Duration: 128.86 ms
AWS Lambda doesn't include third-party dependencies in your environment automatically. You have to package them with your Lambda function deployment, or include a Lambda layer that provides them. stackoverflow.com/questions/40741282/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.