If you have an API running in production, you probably have a lot of peripheral needs around it such as generating client SDKs for your API consumers, maintaining API documentation, and testing your API.
A lot of this can be automated, and the modern approach is to do this in a CI/CD environment.
In part one of this blog post, we described how to create automated jobs around your API contract. In this follow-up we’ll focus on automated testing with Restlet Client.
Restlet Client allows you to write powerful API tests from its UI. What we’ve done now is remove manual steps in the process that takes you from writing tests in the UI to running them in your CI environment.
With the new GitHub integration, we’ve made it extra-simple to automate your tests. You can now push your test files directly to GitHub and have your test jobs retrieve them automatically so that there’s no manual update required anymore.
To use the integration, you will first need to link your GitHub account to your Restlet account. Once that is done, everything you push to GitHub from Restlet Client will be pushed from your GitHub account, thus providing fully-functional versioning of your test files.
Link your project to a repository
Once your GitHub account is linked, you need to link your test project to a GitHub repository. To do so, open the project you want to link and click on the “Link your repository” button to the right of the screen.
You can select the GitHub repository to which you want to push your project test file as well as a branch and path.
In the example above, my test file will be pushed to email@example.com:restlet/star-wars-api.git on branch 1.2.17 in the folder src/main/resources.
Note: We will warn you if the repository you are pushing to is public. Make sure there is no sensitive information in your project and environments before pushing anything in this case!
Restlet Client helps you bootstrap your test project by creating and pushing a test configuration file too. The only thing you have to do is register your Restlet license (see CI-side section) and having Java and Maven setup on your CI machine. If you want detailed instructions on how to run the tests, please refer to the our detailed tutorials on how to integrate with Travis, Circle CI and Jenkins.
Push your project to GitHub
You are all set now, you just need to open your project and click on the GitHub button (that now says ‘Push’) to push your project to GitHub.
On the CI-side
On your CI server, you can now create a pipeline to test your API. You can create the kind of workflow that makes sense for you. Here we’ll quickly describe two standard workflows to help you get started.
Basic workflow: test your main branch once a day
One simple and typical setup is to test your API on a staging environment. This test job can be run on demand or be scheduled to run every night. The main drawback of that solution is that you need a dedicated environment to run your tests.
Just create a CRON task that will run once a night and make it:
- pull the API code and test file from GitHub
- build and unit test your API code
- deploy it to your staging environment
- run your API tests against it with our maven plugin (learn more in our user guide)
- generate test reports
Advanced workflow: check your API is OK on each commit
A more advanced solution is to deploy your API in a short-lived container for each job run. That allows you to work in a compartmentalized environment and test multiple branches at the same time which in turn allows you to test every commit.
Just create a job that:
- runs everytime a new commit is pushed to GitHub
- builds and unit tests the API code
- deploys your API in a container
- runs your API tests against it with our maven plugin (learn more in the user guide)
- generate test reports and send the branch status back to GitHub.
If you want to learn more, our user guide contains additional resources to help you automate your API tests the best possible way.