1

I'm busy testing some web API, and am trying to use Postman to automate that.

As far as I can see, test scripts in Postman are always tied to a single request, so if I want to test different scenarios for the same request, I would have to make a copy of that request for each scenario, where each copy has a different test script. This is something I'd like to avoid.

Example: I have a request that gets a certain resource, and I want to test with or without an access token:

  1. Request without access token: expect status 401
  2. Request with a valid access token: expect status 200

Other example: I create some resource on the server, and then try to create the same thing again:

  1. First time: expect status 200
  2. Second time: expect status 409

I found some related questions, but they all seem to involve a lot of scripting, where you are basically writing your own test runner environment. As a developer, I don't mind writing scripts, but in this case I'd like to focus on writing actual tests.

So my question is: can Postman do this, or is it maybe not the best tool for this job?

Destroy666
  • 12,350
Berend
  • 3,192

1 Answers1

1

You don't have to do multiple copies of any request. Simply adapt your post-response scripts to all needed cases.

First time: expect status 200
Second time: expect status 409

Set a collection variable that indicates the creation status, if the 1st request was successful:

pm.collectionVariables.set("objectCreated", true);

And at the beginning of the script read the value to change the flow:

const created = pm.collectionVariables.get("objectCreated");
if (created) {
  // test for 409
} else {
  // test for 200 and set the collection var if successful
}

Call the request twice.

Request without access token: expect status 401
Request with a valid access token: expect status 200

Similar case to the one above, but use {{accessToken}} syntax to represent accessToken collection variable in request header or body. Then check in the post-response script whether the var was empty, likewise separating both cases and testing the correct response code.

Then run the request with valid accessToken entered and with it emptied.


In case you don't know how to automate the data, here's the documentation. TLDR: in collection runner use a JSON data file like this:

[
  {
    "accessToken": "xyz"
  },
  {
    "accessToken": ""
  }
]
Destroy666
  • 12,350