Any way to change the test's name

tests

#1

Using pm API, I wish to update the test of name on the fly based on the response/calculation.
Usage: want to make final test report more intuitive and useful by appending the important information on the Test name as it’s visible i the report.

right now, dealing by appending the full response in the function call itself but that gets huge at time.
pm.test(“TEST NAME”+pm.response.json(), () => {
//
});


#2

I used to do this back in the day by using variables to shorten the JSON path in the test name.

If the below response is returned:

{
	"people": [
		{
			"name": "Paul",
			"age": 29
		}
	]	
}

My Tests tab would look like this:

const body = pm.response.json();
const statusCode = pm.response.code;

pm.test("Expected 200 Satus Code to be returned", function() {
	pm.expect(statusCode).to.equal(200);
});

pm.test("Expect at least 1 person to be returned", function() {
	pm.expect(body).to.have.property("people");
	pm.expect(body.people).to.have.a.lengthOf.least(1);
});

if(body.people.length > 0) {
	const people = body.people;

	// Test will read: "Expect Paul's age to be 29"
	pm.test("Expect Paul's age to be " + people[0].age, function() {
		pm.expect(people[0]).to.have.property("age", 29);
	});

    // Or use backticks if you prefer
	pm.test(`Expect Paul's age to be ${people[0].age}`, function() {
		pm.expect(people[0]).to.have.property("age", 29);
	});

    // Or use variables to shorten the path
    const ageOfPaul = people[0].age;
	pm.test(`Expect Paul's age to be ${ageOfPaul}`, function() {
		pm.expect(ageOfPaul).to.equal(29);
	});
};

I stopped doing this though in case the property or value wasn’t returned so the test would say something like: “Expect Paul’s age to be undefined” which isn’t helpful.

I assume it’s not recommended to do this though as you should know what the value/result of the test will be upfront. Correct me if I’m wrong on this though.


#3

Hey @p00j, something like this can be achieved using Collection/Folder level test scripts (... => Edit). Let us take an example to understand this better.

Consider the case where you have two requests each of which return an user object. You would like to use the name of the user returned in each request in your test name. The response look like:

{
"user": {
  "name": "luffy",
  "username": "Kaizoku no ō"
  }
}

Now since the parent level test scripts get executed after each request they include, you can write some common parsing logic there to use the response returned by each request.

So if I consider, each request returns the above user object, I can write something like:

const response = pm.response.json(),
    user = response.user;
    
pm.test(user.name + ' additional string', function () {
    // test code goes here
});

This is just a basic example to demonstrate how you can build more complex workflows using the Collection/Folder level scripts, give it a shot and let me know if you need additional help.