Obtain folder name in test script
Asked Answered
V

4

5

I'm writing API tests using Postman. I'm organizing them into folders by endpoint, and subfolders by test cases within the endpoint folders. There are multiple cases for each endpoint and for each case there are post calls that set up data prior the the csubject-endpoint call that I'm making assertions against.

I already have 100s of calls in this suite. The test runner, unfortunately, does not provide the folder names in its output, so it's difficult to see at a glance which particular case I am looking at when, for example, it reports a test fail.

Is there a convenient way to obtain the folder names for a given call in its test script? With this, I could prepend the case name to the test name, and that would make my tests vastly more readable.

Violet answered 2/10, 2018 at 16:58 Comment(0)
C
1

I don't think that there is anything like that from within the application - The closet I can see is the pm.info.requestName function which references the request name that the test belongs too.

This is a basic use case but you could add this to the test name to give you a 'quick glance' and what was run against what request.

pm.test(`${pm.info.requestName} - Status code is 200`, () => {
    pm.response.to.have.status(200)
})

enter image description here

If you take a look at Newman it might have something within the summary object that you could extract, in a script, to get the folder name but I've never done this.

Caines answered 3/10, 2018 at 10:8 Comment(0)
S
5

I think a variable containing the current request path in the folder hierarchy would be the best, but for now I didn't find such a feature.

Instead I may suggest such a workaround solution:

In each folder prerequest scripts you set a variable:

pm.environment.set("folder", "folder1/folder1.1/")

the value of folder variable you have to maintain separately for each folder.

Then on a collection level you put a collection test like this:

pm.test("location: " + pm.environment.get("folder"), true)

After running your collection in the Runner you will get the output from collection tests at the beginning of the test results for each request showing the folder location.

The effort of setting folder variables pays off when you estimate the results of complex tests. I used to change the names of the requests but it is even more complicated.

UPDATE:

You can also find the info in results hovering over a gray shortcut of path on the left of the request status. A tooltip displays a full path, what in fact eliminates the need of the above solution if you only want to observe the results, but the solution can be useful if you want to make some logs containing the path.

Ss answered 10/10, 2018 at 13:27 Comment(1)
Clever hack. Thanks. πŸ‘Š – Timbrel
T
3

Try this below code insidea Pre-request Script tab. I hope it's help you.

/**
 * Folder
 */
const location = pm.execution.location;
const filterLocation = location.filter((f, i) => i && f !== "root" && f !== "index" );
const folder = filterLocation.join("/");
pm.variables.set("folder_name", folder);

Show complete my request with console

Tressietressure answered 22/3 at 17:10 Comment(6)
I got the "location" returned ['collection name', 'request name'], not folder name, any way, nice try pm.execution.location. – Souvenir
Have you folder inside a collection in your request? – Tressietressure
No, I put this script in Tests tab. ``` const location = pm.execution.location; console.log(location); ``` I got this log in console.log() ``` (2) ["101-demo", "Get Lion"] 0: "101-demo" 1: "Get Lion" ``` – Souvenir
I try in pre-request script tab, it's worked for me – Tressietressure
Same result ran at pre-request script You worked what folder_name display? local directory or something else? – Souvenir
Do you follow the structure like a above image? – Tressietressure
C
1

I don't think that there is anything like that from within the application - The closet I can see is the pm.info.requestName function which references the request name that the test belongs too.

This is a basic use case but you could add this to the test name to give you a 'quick glance' and what was run against what request.

pm.test(`${pm.info.requestName} - Status code is 200`, () => {
    pm.response.to.have.status(200)
})

enter image description here

If you take a look at Newman it might have something within the summary object that you could extract, in a script, to get the folder name but I've never done this.

Caines answered 3/10, 2018 at 10:8 Comment(0)
H
-1

Closest thing right now would be this:

enter image description here

I believe I included this in my logged bugs / feature requests out to their team.

Hiatus answered 1/2, 2021 at 17:52 Comment(0)

© 2022 - 2024 β€” McMap. All rights reserved.