SOA Testing

with

mocha

@loveagileqa

qualityworkscg.com

Getting started

Definitions

&

Explanation

mocha

Mocha

simple,flexible, fun

Mocha is a javascript testing framework that makes asynchronous testing easy.

 

Mocha can run on either node.js or in the browser. When compared to other javascript testing frameworks like Jasmine, it was found that Mocha’s handling of asynchronous testing was a key factor in any decision to use it. When testing  API, it needs to send some data to an endpoint and use the data that comes back to make a call to another endpoint. For example, you need to get a user, then grab that user’s id to get all locations that belong to that user.

 

Learn more: 

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”.

 

Although Mocha can be used with any assertion library and Chai can be used with any javascript testing framework, many javascript developers choose to use them together. Chai’s “expect” interface is used to chain natural language assertions together, so you can test the JSON coming back from your API endpoints thoroughly.

 

Learn more about Chai API:   

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.

 

In addition to testing the contents of the JSON objects, we also wanted to test other elements of the endpoint response including header type and response code. SuperTest also has a pretty intuitive interface. Simply call the endpoint and send it the data it needs, then test the response.

 

Learn more about SuperTest and it’s API:

Setup

How to get started

mocha

Install Dev Dependencies 

This tutorial assumes you already have node.js installed.

 

 

 

 

IF(node.js != installed)
    


    visit 


ELSE
    CONTINUE 
   

Installing Packages 

$ npm i -g mocha

$ npm init

$ npm i supertest

$ npm i chai

$ npm i jsonschema

Set up your test folder

 

From your terminal, run mkdir to create test project folder to house all of your test files for the specific services/api that you will be testing. Then cd into that directory to create a test directory within the test project. The directory must be called test for Mocha to find test files to run.

$ mkdir testProject

$ cd testProject

$ mkdir test

Create your test file

 

You can name your mocha files anything. however, if you are testing multiple endpoints that are associated with set of models. recommend naming them:  “yourModel_test.js” etc. For this example, just create a test.js file within the test directory. 

*you can do so by simply using this command

$ touch test/test.js

Require the Libraries

 

Require each library and set it to a global variable at the top of the test file.

Add to your test.js:

 

 

var should = require('chai').should(),
expect = require('chai').expect,
supertest = require('supertest');

Set your API URL

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. If you have a local database, set the variable to localhost. If you want to test in development, be sure to set the url to your service/api instance.

var api = supertest("http://localhost:3000");

Writing your first TEST

mocha

Step 1

Defining the TestSuite

For our first test, we’ll start by writing a describe function and passing it a string. The string should be a verb because it is declaring the thing we are doing. In this case we will use our first describe function to tell the kind of REST request (GET, POST, DELETE OR PUT)

describe('GET', function){

});

Note: The structure used in this tutorial is a not must when doing tests with Mocha. Nonetheless, we have found it to be easier to have this layout as it will be easier for anyone viewing your test code to understand. 

  • We describe the kind of REST request. ex GET

 

 

 

Step 2

Defining the TestCase

Now, we'll be creating our test case within our test suite by using another describe function. The string should be meaningful because it is declaring the thing we are hoping to verify. So now we have "GET homepage and check for valid response"

describe('GET', function){
    
    describe('homepage and check for valid response',function(){
        
        
    });

});

Step 3

Defining the Actual Test

Next, we'll be creating our test within our test case by using the it function that gives detail the behavior that we want to test. We’ll pass each function a string that explicitly states the desired outcome of a passing test. This string can be anything, but it’s important that it is human readable because you’ll be referencing this string in the terminal when we run our tests.

describe('GET', function){
    
    describe('homepage and check for valid response',function(){
        
        it('should return a 200 response', function(done){

		api.get('/')
		.set('Accept','application/json')
		.expect(200,done);
	});
    });
});

Now to run your test from the terminal:

*Make sure you are in your projects' parent directory then you run the following command

 

 

In our test, we are simply sending a get request to an endpoint and expecting a 200 response.

Congratulations 

You have just created your first api test… 

 $ mocha

Writing More TEST

mocha

Title Text

Subtitle

Made with Slides.com