How can I conduct a local keyword test for the lambda function before deploying it to the cloud?

193    Asked by debbieJha in AWS , Asked on May 7, 2024

 I am a software developer and I am currently working on a team project that involves the implementation of a lambda function for a cloud-based application. My task is to conduct local keyword tests for one of the lambda functions before deploying it to the cloud. How can I approach this situation, considering factors like test coverage, data isolation, and debugging capabilities? 

 In the context of AWS, you can conduct the local keyword for a lambda function by using the steps which are given below:-


Setup local development environment

Install the AWS CLI and then configure it with your credentials.

Set up the local development with node.js or python depending on which language you are using.

Creating test cases

Write the test cases by using the testing framework like jest for node.js or pytest for Python.

Try to ensure comprehensive coverage of different input scenarios and edge cases.

Mock AWS services

You can use the tools like AWS SDK mock to mock the AWS services locally.

Invoking the lambda function locally

You can use the AWS CLI or local testing tools like AWS SAM for invoking your lambda function locally.

Here is the example given of Node.js by using the jest for testing a lambda function that adds two numbers:-

// lambda.js (Lambda function code)
Exports.handler = async (event) => {
    Const { num1, num2 } = event;
    Const sum = num1 + num2;
    Return { result: sum };
};
// lambda.test.js (Jest test cases)
Const lambdaHandler = require(‘./lambda’);
Test(‘Adds two numbers correctly’, async () => {
    Const event = { num1: 5, num2: 10 };
    Const result = await lambdaHandler.handler(event);
    Expect(result.result).toBe(15);
});
Test(‘Handles negative numbers’, async () => {
    Const event = { num1: -8, num2: 3 };
    Const result = await lambdaHandler.handler(event);
    Expect(result.result).toBe(-5);
});

Here is another example given of pytest by using Python programming language:-

# test_lambda_function.py (Pytest test cases)Import pytest
Import boto3
From moto import mock_lambda
From lambda_function import lambda_handler
@pytest.fixture(scope=”module”)
Def lambda_client():
    With mock_lambda():        Yield boto3.client(‘lambda’, region_name=’us-east-1’)
@pytest.fixture
Def lambda_event():
    Return {‘num1’: 5, ‘num2’: 10}
Def test_add_numbers(lambda_client, lambda_event):
    Response = lambda_handler(lambda_event, None)
    Assert response[‘result’] == 15
Def test_negative_numbers(lambda_client, lambda_event):
    Lambda_event[‘num1’] = -8
    Lambda_event[‘num2’] = 3
    Response = lambda_handler(lambda_event, None)
    Assert response[‘result’] == -5
@pytest.mark.parametrize(“num1, num2, expected_result”, [
    (3, 7, 10),
    (-10, 5, -5),
    (0, 0, 0)
])

Def test_parameterized_add(lambda_client, num1, num2, expected_result):

    Event = {‘num1’: num1, ‘num2’: num2}

    Response = lambda_handler(event, None)

    Assert response[‘result’] == expected_result



Your Answer

Interviews

Parent Categories