DVA-C02 Question 51
Select 3You are developing a serverless application using AWS Lambda and want to ensure your code is well-tested before deployment. You decide to write unit tests for your Lambda function. Which of the following practices will help you achieve effective unit testing for the function?
- A
Mock any AWS service calls used in the Lambda function.
- B
Write tests that depend on live AWS services to ensure real-world accuracy.
- C
Keep the unit test logic focused on a single function or module.
- D
Hardcode AWS credentials in the unit test to authenticate API calls.
- E
Use a local testing framework, such as pytest or unittest, to validate the function's logic.
Show answer and explanation
Correct answers: A, C, E
Explanation
Effective unit testing for AWS Lambda functions involves isolating the function's logic from external dependencies like AWS services. Mocking AWS service calls, focusing tests on single functions or modules, and using local testing frameworks ensure that tests are fast, predictable, and maintainable. Avoid depending on live AWS services or hardcoding sensitive information to maintain security and reliability.
- A. Correct.
Correct. Mocking AWS service calls isolates the function's logic and ensures tests focus solely on the code, not external dependencies.
- B. Incorrect.
Incorrect. Unit tests should avoid dependencies on live services to ensure they are fast, reliable, and do not incur costs or require network access.
- C. Correct.
Correct. Unit tests should test a small, focused piece of logic without external factors, ensuring clarity and maintainability.
- D. Incorrect.
Incorrect. Hardcoding AWS credentials is a security risk and not aligned with best practices for unit testing.
- E. Correct.
Correct. Local testing frameworks such as pytest or unittest are commonly used to validate Lambda function logic in isolation.