environment-variablespostmanpostman-collection-runnerpostman-collection

Postman Running collections against different environments


I have a postman collection with 3 requests (A,B,C) and I have 3 different environments: "dev", "test", "uat". I would like to create a Run which will run all 3 requests from the collection against the 3 different envs. So the effective runs will be:

env:dev, request A
env:dev, request B
env:dev, request C

env:test, request A
env:test, request B
env:test, request C

env:uat, request A
env:uat, request B
env:uat, request C

Is there a simple way to achieve it in Postman?


Solution

  • Newman will address your question.

    Run newman with environment

    newman run your.postman_collection.json  -e dev.postman_environment.json
    newman run your.postman_collection.json  -e test.postman_environment.json
    newman run your.postman_collection.json  -e uat.postman_environment.json
    

    Demo REST API

    https://freetestapi.com/apis/animals
    

    Get Animal by [animal name] query https://freetestapi.com/api/v1/animals?search=[animal name]

    example

    https://freetestapi.com/api/v1/animals?search=Lion
    

    enter image description here

    Set three environments (dev/test/uat)

    dev environment enter image description here

    test environment enter image description here

    uat environment enter image description here

    Set three requests (dev/test/uat)

    request A

    https://freetestapi.com/api/v1/animals?search={{request_A}}
    

    Tests tab

    var jsonData = JSON.parse(responseBody);
    console.log(jsonData[0].name);
    pm.expect(jsonData[0].name).to.eql(pm.environment.get("request_A"));
    

    Run on `dev' environment enter image description here

    request B

    https://freetestapi.com/api/v1/animals?search={{request_B}}
    

    Tests tab

    var jsonData = JSON.parse(responseBody);
    console.log(jsonData[0].name);
    pm.expect(jsonData[0].name).to.eql(pm.environment.get("request_B"));
    

    Run on `dev' environment enter image description here

    request C

    https://freetestapi.com/api/v1/animals?search={{request_C}}
    

    Tests tab

    var jsonData = JSON.parse(responseBody);
    console.log(jsonData[0].name);
    pm.expect(jsonData[0].name).to.eql(pm.environment.get("request_C"));
    

    Run on `dev' environment enter image description here

    Export collection and environments

    Export collection Will be saved as 1-demo.postman_collection.json

    enter image description here

    Export environment Will be saved as dev.postman_environment.json

    enter image description here

    Other two environments

    Will be saved as test.postman_environment.json

    Will be saved as uat.postman_environment.json

    Run newman with three environments by bash

    Save as run.sh

    newman run 1-demo.postman_collection.json  -e dev.postman_environment.json
    newman run 1-demo.postman_collection.json  -e test.postman_environment.json
    newman run 1-demo.postman_collection.json  -e uat.postman_environment.json
    

    Run it in git bash

    ./run.sh
    

    Result

    enter image description here