Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to refer to postman call name within the tests body

Tags:

postman

App Details:

Postman for Chrome
Version 5.0.1
win / x86-64
Chrome 58.0.3029.110

In my collection, I have various requests within folders. e.g. Collection>CollectionFolder1>Request1, Request2 ...

Is it possible to get a reference of the request names within the tests so that I could write something like:

try {
   ...
} catch (e) {
    tests[Test failed with exception ${e} for call request ${SOME_VARIABLE_THAT_STORES_REQUEST_NAME}] = false
}

This would allow me to duplicate this skeleton in all my requests without having to bother about maintaining it.

Is there any postman variable or structure that would store any such info.

like image 867
overflower Avatar asked Jun 22 '17 03:06

overflower


People also ask

How do you request a title for a Postman?

If you mouse over the name of the request below the tab a pencil edit icon should appear, click it and then edit the name. Or within your collection on the left hand side of the app you can mouse over the name of the request, click on the ellipsis and then select Rename.

What is header and body in Postman?

The Body tab in Postman allows you to specify the data you need to send with a request. You can send various different types of body data to suit your API. If you're sending body data, make sure you have the correct headers selected to indicate the content type your API may need to process the received data correctly.

What are the two ways in which tests can be written in Postman?

You can write test scripts for your Postman API requests in JavaScript. You can also use test code to aid the debugging process when something goes wrong with your API project.


2 Answers

In https://www.getpostman.com/docs/postman/scripts/postman_sandbox - paragraph Request/response related properties you can use the 'request' object.

request.name: gives you the current test case name Obsolete use pm.info.requestName

request.method: gives you the method used (PUT, GET, etc.)

request.url: gives you the target url

In order to see all the available data you may use, I suggest you to open the console (View/Show postman console or Alt+Ctrl+C) and see the data returned by this: console.log(request)

like image 56
A.Joly Avatar answered Oct 01 '22 13:10

A.Joly


Postman v6.5.2 and up uses pm.info.requestName

console.log("Running: "+ pm.info.requestName); 

Look at pm.info object:

https://learning.postman.com/docs/writing-scripts/script-references/postman-sandbox-api-reference/#scripting-with-request-info

like image 36
Dan Krueger Avatar answered Oct 01 '22 12:10

Dan Krueger