API, Postman, Newman – Create a quick API with json-server and Faker and then test it with Chai-JS, Postman and Newman

It is true I have given a title to this post that sounds like a user story of somebody who want to make name dropping. In a sense, it is true but it is for SEO purpose. The story behind is absolutely genuine : I wanted to explore testing on an API with the help of Postman and Chai! But as usual, this question is followed by another one: what API can I test? Some posts quoted below goes for an existing API such as Twitter for instance. As one thing led to another, I have decided to first build an API with the help of json-server and faker and then test this freshly born API with Chai-JS, Postman. I added at the very end Newman, just to have the cool triptych, a kind of holy trinity of API testing 🙂

The code is on github @https://github.com/bflaven/BlogArticlesExamples/tree/master/to-test-a-mockingapi

Build your API

I found the easiest way to build-up an API with json-server. It is straight forward, the downside is that API is very primitive.

The idea is to installed json-server and create a dir for you API, then you create a json file that will « host » the datas. In my case, the file is called users_russian.json.

The fun part came with the usage of Faker, it is always fun to generate fake content especially in foreign language. I have decided to go for Russian.

1. Just to install json-server

npm install -g json-server

2. Create a directory for your future API. Mine is called to-test-a-mockingapi

mkdir to-test-a-mockingapi

3. Get in the directory

cd /[to-your-dir]/to-test-a-mockingapi

4. Create a json file. Mine is users_russian.json

touch users_russian.json

5. Add one line to users_russian.json

	{
  "users": [
    { "id": 1, "first_name": "Лука", "last_name": "Соловьев",  "city": "Натальяport", "email": "Соло51@hotmail.com" }
  ]
}

6. To load the json into the json server:

json-server users_russian.json

After these 6 steps, you have a fully functional API, able to make basic CRUD operations (Create, Read, Update, and Delete)

Let’s have fun with Faker

The idea is to generate a lot a fake content : first to avoid implementing real data, second avoid manually populating data in your API, this is exhausting.

Install faker to generate fake content

npm install faker

Generate fake content in Russian

node users_russian.js

Generate fake content in Russian into a json file that will be loaded in json-server

node users_russian.js > users_russian.json

The content of JS script for node, users_russian.js

//  users_russian.js
 
var faker = require('faker');
 
/*
Check https://github.com/marak/Faker.js/ for Localization
*/
// sets locale
// faker.locale = "ru" // (Russian)
// faker.locale = "pt_BR" // (Português do Brasil)
// faker.locale = "it" // (Italian)
// faker.locale = "fr" // (French)
// faker.locale = "es" // (Spanish)
 
 
faker.locale = "ru"; // (Russian)
 
var db = { users: [] };
 
for (var i=1; i<=1000; i++) {
  db.users.push({
            id: i,
            first_name: faker.name.firstName(),
            last_name: faker.name.lastName(),
            city: faker.address.city(),
            email: faker.internet.email()
  });
}
console.log(JSON.stringify(db));

The CRUD actions are still working but you have much more records…

POST in postman

{"id": 1001, "first_name": "Татьяна", "last_name": "Сафонова", "city": "Кузьминside", "email": "Татьяна81@gmail.com"}

PUT in postman for http://localhost:3000/users/1001

{"id": 1001, "first_name": "Никита", "last_name": "Юдина", "city": "New Лариса", "email": "Никита_40@yahoo.com"}

Sample of Russian users made with Faker
API, Postman, Newman - Create a quick API with json-server and Faker and then test it with Chai-JS, Postman and Newman

Start json-server with users_russian.json
API, Postman, Newman - Create a quick API with json-server and Faker and then test it with Chai-JS, Postman and Newman

Show me the fresh API in a browser
API, Postman, Newman - Create a quick API with json-server and Faker and then test it with Chai-JS, Postman and Newman

Show me the fresh API in a Postman
API, Postman, Newman - Create a quick API with json-server and Faker and then test it with Chai-JS, Postman and Newman

The CRUD operations: GET one record of the API via Postman
API, Postman, Newman - Create a quick API with json-server and Faker and then test it with Chai-JS, Postman and Newman

The CRUD operations: POST one record of the API via Postman
API, Postman, Newman - Create a quick API with json-server and Faker and then test it with Chai-JS, Postman and Newman

The CRUD operations: DELETE one record of the API via Postman
API, Postman, Newman - Create a quick API with json-server and Faker and then test it with Chai-JS, Postman and Newman

The CRUD operations: PUT (update) one record of the API via Postman
API, Postman, Newman - Create a quick API with json-server and Faker and then test it with Chai-JS, Postman and Newman

Check the activity of the json-server via the console
API, Postman, Newman - Create a quick API with json-server and Faker and then test it with Chai-JS, Postman and Newman

You have fully functional API with all the CRUD operations. Let’s make some test now with Chai.

Test with Chai

You need to create an development environment (DEV ENV) in Postman. Very easy, just enter this variable and make the query the first of your test.

I have called the DEV ENV postman_bdd_env

postman_bdd_path: http://bigstickcarpet.com/postman-bdd/dist/postman-bdd.min.js

Source : https://www.npmjs.com/package/postman-bdd

Writing tests with Postman BDD. This is made with Chai-JS.
API, Postman, Newman - Create a quick API with json-server and Faker and then test it with Chai-JS, Postman and Newman

Integration with Newman

You need to install newman first, choose to install globally.

npm install newman --global

Quickly, here what they say about Newman, I just quote.

Postman has a command-line test runner called Newman. If you prefer the CLI instead of a GUI, then this the tool for you. It’s also ideal for continuous-integration and continuous-delivery testing. Just like the Collection Runner, you can run your entire suite of tests, or just a single folder. You can load data from a file, and even write the test results to an output file in various formats (JSON, XML, HTML).

Source : https://github.com/BigstickCarpet/postman-bdd

When you have exported your collection, just run this command. My collection is called russian_roulette.postman_collection.json

newman run russian_roulette.postman_collection.json

Launching a test collection (russian_roulette.postman_collection.json) via Newman. It fails!
API, Postman, Newman - Create a quick API with json-server and Faker and then test it with Chai-JS, Postman and Newman

To proper launch your tests collection (russian_roulette.postman_collection.json), you need to make a first call to {{postman_bdd_path}} aka postman-bdd.min.js
API, Postman, Newman - Create a quick API with json-server and Faker and then test it with Chai-JS, Postman and Newman

Launching a test collection (russian_roulette.postman_collection.json) via Newman after the correction. It works!
API, Postman, Newman - Create a quick API with json-server and Faker and then test it with Chai-JS, Postman and Newman

Conclusion : Very useful and promising for anyone who dare to develop an API and remember little grasshopper « to test is to doubt ».

Read more