Basic Integration Testing with Serverless Framework
Tags: serverless, serverlessframework, testing, cloud
Published: 2022-04-28 16:19:54

Originally posted at [Serverless](https://www.serverless.com/blog/serverless-test-framework) on July 10th, 2019

![](https://cdn-images-1.medium.com/max/5200/0*Bl_AGkk0rUks8AB2.png)

With the latest Serverless Framework release, we made it easier to test APIs built with the Serverless Framework.

Testing is important! We do it all the time when getting started developing a new service with the Serverless Framework. We might not have unit tests or a CI/CD in place yet, but at a minimum we will run serverless invoke or curl a few times with various inputs to make sure our APIs don't return 500s. After hitting [up] & [enter] in the terminal once too many times we begin to wonder if there is a better way.

Yes, you should probably write some unit tests. When you are ready for that, here is an in depth guide on [writing unit tests for Node.js Serverless projects with Jest](https://serverless.com/blog/unit-testing-nodejs-serverless-jest/). Once you have the coverage and maturity you need, you’ll also need some integration tests. However, integration tests can be complicated and time consuming to implement. So if you need some basic integration tests in the meantime but you are not ready to commit to writing integration tests, then let me introduce you to a new testing tool in Serverless Framework to add to your testing arsenal.

Serverless Framework provides a new way to define basic integration tests for functions with HTTP endpoints. It’s goal is to enable you to test your serverless applications without having to manually write a lot of code to do so. Tests are defined in a new file, serverless.test.yml and they are tested using the serverless test command. The [testing documentation](https://github.com/serverless/enterprise/blob/master/docs/testing.md) goes into detail about writing tests but here is a quick preview.

As an example let’s look at this handler.js and serverless.yml file.

**handler.js**
{% gist https://gist.github.com/richardgrantserverless/eb765218d2c1db9b56401ea7f6151714.js %}
This function will return hello, <name> if a name was provided in the JSON request body, and hello, world if the name wasn't set.

Here is the serverless.yml for this function with an HTTP POST endpoint.

**serverless.yml**
{% gist https://gist.github.com/richardgrantserverless/e5e248b8c941a0c32f4c53f84a4c36a2.js %}
Go ahead and deploy the service using sls deploy. Now let's write some basic integration tests in a new serverless.test.yml file.

**serverless.test.yml**
{% gist https://gist.github.com/richardgrantserverless/69f0ea8850b16f2a964e6ead94ce7068.js %}
Once the service is deployed run sls test and you can expect output like this:
{% gist https://gist.github.com/richardgrantserverless/b967db7e24b3a3151ade160c809c868f.js %}
As you can see, this light-weight test framework enables us to write some basic integration tests and run them on the live service. The [docs](https://github.com/serverless/enterprise/blob/master/docs/testing.md#serverlesstestyml-specification) show you how to send HTTP headers, JSON bodies, string bodies, and submit forms. You can also test the responses for HTTP response codes, string bodies, or JSON content.

*Originally published at [https://www.serverless.com](https://www.serverless.com/blog/serverless-test-framework).*