Get In touch

Blog Details

Web-Services Testing: Develop JavaScript tests in POSTMAN

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):

Screenshot 2019-04-24 at 10.46.02 AM

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:

Screenshot 2019-04-24 at 10.49.15 AM

Step 3: Develop tests

Screenshot 2019-04-24 at 10.56.11 AM

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.

© 2022 QA Fiction. All rights reserved.