Reputation: 111
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!')
}
Upvotes: 1
Views: 1444
Reputation: 200501
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)
Upvotes: 2