Introduction to Programmatic Approach for Testing with Mocha
Simple and Efficient Approach to automation testing using Mocha Framework
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
Open sample-test.js and paste below code:
Execute tests by running
npm run testand terminal report would be generated with all 10 tests passed as 0–9 numbers are lesser than 10.
Now, lets see how we can write the same test in programmatic way.
lib folder by running
mkdir lib && touch lib/mocha-setup.js. 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
npm i moment-timezone mochawesome
Time to create actual test suite and tests. First, lets create a new file to place these sample tests by running
mkdir programmatic && touch programmatic/sample-tests.js. 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
Once all the test suites are designed, time to see how to execute them.
Create new file
execute-test.js and copy below code:
scripts with below code
"test": "node execute-tests.js"
Let’s execute tests by running same command
npm run test. This will auotmatically create a new folder named as
execution-report and subfolders will be created based on current datetime. Mochawesome html and json would be available in the
currentime folder. I have executed tests few times hence there are multiple timestamp folders. Your entire directory will look like as:
Open the mochawesome.html report generated under latest timestamp folder
22:03:03 (name would change based on execution time).
And thats it… feel free to add more tests in the
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.