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!')
            }
mark liberhman
  • 101
  • 1
  • 11
  • Check out https://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. https://aws.amazon.com/serverless/sam/ – Hayden Feb 19 '20 at 03:08
  • I need to run my tests in AWS Lambda and watch the output in Cloud watch,above project doesnt serves the purpose – mark liberhman Feb 19 '20 at 03:11

1 Answers1

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)
Mark B
  • 183,023
  • 24
  • 297
  • 295
  • 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... – Mark B Feb 19 '20 at 15:12
  • 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 – mark liberhman Feb 19 '20 at 17:31
  • 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. https://stackoverflow.com/questions/40741282/cannot-use-requests-module-on-aws-lambda – Mark B Feb 19 '20 at 17:33