2

Our team wants to automate our REST API testing. Right now, we have a collection of Postman requests and make them jump through hoops manually.

We could create a collection/folder for each testing scenario, but that would mean a ton of duplication. Our API is still under heavy development and I really don't want to fix the same thing at twenty places after it changes.

I would like to have each endpoint request only once in a collection and some kind of independent logic that can execute them in an arbitrary order. I know Postman doesn't support request reuse in any clean way, so I am looking for at least a hacky way how to do it.

Martin Grey
  • 744
  • 2
  • 12
  • 26
  • Are the collection and sub-folder elements of use to you. These were introduced to stop repeating the same thing over and over in your request and extract that out to a central area. It won’t solve all of your use case but might help - http://blog.getpostman.com/2017/12/13/keep-it-dry-with-collection-and-folder-elements/ – Danny Dainton May 11 '18 at 18:12

1 Answers1

8

Create a file to load into the Postman Collection Runner, with the following structure:

[{
    "testSequence": ["First request name", "Second request name", "..." ],
    "anyOtherData":  "Whatever the request needs",
    "evenMoreData":  "Whatever the request needs",
    "...":           "..."
},{
    "testSequence": ["Login", "Check newsfeed", "Send a picture", "Logout" ],
    "username":  "Example",
    "password":  "correcthorsebatterystaple",
},{
    "...": "keep the structure for any other test scenario or request sequence"
}]

Put all your test sequences in that file, then make Postman check the list after each request and decide what to execute next. This can be done e. g. in a "tests block" of the whole collection:

// Use the mechanism only if there is a test scenario file
// This IF prevents the block from firing when running single requests in Postman
if (pm.iterationData.get("testSequence")) {

    // Is there another request in the scenario?
    var sequence = pm.globals.get("testSequence");
    if ((sequence instanceof Array) && (sequence.length > 0)) {

        // If so, set it as the next one
        var nextRequest = sequence.shift();
        pm.globals.set("testSequence", sequence);
        postman.setNextRequest(nextRequest);

    } else {
        // Otherwise, this was the last one. Finish the execution.
        postman.setNextRequest(null);
    }
}

If your requests need to use different data during different runs, you can define the data in the input file and use them as variables in the request.

Martin Grey
  • 744
  • 2
  • 12
  • 26
  • The only way to run JavaScript code is to do a request. So I'll need to do a unrelated request that executes the JavaScript to get the test sequence loaded and the "first" request of the sequence set. Did you ever found a more graceful way of handling this without the need for a extra request? – PixelPlex Feb 03 '21 at 22:10
  • We usually ran something without side effects as the first request, like **/ping** . It's not very elegant, but gets the job done. – Martin Grey Feb 12 '21 at 12:32
  • Hi, I ended up doing something similar. My first and last request just load google. I use them as test startup and teardown where I do some logging, set/unset collection variables, etc... I hope Postman allows running js separate from the requests some day though – PixelPlex Feb 13 '21 at 13:58