Getting started with API Automation: Part 1- Overview

Shashi Kumar Raja
TestCult
Published in
5 min readJul 26, 2018

So you want to start automating APIs but, every time you search google for best api automation tool, you see so many top 10 tools that you get confused and think of definitely doing it tomorrow πŸ’€

Let’s start by understanding what all things we need if we want to setup(hopefully tomorrow 😊 ) an API automation framework. Hold that thought a sec…I said stop thinking…, I know your boss ain’t allocating no penny for any paid tools. See, I am a mind reader, don’t think out loud 😼

Paid tools to be hanged, not executed:)

1. Where the heck am I supposed to start to write tests?

You will need something which exposes you to set of rules and guidelines to write tests and also enables you to do so by giving you access to several tools and practices. Rang a bell πŸ””, no!! Ok, I know what will trigger the bell.

Ever heard of TestNG, Junit, mocha, pytest, Robot, yes they are all Test Automation frameworks.

You need to find a suitable test framework based on your requirements-What existing tech stack your company uses?, how much automation do you want to do?, which language are you comfortable with, etc. You will find an automation framework in most of the popular languages which will enable you to write unit, functional and other kinds of testing for APIs.

To know more about test framework refer to this 2nd part of the series where I have explained Mocha and Pytest in detail.

2. How am I going to make the API calls in test framework?

Most of these frameworks have support for making API calls by including a HTTP request library, as REST APIs make use of HTTP protocol for communication.

Some frameworks like mocha give you the liberty to use HTTP request library of your choice like superagent.

request
.post('/api/pet')
.send({ name: 'Manny', species: 'cat' }) // sends a JSON post body
.set('X-API-Key', 'foobar')
.set('accept', 'json')
.end((err, res) => {
// Calling the end function will send the request
});

They give you easy support to call GET,PUT, POST,DELETE and all other methods.You can pass headers, cache, query parameters, you name it you got it πŸ‘†

3. Cool, but some of my APIs give JSON while others XML in response, how will I handle that?

Most of these HTTP request libraries will allow you to send and receive data in JSON, XML, CSV, Text, Image, form-data, encoded-data with several authorization standards supported.

They also allow to handle HTTP response status code and validate whether we got the required response status code or not.

  • Informational Response Codes (1xx)
    100 β€” Continue
    101 β€” Switching Protocols
    102 β€” Processing
  • Success Response Codes (2xx)
    200 β€” OK 206 β€” Partial Content
    201 β€” Created 207 β€” Multi-status
    202 β€” Accepted 208 β€” Already Reported
    203 β€” Non-authoritative Info 226 β€” IM Used
    204 β€” No Content 250 β€” Low Storage Space
    205 β€” Reset Content
  • Redirection Response Codes (3xx)
    300 β€” Multiple Choices 304 β€” Not Modified
    301 β€” Moved Permanently 305 β€” Use Proxy
    302 β€” Found 307 β€” Temporary Redirect
    303 β€” See Other 308 β€” Permanent Redirect
  • Client Error Response Codes (4xx)
    400 β€” Bad Request
    401 β€” Unauthorized
    403 β€” Forbidden
    404 β€” Not Found
    405 β€” Method Not Allowed
    406 β€”Not Acceptable
    412 β€” Precondition Failed , 415 β€” Unsupported Media Type
  • Server Error Response Codes (5xx)
    500 β€” Internal Server Error 508 β€” Loop Detected
    501 β€” Not Implemented 509 β€” Bandwidth Limited
    502 β€” Bad Gateway 510 β€” Not Extended
    503 β€” Service Unavailable 511 β€” Network Auth Requried
    504 β€” Gateway Timeout 550 β€” Permission Denied
    505 β€” HTTP Ver Not Supported 551 β€” Option Not Supported
    506 β€” Variant Also Negotiates 598 β€” Nework Read Timeout Error
    507 β€” Insufficient Storage 599 β€” Network Connect Timeout Error

4. Ok, but how will I handle test data?

Depends on where you get your test data from. These test frameworks will let you utilize all the features of the language they are based on-

a. Database: You can easily create db connections to read data.

b. External File: You can read the external text, JSON, CSV or any other files.

c. Random data: You can make use of libraries like faker to generate random test data on the fly.

var faker = require('faker');

var randomName = faker.name.findName(); // Rowan Nikolaus
var randomEmail = faker.internet.email(); // Kassandra.Haley@erich.biz
var randomCard = faker.helpers.createCard(); // random contact card containing many properties

d. Data from API response: Many times while testing you will require to pass response of one API as request data to another. You can do so by making use of hooks.You will get functions like Before, Before each, After, After each which as the name suggests get executed before/after any or all tests. Call API1 in before of API2, and pass its response to API2. Simple right!!! ☺️

5. Handling test data and making API calls seems easy,but how am I going to validate the API responses?

For validating the correctness of responses you need something called Assertion library. Many of the test frameworks come bundled with assertion libraries which gives you ability to write assertions in simple English like syntax. They also allow you to validate JSON Schema of your response.

In mocha, you can use assertion library of your choice like chai.

response.status.should.equal(200)
foo.should.be.a('string');
foo.should.have.lengthOf(3);
tea.should.have.property('flavors')
.with.lengthOf(3);

6. Awesome!! One last thing, I do all this testing but how to show my boss I did these and found issues?

Most of these frameworks will give you basic HTML report of the test run which you can download and share. If you want more beautiful reports with graphs and charts you can use several open-source reporting tools like allure, or mochawesome.

7. Only if I could get some boiler plate with these things to kickstart the API automation now

What did I tell you, you name it you got it πŸ‘‡

This is a boilerplate I have created for API automation using mocha in node.js.

It includes-

  • Test-framework(mocha),
  • HTTP request library(supertest),
  • Assertion library(chai),
  • Reporter(allure),
  • logger(winston),
  • Random data generator(faker),
  • Eslint support and
  • naughty string validation integrated. All you need to do is clone/download it and you are ready to go.

If you want the boiler plate in some other language, I can get it ready by tomorrow πŸ’€

If you liked this answer you can clap, it encourages me write more πŸ™

--

--