2

I'll go step-by-step.

I have a node js program that refers to a swagger specification json file and converts it into a Postman importable collection file (also in JSON format). I am doing this using "swagger2-postman-generator" npm library.

For a test I used this online available petstrore specification and the code is running fine. Reference link: http://petstore.swagger.io/

I want to take this one step further and add postman tests automatically into the exported Postman collection for each request.

My Requirements:

The code should read each request's response code specification from the swagger JSON. Basically it should read the below section of each request.

"responses": {
      "200": {
        "description": "successful operation",
        "schema": {
          "$ref": "#/definitions/ApiResponse"
        }
      }
    }

and create a corresponding postman test and add it to the exported JSON.

Example:

  • For /pet/{petId}/uploadImage, the expected response code is 200.
  • For /pet, th eexpected response code is 405.

The code that I have to convert Swagger to Postman Collection is:

const Swagger2Postman = require("swagger2-postman-generator");
var collectionJson = Swagger2Postman
.convertSwagger()
.fromUrl("http://petstore.swagger.io/v2/swagger.json")
.toPostmanCollectionFile("test_petstore_today.json")
const collectionJSONToday = require("./test_petstore_today.json");
console.log(collectionJSONToday);
user1710861
  • 384
  • 5
  • 8

0 Answers0