Document APIs with Swagger (4/4) — The API test

In this series of articles, I will introduce the different Swagger tools. Swagger is a very handy tool for API documentation. It allows you to generate “living” documentation, so that the documentation is always up to date, which is very difficult to do without this kind of tool. It also allows to generate code automatically, allowing the developer to focus on his core business. Finally, it is based on an open source specification format.

These articles focus on Swagger 2.0, while the 3.0 version (“OpenAPI 3.0.0”) is available.

This article is thus broken down into four parts :

Basic API testing

To test the implementation of the Swagger specifications, you can use Dredd (https://dredd.org).

To install Dredd:

Let’s run Dredd on our specification file:

Creating a Mock Server

You can easily create a realistic Mock server with Prism.

Install Prism:

Let’s launch the Mock server:

Advanced API testing

To perform advanced testing of the API, we will use:

  • Mocha: it is a Javascript testing framework that facilitates asynchronous testing,
  • Chai: is an assertion library providing assert, expect and should methods,
  • SuperTest: is a library providing a high level abstraction for testing NodeJS API endpoint responses.

Let’s install these three libraries :

In the project directory, let’s create a test file:

Now let’s run the tests:

Creating a Docker Image

Since we are now going to connect to the API web server from the container and not from the host, you need to change the API IP address in the Swagger specification file replacing the line:

by:

XX.XX.XX.XX being the IP address of your host.

Create the Dockerfile file which allows you to generate a Docker:

Add an entry point shell:

Generate the Docker image:

To use the Docker image, simply proceed as follows:

We have now discussed several approaches to testing Swagger specifications and their implementation.

More articles on my blog http://www.DevOpsTestLab.com.

My DevOpsTestLab Youtube channel.

My LinkedIn profile: https://fr.linkedin.com/in/brunodelb

Interests in the full lifecycle: design, Agile Coaching, development, testing, DevOps, Cloud, Management 3.0, ITIL. It defines me.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store