Writing unit tests for Chalice
By Brian
Chalice is a Python serverless microframework for AWS that enables you to quickly create and deploy applications that use Amazon API Gateway and AWS Lambda. In this blog post, I discuss how to create unit tests for Chalice. I’ll use Chalice local mode to execute these tests without provisioning API Gateway and Lambda resources.
Creating a new project
Let’s begin by creating a new Chalice project using the chalice command line.
Note: You might want to create a virtual environment to complete the tasks in this post.
|
|
As you can see, this creates a simple application with a few sample functions. Notice that there are two functions, hello_name and create_user, which are commented out in the sample code. Open app.py in a text editor and uncomment those lines. The complete application should look like this.
|
|
Chalice local mode
As you develop your application, you can experiment locally before deploying your changes. You can use the chalice local command to spin up a local HTTP server, as follows.
|
|
Now we can test our application using cURL.
Note: You will need to start a second shell.
|
|
Local mode is great for interactive testing, but we want to automate testing to ensure all our tests run regularly. Next, I’ll create a Python unit test to automate testing.
Writing a unit test
pytest is a framework that makes it easy to write unit tests. If you don’t already have pytest installed, you can install it using pip.
|
|
I’ll add a new module named app_test.py to my project, with the following content.
|
|
Let’s examine the code. First, I defined a fixture named gateway_factory, that creates a new local gateway we can use in our test cases. This is the Python equivalent of the chalice local command line we ran earlier. Note that the Config and LocalGateway objects used inside the factory are private and may change in the future.
Next, I defined new class named TestChalice with a single test named test_index. test_index submits a GET request to our application and verifies that I receive an expected response. This is the equivalent of the curl command that we tested earlier. You execute the unit test with the pytest command.
|
|
Adding tests
Let’s add a few more tests to validate the other functions in our project. test_hello expects to receive a name of the person to say hello to in the path. Here is a test case that passes “alice” and ensures that the response is correct.
|
|
Unlike the previous functions, test_users uses the POST verb instead of GET. POST requests require a body and a content-type header to tell Chalice the type of data you’re sending.
|
|
Now, when we run pytest, our three tests are all run and all three pass.
|
|
Your tests can now be added to a continuous deployment (CD) pipeline. The pipeline can run tests on code changes and, if they pass, promote the new build to a testing stage. Chalice can generate a CloudFormation template that will create a starter CD pipeline. It contains a CodeCommit repo, a CodeBuild stage for packaging your chalice app, and a CodePipeline stage to deploy your application using CloudFormation. For more information, see the chalice generate-pipeline command in the Chalice Documentation.
Conclusion
Using Chalice, you can quickly create and deploy serverless applications. In addition, Chalice’s local mode enables you to easily create unit tests for your projects. Finally, Chalice can generate a continuous deployment to automate testing.