10 Steps How to Automate your API Testing Effectively~

Before reading this post, make sure you have at least one of these problems:

  1. Do you have trouble or still doing API testing manually?
  2. 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?
  3. 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.


PRE-REQUISITES

  1. Node.js: Node.js is a JavaScript runtime built on Chrome’s V8 JavaScript engine. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient. Node.js’ package ecosystem, npm, is the largest ecosystem of open source libraries in the world.
  2. Mocha: Mocha is a javascript testing framework that makes asynchronous testing easy.
  3. 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”.
  4. 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.
  5. 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.
  6. 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:

  1. Node.js and docker installed on your machine.
  2. App with the API that you want to test already running. (Here is the example app https://github.com/rifkyalikiki/example-api-app)
  3. Jenkins CI already installed.

LET’S GET STARTED

  1. Create your test project folder. For example: example-api-testing
  2. Create package.json file to add all dependencies.

3. Install all dependencies by running these commands.

cd your_test_project_folder
npm install -g mocha
npm install

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.

cd your_test_project_folder
mkdir test

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

cd your_test_project_folder
npm start
or
cd your_test_project_folder
JUNIT_REPORT_PATH=test-result/result.xml JUNIT_REPORT_STACK=1 mocha --timeout 25000 --colors --reporter mocha-jenkins-reporter

Here is the test result

test-results from terminal
result.xml

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

New Jenkins Job Item
Jenkins Job Item (1)
Jenkins Job Item (2)
Jenkins Job Item (3)

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.

Run the jenkins job by click the play button
Test result from the jenkins job

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.

Happy Testing!

Source: http://developmentnow.com/2015/02/05/make-your-node-js-api-bulletproof-how-to-test-with-mocha-chai-and-supertest/