Introduction to Programmatic Approach for Testing with Mocha

Simple and Efficient Approach to automation testing using Mocha Framework

Puneet Punj
Mar 29 · 4 min read
Mocha — Testing Framework (simple, flexible, fun)

Mocha is a feature-rich JavaScript test framework running on Node.js and in the browser, making asynchronous testing simple and fun. Mocha tests run serially, allowing for flexible and accurate reporting, while mapping uncaught exceptions to the correct test cases. Hosted on GitHub.

Lets start with understanding how to quickly setup mocha framework and execute a sample test. Please ensure node.js is already installed.

Open your terminal and start by installing mocha and chai (assertion library) packages.

mkdir programmatic-mocha-framework && cd programmatic-mocha-framework
npm init -y
npm i mocha chai
//setup first testmkdir test
touch test/sample-test.js

Open sample-test.js and paste below code:

test/sample-test.js

Update in .

"scripts": {
"test": "mocha"
},

Execute tests by running and terminal report would be generated with all 10 tests passed as 0–9 numbers are lesser than 10.

Mocha Execution Results

Now, lets see how we can write the same test in programmatic way.

Photo by Markus Spiske on Unsplash

Step1

Create new inside folder by running . As name suggests, this would contain the basic setup required for mocha tests. This file provides ability to create new suites and tests and exposes a function to execute them.

Before copying below code to your test-setup file, install two packages required for basic setup

lib/mocha-setup.js

Step2

Time to create actual test suite and tests. First, lets create a new file to place these sample tests by running . In this file we will:

  • Add same tests we had under test directory but in a programmtic way
  • Add another test suite under parent suite and add test in child suite
  • Add test directory under the parent suite

Copy below code to sample-tests.js

programmatic/sample-tests.js

Step3

Once all the test suites are designed, time to see how to execute them.

Create new file and copy below code:

execute-test.js

Step4

Update , replace with below code

"scripts": {
"test": "node execute-tests.js"
},

Let’s execute tests by running same command . This will auotmatically create a new folder named as and subfolders will be created based on current datetime. Mochawesome html and json would be available in the folder. I have executed tests few times hence there are multiple timestamp folders. Your entire directory will look like as:

Programmatic Mocha Execution

Open the mochawesome.html report generated under latest timestamp folder (name would change based on execution time).

Mochawesome Execution Report

And thats it… feel free to add more tests in the


Benefits

Some of the benefits, I found, while working on this approach:

  • Abstraction of tests from the actual framework
  • Centrally managed framework design and let the tests file per initiative/project
  • Allowed to make tests data driven dynamically — this is one of the key advantage over traditional mocha. In this way, you can dynamically read data from csv files and iterate to generate same number of tests rather than providing hard coded value in the code
  • One place change for the entire framework used by different teams
  • I have used this approach to upload files to S3 bucket and sending notification via email on failure by uplifting lib/mocha-setup.js and passed initiative name as parameter to distinguish between report files.

For more information go to “Using Mocha programmatically


Code is available on my git here. Feel free to reach out to me in case of any feedback.

A note from JavaScript In Plain English: We are always interested in helping to promote quality content. If you have an article that you would like to submit to JavaScript In Plain English, send us an email at submissions@javascriptinplainenglish.com with your Medium username and we will get you added as a writer.

JavaScript In Plain English

New articles every day.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

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