[{"data":1,"prerenderedAt":66},["ShallowReactive",2],{"content-testing-python-starter-3-local-testing":3},{"markdown":4,"frontMatterAttributes":5,"bodyRaw":12,"contributorPaths":13,"menuType":16,"contentName":17,"slug":18,"readingTime":19,"menu":20,"nextDocItem":44,"previousDocItem":32},"\u003Ch2>Working with events\u003C\u002Fh2>\n\u003Cp>Testing event driven architectures often requires working with synthetic events. You can define events as JSON documents. Synthetic events are test data that represent AWS events such as a requests from API Gateway or messages from SQS. \u003C\u002Fp>\n\u003Cp>AWS Lambda always requires an event during invocation. This project includes a sample test event in the \u003Ccode>events\u003C\u002Fcode> folder. SAM provides the capability of generating additional synthetic events for a variety of AWS services. \u003Ca href=\"https:\u002F\u002Fdocs.aws.amazon.com\u002Fserverless-application-model\u002Flatest\u002Fdeveloperguide\u002Fsam-cli-command-reference-sam-local-generate-event.html\">Read the documentation\u003C\u002Fa>.\u003C\u002Fp>\n\u003Cp>Use the following command to learn more about generating synthetic events:\u003C\u002Fp>\n\u003Cpre>\u003Ccode class=\"language-bash\"># generate a synthetic event\n sam local generate-event\n\u003C\u002Fcode>\u003C\u002Fpre>\n\u003Ch2>Working with local emulators\u003C\u002Fh2>\n\u003Cp>Local emulation of AWS services offers a simple way to build and test cloud native applications using local resources. Local emulation can speed up the build and deploy cycle, creating faster feedback loops for application developers. \u003C\u002Fp>\n\u003Cp>Local emulation has several limitations. Cloud services evolve rapidly, so local emulators are unlikely to have feature parity with their counterpart services in the cloud. Local emulators may not provide an accurate representation of IAM permissions or service quotas. Local emulators do not exist for every AWS service.\u003C\u002Fp>\n\u003Cp>SAM provides local emulation features for \u003Ca href=\"https:\u002F\u002Fdocs.aws.amazon.com\u002Fserverless-application-model\u002Flatest\u002Fdeveloperguide\u002Fserverless-sam-cli-using-invoke.html\">AWS Lambda\u003C\u002Fa> and \u003Ca href=\"https:\u002F\u002Fdocs.aws.amazon.com\u002Fserverless-application-model\u002Flatest\u002Fdeveloperguide\u002Fserverless-sam-cli-using-start-api.html\">Amazon API Gateway\u003C\u002Fa>. AWS provides \u003Ca href=\"https:\u002F\u002Fdocs.aws.amazon.com\u002Famazondynamodb\u002Flatest\u002Fdeveloperguide\u002FDynamoDBLocal.html\">Amazon DynamoDB Local\u003C\u002Fa> and \u003Ca href=\"https:\u002F\u002Fdocs.aws.amazon.com\u002Fstep-functions\u002Flatest\u002Fdg\u002Fsfn-local.html\">AWS Step Functions Local\u003C\u002Fa>. Third-party vendors like \u003Ca href=\"https:\u002F\u002Fdocs.localstack.cloud\u002Foverview\u002F\">LocalStack\u003C\u002Fa> may provide emulation for additional AWS services. \u003C\u002Fp>\n\u003Cp>This project demonstrates local emulation of Lambda and API Gateway with SAM.\u003C\u002Fp>\n\u003Ch2>Use the SAM Lambda emulator\u003C\u002Fh2>\n\u003Cp>The SAM CLI can emulate a Lambda function inside a Docker container deployed to your local desktop. To use this feature, invoke the function with the \u003Ccode>sam local invoke\u003C\u002Fcode> command passing a synthetic event. Print statements log to standard out. \u003Ca href=\"https:\u002F\u002Fdocs.aws.amazon.com\u002Fserverless-application-model\u002Flatest\u002Fdeveloperguide\u002Fserverless-sam-cli-using-invoke.html\">Read the documentation\u003C\u002Fa>.\u003C\u002Fp>\n\u003Cpre>\u003Ccode class=\"language-bash\"># invoke a Lambda function locally\n sam local invoke PythonTestDemo --event events\u002Fevent.json\n\u003C\u002Fcode>\u003C\u002Fpre>\n\u003Cp>The \u003Ccode>sam local start-lambda\u003C\u002Fcode> command starts a local endpoint that emulates the AWS Lambda invoke endpoint. You can invoke it from your automated tests. Because this endpoint emulates the AWS Lambda invoke endpoint, you can write tests and then run them against the local Lambda emulator. You can also run the same tests against a deployed AWS SAM stack in your CI\u002FCD pipeline. \u003Ca href=\"https:\u002F\u002Fdocs.aws.amazon.com\u002Fserverless-application-model\u002Flatest\u002Fdeveloperguide\u002Fserverless-sam-cli-using-automated-tests.html\">Read the documentation\u003C\u002Fa>.\u003C\u002Fp>\n\u003Cpre>\u003Ccode class=\"language-bash\"># start a local emulator for a Lambda function endpoint\n sam local start-lambda --region us-east-1\n\u003C\u002Fcode>\u003C\u002Fpre>\n\u003Cpre>\u003Ccode class=\"language-bash\"># run a unit test in a separate terminal\n python3 -m pytest -s tests\u002Funit\u002Flocal_emulator_test.py -v\n\u003C\u002Fcode>\u003C\u002Fpre>\n\u003Ch2>Use the SAM API Gateway emulator\u003C\u002Fh2>\n\u003Cp>The SAM CLI can also emulate your application&#39;s API. Use the \u003Ccode>sam local start-api\u003C\u002Fcode> to run the API locally on port 3000.\u003C\u002Fp>\n\u003Cpre>\u003Ccode class=\"language-bash\"># start a local emulator for an API Gateway endpoint\n sam local start-api\n\u003C\u002Fcode>\u003C\u002Fpre>\n\u003Cpre>\u003Ccode class=\"language-bash\"># make a request to the endpoint in a separate terminal\n curl http:\u002F\u002Flocalhost:3000\u002Fhello\n\u003C\u002Fcode>\u003C\u002Fpre>\n\u003Cp>The SAM CLI reads the application template to determine the API&#39;s routes and the functions that they invoke. The \u003Ccode>Events\u003C\u002Fcode> property on each function&#39;s definition includes the route and method for each path.\u003C\u002Fp>\n\u003Cpre>\u003Ccode class=\"language-yaml\">      Events:\n        HelloWorld:\n          Type: Api\n          Properties:\n            Path: \u002Fhello\n            Method: get\n\u003C\u002Fcode>\u003C\u002Fpre>\n\u003Ch2>Run a unit test using a mock framework\u003C\u002Fh2>\n\u003Cp>Lambda functions frequently call other AWS or 3rd party services. Mock frameworks are useful in simulating service responses. Mock frameworks can speed the development process by enabling rapid feedback iterations. Mocks can be useful for testing failure cases when testing these branches of logic is difficult to do in the cloud.\u003C\u002Fp>\n\u003Cp>This project uses mocks to test the internal logic of a Lambda function. \nThe project uses the \u003Ca href=\"http:\u002F\u002Fdocs.getmoto.org\u002Fen\u002Flatest\u002F\">moto\u003C\u002Fa> dependency library to mock an external service call to Amazon S3. The \u003Ccode>moto\u003C\u002Fcode> library can simulate responses from a variety of \u003Ca href=\"http:\u002F\u002Fdocs.getmoto.org\u002Fen\u002Flatest\u002Fdocs\u002Fservices\u002Findex.html\">AWS services\u003C\u002Fa>. We defined tests with mocks in the \u003Ccode>tests\u002Funit\u003C\u002Fcode> folder. Use \u003Ccode>pip\u003C\u002Fcode> to install test dependencies and \u003Ccode>pytest\u003C\u002Fcode> to run the unit test.\u003C\u002Fp>\n\u003Cpre>\u003Ccode class=\"language-bash\"># Create and Activate a Python Virtual Environment\n pip3 install virtualenv\n python3 -m venv venv\n source .\u002Fvenv\u002Fbin\u002Factivate\n\n# install dependencies\n pip3 install -r tests\u002Frequirements.txt\n\n# run unit tests with mocks\n python3 -m pytest -s tests\u002Funit\u002Fmock_test.py -v\n\u003C\u002Fcode>\u003C\u002Fpre>\n",{"title":6,"order":7,"callout":8},"Local Testing",3,{"title":9,"description":10,"link":11},"Check out other starter guides","Do you prefer to learn in another language? We also have starter guides in Java, TypeScript, and .NET.","https:\u002F\u002Fgithub.com\u002Faws-samples\u002Fserverless-test-samples\u002F","## Working with events\nTesting event driven architectures often requires working with synthetic events. You can define events as JSON documents. Synthetic events are test data that represent AWS events such as a requests from API Gateway or messages from SQS. \n\nAWS Lambda always requires an event during invocation. This project includes a sample test event in the `events` folder. SAM provides the capability of generating additional synthetic events for a variety of AWS services. [Read the documentation](https:\u002F\u002Fdocs.aws.amazon.com\u002Fserverless-application-model\u002Flatest\u002Fdeveloperguide\u002Fsam-cli-command-reference-sam-local-generate-event.html).\n\nUse the following command to learn more about generating synthetic events:\n```bash\n# generate a synthetic event\n sam local generate-event\n```\n\n## Working with local emulators\nLocal emulation of AWS services offers a simple way to build and test cloud native applications using local resources. Local emulation can speed up the build and deploy cycle, creating faster feedback loops for application developers. \n\nLocal emulation has several limitations. Cloud services evolve rapidly, so local emulators are unlikely to have feature parity with their counterpart services in the cloud. Local emulators may not provide an accurate representation of IAM permissions or service quotas. Local emulators do not exist for every AWS service.\n\nSAM provides local emulation features for [AWS Lambda](https:\u002F\u002Fdocs.aws.amazon.com\u002Fserverless-application-model\u002Flatest\u002Fdeveloperguide\u002Fserverless-sam-cli-using-invoke.html) and [Amazon API Gateway](https:\u002F\u002Fdocs.aws.amazon.com\u002Fserverless-application-model\u002Flatest\u002Fdeveloperguide\u002Fserverless-sam-cli-using-start-api.html). AWS provides [Amazon DynamoDB Local](https:\u002F\u002Fdocs.aws.amazon.com\u002Famazondynamodb\u002Flatest\u002Fdeveloperguide\u002FDynamoDBLocal.html) and [AWS Step Functions Local](https:\u002F\u002Fdocs.aws.amazon.com\u002Fstep-functions\u002Flatest\u002Fdg\u002Fsfn-local.html). Third-party vendors like [LocalStack](https:\u002F\u002Fdocs.localstack.cloud\u002Foverview\u002F) may provide emulation for additional AWS services. \n\nThis project demonstrates local emulation of Lambda and API Gateway with SAM.\n\n## Use the SAM Lambda emulator \nThe SAM CLI can emulate a Lambda function inside a Docker container deployed to your local desktop. To use this feature, invoke the function with the `sam local invoke` command passing a synthetic event. Print statements log to standard out. [Read the documentation](https:\u002F\u002Fdocs.aws.amazon.com\u002Fserverless-application-model\u002Flatest\u002Fdeveloperguide\u002Fserverless-sam-cli-using-invoke.html).\n\n```bash\n# invoke a Lambda function locally\n sam local invoke PythonTestDemo --event events\u002Fevent.json\n```\n\nThe `sam local start-lambda` command starts a local endpoint that emulates the AWS Lambda invoke endpoint. You can invoke it from your automated tests. Because this endpoint emulates the AWS Lambda invoke endpoint, you can write tests and then run them against the local Lambda emulator. You can also run the same tests against a deployed AWS SAM stack in your CI\u002FCD pipeline. [Read the documentation](https:\u002F\u002Fdocs.aws.amazon.com\u002Fserverless-application-model\u002Flatest\u002Fdeveloperguide\u002Fserverless-sam-cli-using-automated-tests.html).\n\n```bash\n# start a local emulator for a Lambda function endpoint\n sam local start-lambda --region us-east-1\n```\n\n```bash\n# run a unit test in a separate terminal\n python3 -m pytest -s tests\u002Funit\u002Flocal_emulator_test.py -v\n```\n\n## Use the SAM API Gateway emulator\nThe SAM CLI can also emulate your application's API. Use the `sam local start-api` to run the API locally on port 3000.\n\n```bash\n# start a local emulator for an API Gateway endpoint\n sam local start-api\n```\n\n```bash\n# make a request to the endpoint in a separate terminal\n curl http:\u002F\u002Flocalhost:3000\u002Fhello\n```\n\nThe SAM CLI reads the application template to determine the API's routes and the functions that they invoke. The `Events` property on each function's definition includes the route and method for each path.\n\n```yaml\n      Events:\n        HelloWorld:\n          Type: Api\n          Properties:\n            Path: \u002Fhello\n            Method: get\n```\n\n## Run a unit test using a mock framework\nLambda functions frequently call other AWS or 3rd party services. Mock frameworks are useful in simulating service responses. Mock frameworks can speed the development process by enabling rapid feedback iterations. Mocks can be useful for testing failure cases when testing these branches of logic is difficult to do in the cloud.\n\nThis project uses mocks to test the internal logic of a Lambda function. \nThe project uses the [moto](http:\u002F\u002Fdocs.getmoto.org\u002Fen\u002Flatest\u002F) dependency library to mock an external service call to Amazon S3. The `moto` library can simulate responses from a variety of [AWS services](http:\u002F\u002Fdocs.getmoto.org\u002Fen\u002Flatest\u002Fdocs\u002Fservices\u002Findex.html). We defined tests with mocks in the `tests\u002Funit` folder. Use `pip` to install test dependencies and `pytest` to run the unit test.\n\n```bash\n# Create and Activate a Python Virtual Environment\n pip3 install virtualenv\n python3 -m venv venv\n source .\u002Fvenv\u002Fbin\u002Factivate\n\n# install dependencies\n pip3 install -r tests\u002Frequirements.txt\n\n# run unit tests with mocks\n python3 -m pytest -s tests\u002Funit\u002Fmock_test.py -v\n```\n\n",[14,15],"content\u002Fcontributors\u002Fdan-fox.json","content\u002Fcontributors\u002Frohan-mehta.json","LIST","Python Test Samples Starter Project","\u002Fcontent\u002Ftesting\u002Fpython-starter\u002F3-local-testing","4 min",[21],{"title":22,"content":23},"Testing Guide",[24,32,40,44,52,59],{"title":25,"order":26,"callout":27,"time":28,"path":29,"id":30,"link":31},"Introduction",1,{"title":9,"description":10,"link":11},"1 min","1-introduction","1-introduction.md","\u002Fcontent\u002Ftesting\u002Fpython-starter\u002F1-introduction",{"title":33,"order":34,"callout":35,"time":36,"path":37,"id":38,"link":39},"Project Setup",2,{"title":9,"description":10,"link":11},"3 min","2-project-setup","2-project-setup.md","\u002Fcontent\u002Ftesting\u002Fpython-starter\u002F2-project-setup",{"title":6,"order":7,"callout":41,"time":19,"path":42,"id":43,"link":18},{"title":9,"description":10,"link":11},"3-local-testing","3-local-testing.md",{"title":45,"order":46,"callout":47,"time":48,"path":49,"id":50,"link":51},"Testing in the Cloud",4,{"title":9,"description":10,"link":11},"2 min","4-testing-in-the-cloud","4-testing-in-the-cloud.md","\u002Fcontent\u002Ftesting\u002Fpython-starter\u002F4-testing-in-the-cloud",{"title":53,"order":54,"callout":55,"time":48,"path":56,"id":57,"link":58},"Observability in Testing",5,{"title":9,"description":10,"link":11},"5-observability-in-testing","5-observability-in-testing.md","\u002Fcontent\u002Ftesting\u002Fpython-starter\u002F5-observability-in-testing",{"title":60,"order":61,"callout":62,"time":28,"path":63,"id":64,"link":65},"Cleanup",6,{"title":9,"description":10,"link":11},"6-cleanup","6-cleanup.md","\u002Fcontent\u002Ftesting\u002Fpython-starter\u002F6-cleanup",1792578875024]