New Postman to k6 converter tool with support for pre-request and test scripts

Posted by Robin on Mar 26, 2019

About two years ago, not long after the open-sourcing of k6, we released a Postman-to-k6 converter that was capable of converting the request definitions specified in a Postman collection to the equivalent k6 JS code. It worked well, but was very limited in scope as it didn’t handle all the features that makes Postman the great tool it is, like variables, data files, and pre-request and test scripts.

Today, we’re happy to announce a greatly improved version of the converter, that is capable of converting a much larger part of the Postman feature set:

  • Pre-request scripts.

  • Test scripts.

  • Variables (at all scopes + dynamic).

  • Data files.

  • Authentication methods (except Hawk).

  • postman.* interface.

  • pm.* interface.

  • Global variables exposed by Postman: globals environment data iteration.

  • xml2Json conversion.

  • File formats v2 and v2.1.

Before jumping into the details of what the converter can do and how it works, credit needs to be given where credit is due; major thanks to @bookmoons for doing an amazing job (yet again) implementing this tool 👏🙌

 

Getting started

Let’s waste no more time, this is how you get the converter installed:

npm install -g postman-to-k6

This will install the converter globally on your system so it can be run from anywhere (we recommend to use nvm to avoid permission issues with trying to use sudo on Unix/Linux systems). If you prefer to install it on a per-project basis, as a dependency, you can of course do that as well (run something like npm install --save-dev postman-to-k6instead).

To convert a Postman collection file you run a command like this:

postman-to-k6 -o /conversion/output/test.js MyCollection.postman_collection.json

When you run this two things happen:

  1. The converter will parse the Postman collection and output the equivalent k6 JS test script to a file at /conversion/output/test.js.

  2. As some Postman features don’t exist in k6 yet, we need to bridge that feature gap by importing third-party Node.js libraries. A /conversion/output/libs directory will be created including the necessary (browserified) Node.js libraries. The JS libs in this directory will be imported by the main test.js file as needed.

 

Will it work on all Postman collections?

The converter can convert v2.0 and v2.1 of the Postman collection format.

 

A word about the future

We’re very excited about getting this new release into the hands of our customers, users and the greater community. We’d love to hear what you think of this new incarnation of the tool, especially what you think we should improve. Add a comment to this post, open an issue on Github or reach out through any of the other channels (k6 community forum, k6 Slack or Email).

I’ll close off by sharing the next steps we’d like to take with the converter:

  • Output requests to separate JS files: Getting each request converted to its own JS file will make it easy to compose different combinations of requests together into user journeys.

  • Support the complete postman.* and pm.* APIs: Some of these Postman Sandbox APIs can’t be supported because of missing features in k6. Once they get addressed in k6 we can implement them in the converter:

    • Sending requests from scripts: pm.sendRequest (k6 currently doesn’t support asynchronous code; callbacks, promises, async/await etc.)

    • Controlling request execution order: postman.setNextRequest

    • Cookie properties: hostOnly, session and storeId

    • Textual response status message: responseCode.name, responseCode.detail, pm.response.reasonpm.response.to.have.status(reason) and pm.response.to.not.have.status(reason)

    • Properties returning Postman classes: pm.request.url, pm.request.headers and pm.response.headers

Topics: postman, k6, api performance testing, api load testing, OSS

Recent Posts

Popular posts

Posts by Topic

see all

Subscribe to Email Updates