Newman: [FEATURE REQUEST] reuse codes in postman/newman

Created on 1 Nov 2016  路  8Comments  路  Source: postmanlabs/newman

For example, many of my tests look like this:

tests[`[INFO] Request params: ${JSON.stringify(request.data)}`] = true;
let json;
try {
  json = JSON.parse(responseBody);
} catch (err) {
  tests['Expect response body to be valid JSON'] = false;
  tests[`[INFO] Response body: ${responseBody}`] = true;
  console.error(err);
}

if (json) {
  const { resultCode, resultMsg, detailMsg } = json;
  if (resultMsg) tests[`[INFO] Response body: ${resultMsg}`] = true;
  if (detailMsg) tests[`[INFO] Response body: ${detailMsg}`] = true;

  tests[`Expect "resultCode" to equal 1, actual: ${resultCode}`] = resultCode === 1;
}

// all APIs need codes like above : (

if (json && json.data) {
  const { accountNum, accountType, userId, userType, password } = json.data;
  tests['Expect "accountNum" to be string'] = typeof accountNum === 'string';
  tests['Expect "accountType" to be string'] = typeof accountType === 'string';
  tests['Expect "userType" to be number'] = typeof userType === 'number';
  tests['Expect "userId" to be number'] = typeof userId === 'number';
  tests['Expect "password" to be string'] = typeof password === 'string';
} else {
  tests['Expect response body to have "data"'] = false;
}

For now, all I can do is put those codes in globals:

image

image

Otherwise it's a nightmare to write assertions like this: ( Imagine how many times you need to type tests['blahblahblah'] = foo === bar ...)

image

And sometimes we may even want to reuse the whole assertions above, as multiple APIs may return same object, e.g. user object, auth object etc.

request

Need a place to store code snippets and can be called globally with something better than eval.

feature request runtime

Most helpful comment

This is coming soon. The route we've chosen is, we will allow having common scripts at the folder/collection level.

Since each script runs independently of the others, there's little chance of having persistent functions defined across different scripts.

All 8 comments

This is coming soon. The route we've chosen is, we will allow having common scripts at the folder/collection level.

Since each script runs independently of the others, there's little chance of having persistent functions defined across different scripts.

Exciting feature, can't wait!
also hope could reuse/refer a request itself, let's say we have a sequence of requests in a test suit:
step 1. list
step 2. add
step 3. list again

The "list again" in step 3 is exactly the same as the "list" in step 1, can we just refer to the "list" request without duplicating a new "list again" request?

@x-iron You can use postman.setNextRequest("list"); in the test script of the add request to achieve this. Please see https://www.getpostman.com/docs/workflows for more details.

Hi @kunagpal, thanks for your info. but in my case:
step 1. list
step 2. add
step 3. list again
if I change to the following steps with work flow feature:
step 1. list
step 2. add then setNextRequest("list")

I think step 1 and step 2 will come into an infinite loop?

@x-iron Plainly setting the next request will indeed cause an infinite loop, but postman.setNextRequest(null); can be used to break out of the request cycle, based on a condition of your choice. For instance, the test script of the add request can have:

postman.setEnvironmentVariable("flag", true);`

Within the test script of list, you could use the following logic:

if (postman.getEnvironmentVariable("flag")) {
    postman.setEnvironmentVariable("flag", "");
    postman.setNextRequest(null);
}

@czardoz any update on reusing the test code?

@anugupta007 Apologies for the delay, we're in the process of polishing the user experience for collection and folder level entities. Thanks for all your patience 馃槃

@anugupta007 @keithmork @x-iron Newman v3.9.0 supports collection and folder level auth and scripts. These can be set within v5.4.1 of the Postman app and above 馃槃

Was this page helpful?
0 / 5 - 0 ratings