Have you ever tried writing JavaScript tests in POSTMAN?
In this post, we’ll learn how to write JavaScript unit tests in POSTMAN using MochaJs, a node.js based library.
Note: I’m assuming that you have POSTMAN desktop application.
Postman supports MochaJS library explicitly which assist in developing bdd-styled unit tests.
Step 1: Request to Postman-Mocha BDD API and Store Response in Global env Variable
As part of framework set up, we need to send a request to postman-mocha api and store response in an env variable within postman env (Needs to execute as pre-requisite script to further requests):
Step 2: Access Global env Variable to Utilise Mocha library
With each API call, we’ll access stored env variable postmanBDD to write mocha tests:
Step 3: Develop tests
Mocha uses describe-it syntax to modularise cases. A describe-hook can hold multiple it-hook to group similar tests.
Consolidating to Conclusion
With BDD-styled JavaScript tests, we can automate our API tests for unit testing purposes that could be executed as part of Smoke and Sanity to quickly test web-services.
Import Github hosted postman collection in your postman application and try by yourself.