1
votes

I'm writing test in Postman and I have multiple requests grouped like this:

  1. Some test title:

    • Create a user (set of "pre-requests"):

      • sending a few requests required to create a user, necessary to run tests
    • Some action on a created user (here I'm testing what is in the test title)

      • one or more requests
  2. Another test title:
    • Create a user (set of "pre-requests", the same as in test 1.):
      • sending a few requests required to create a user, necessary to run tests
    • Some action on a created user (not related ot test 1.)
      • one or more requests

To summarize I need to create a user before every request when I want to test something.

My question - How can I re-use "Create a user" set of requests without copying it?

2
Is this user always the same?so cal cheesehead
I have to create a new user every time to test the flow.wawr
Yes but is the user object always the same? i.e. same name, id, etc. Also does it need to execute for every request or just at the beginning of the collection?so cal cheesehead
Every time I create a new user with different name: user_name_prefix + incremented number. User has to be created before set of request. e.g. 1) Create a user, 2) add some rightswawr

2 Answers

0
votes

This is how you can re-use the set of requests in pre-testcases and Test by setting environment variable and call it using eval function

pre-test case -

var Create_a_user = () => {
pm.sendRequest("http://mocktarget.apigee.net/json", function(err, res) {

    tests["Status code is 200"] = pm.expect(res).to.have.property('code', 200);
    console.log('firstName',res.json().firstName);      
});

pm.sendRequest("http://mocktarget.apigee.net/json", function(err, res) {

    tests["Status code is 200"] = pm.expect(res).to.have.property('code', 200);
    console.log("lastName - "+ res.json().lastName);
});

pm.sendRequest("http://mocktarget.apigee.net/json", function(err, res) {

    tests["Status code is 200"] = res.code === 200;
    console.log("city - "+ res.json().lastName.city);
});
};

pm.environment.set("Create_a_user", Create_a_user.toString());

Test -

eval(pm.environment.get("Create_a_user"))();

output -

firstName - John

lastName - Doe

city - San Jose

Disclaimer - Use Eval function carefully, it may fizzled up your code or its execution.

0
votes

If i'm understanding correctly what you want is unfortunately not currently supported. I've been waiting on this feature a while myself.

https://github.com/postmanlabs/postman-app-support/issues/1535