10 Steps How to Automate your API Testing Effectively~
Before reading this post, make sure you have at least one of these problems:
- Do you have trouble or still doing API testing manually?
- Are you working as QA/QE/TE/SET using Postman/Insomnia/Paw often to test your REST API endpoints because you don’t exactly know how to test your endpoints automatically?
- Are you working as QA/QE/TE/SET who wants to separate test from the app?
The main purpose of this post is to solve all the problems mentioned above by automating the API testing and integrate it with Jenkins CI. It’s easier than you think if you choose the right tools and techniques for that job.
- Chai: Unlike Jasmine, an additional assertion library must be used to supplement Mocha. Chai is an assertion library that lets you choose the assertion interface that you like best, including “assert”, “expect”, and “should”.
- SuperTest: SuperTest is an extension of SuperAgent, a lightweight HTTP AJAX request library. SuperTest provides high-level abstractions for testing node.js API endpoint responses with easy to understand assertions.
- Docker: Docker is an open platform for developers and sysadmins to build, ship, and run distributed applications, whether on laptops, data center VMs, or the cloud.
- Jenkins CI: Jenkins is an open source automation server written in Java. Jenkins helps to automate the non-human part of the whole software development process, with now common things like continuous integration, but by further empowering teams to implement the technical part of a Continuous Delivery.
This tutorial assumes you already have:
- Node.js and docker installed on your machine.
- App with the API that you want to test already running. (Here is the example app https://github.com/rifkyalikiki/example-api-app)
- Jenkins CI already installed.
LET’S GET STARTED
- Create your test project folder. For example:
package.jsonfile to add all dependencies.
3. Install all dependencies by running these commands.
npm install -g mocha
The node_modules folder should be added to your test project folder and all dependencies should be successfully installed.
4. Create test folder on your test project folder.
The directory must be called test for Mocha to find test files to run.
5. Create your first API test file
You can name your mocha files anything. however, if you are testing multiple endpoints that are associated with sets of models, I recommend naming them “yourModel_test.js”. For this example, just create a
user_test.js file within the test directory. Don’t forget to set your API url to a global variable as well. You will be calling this variable when you make your RESTful requests using SuperTest.
6. Let’s run your first API test by running these commands
JUNIT_REPORT_PATH=test-result/result.xml JUNIT_REPORT_STACK=1 mocha --timeout 25000 --colors --reporter mocha-jenkins-reporter
Here is the test result
7. Create entrypoint.sh to place your test runner command.
8. Create dockerfile that will be used on Jenkins CI.
9. Create your Jenkins Job Items
You can ignore the warning on the test result first, it will disappear after we run the job.
After that click SAVE button to create the job.
10. Run your Jenkins Job and see the test results.
My test results will be different from you because I already ran the job while I write this post.
Congratulation, you have successfully automated your API Testing now. Next posting I will give insight about my idea to create your own testing framework.