Intro to test framework MOCHA

Shashi Kumar Raja
TestCult
Published in
5 min readAug 20, 2018
Photo by rawpixel on Unsplash

In the 2nd part of this series we will get familiar with Mocha, a javascript based test framework running on node.js.

Before proceeding forward it is recommended to read Part 1 of this series which explains the basics of automation-

Step 1: Installation-

You need to install node.js first as it runs on node.js. With node.js comes npm which is a package manager for javascript. We will install mocha using npm.

Let’s create a project directory and setup mocha environment-

mkdir api_tests
cd api_tests
npm init
//Select yes and fill all the questions asked while doing npm init
npm install mocha --save

That’s it, mocha is installed and added to package.json (this file holds various metadata relevant to the project) file of your project directory.

Your project folder should now look like-

- api_tests/
- node_modules/
- package.json

Step 2: Mocha Syntax for writing tests-

Mocha gives you two functions to write tests- describe() and it().

it() is a test case and can be grouped into describe(). So you imagine it() to be a single test case inside a test suite represented by describe().

  • it() needs two arguments, first a string which is a name or description of what it does and second a callback function which will contain the test body.
it('name of test', function(){
//test body here
});
//You can also use arrow functions(=>) buts its usage is discouraged because => lexically binds this and cannot access the Mocha context. However, in this demo I am going to use => due to convenient and will switch to function() wherever this usage is required.it('name of test', () => {
//test body here
});
  • describe() also needs two arguments same as it().
describe('name of suite', () => {
it('name of test', () => {
//test body here
});
});

You can nest describe() as deep as you want.

describe('Inception', () => {
describe('Dream level 7', () => {
describe('Dream level 6', () => {
it('should find Cobb', ()=> {
//test body here
});
});
});
});

Step 3: Hooks-

Now, that we know how to write tests using it() and group them inside a describe(), lets learn how to perform pre and post test operations using hooks.

Mocha provides four hooks-

  • before(): syntax is same as describe() and it() but first argument is optional. It runs once before first it() or describe().
describe('name of suite', () => {
before(() => {
//Will execute only once before first it is executed here.
});
it('Test 1', () => {
//test body here
});
it('Test 2', () => {
//test body here
});
});
  • beforeEach(): syntax is same as before(). It runs before each it() or describe().
describe('name of suite', () => {
beforeEach(() => {
//Will execute before each it below.So total twice here.
});
it('Test 1', () => {
//test body here
});
it('Test 2', () => {
//test body here
});
});
  • after(): syntax is same as before(). It runs once after last it() or describe().
describe('name of suite', () => {
it('Test 1', () => {
//test body here
});
it('Test 2', () => {
//test body here
});
after(() => {
//Will execute only once after last it is executed here.
});
});
  • afterEach(): syntax is same as before(). It runs after each it() or describe().
describe('name of suite', () => {
it('Test 1', () => {
//test body here
});
it('Test 2', () => {
//test body here
});
afterEach(() => {
//Will execute after each it is executed.So total twice here.
});
});

Tests can appear before, after, or interspersed with your hooks. Hooks will run in the order they are defined, as appropriate; all before() hooks run (once), then any beforeEach() hooks, tests, any afterEach() hooks, and finally after() hooks (once).

Step 4: Inclusion and Exclusion of tests-

Mocha gives you two option to run tests selectively-

  • only(): use it with any it() or describe() to run only those tests or suites.
describe('name of suite', () => {
it.only('Test 1', () => {
//test body here
});
it('Test 2', () => {
//test body here
});
});
//Here only Test 1 will run
  • skip(): use it with any it() or describe() to skip those tests or suites.
describe('name of suite', () => {
it.skip('Test 1', () => {
//test body here
});
it('Test 2', () => {
//test body here
});
});
//Here only Test 2 will run and Test 1 will get skipped and marked as pending.

We are now familiar with the basic mocha syntax. So, lets implement it with an example-

Step 5: Write an actual test in mocha

Inside our project folder api_tests create a folder called test and create a file test.js inside that.We will write our test in this file.

- api_tests/
- node_modules/
- package.json
- test/
-test.js

Also open your package.json file and modify your “tests” to have value “mocha test/*”, it simply tells to run everything inside test with mocha.

//package.json
{
"name": "api_tests",
"version": "1.0.0","description": "","main": "index.js","scripts": { "test": "mocha test/*"
},
"author": "","license": "ISC","dependencies": { "mocha": "^5.2.0"
}
}

Now inside our test.js file we will first create a function caculateSavings which returns saving amount given an income and expenditure and write tests for it.

To run the test simply traverse to the project directory in terminal and run-

cd api_tests/
npm test

This will give you a run summary in your terminal -

Run Report

All these details should give you enough knowledge to get started with mocha. For more clarification I encourage you to refer to the official mocha documentation.

I will cover more about Mocha in the next part of this series. Meanwhile to kickstart api automation with mocha you can take a look at this boilerplate repo I have created on git.

--

--