1

Request Body

{
"StudentInfo":{
"Student":[
{
"StudentId":"001"
}
}
}

Response Body

{
"Studentinfo":{
"Student":[
{
"StudentId":"001",
"firstname":"John",
"Lastname":"Tac",
"DOB":"2015-02-10"
}
}
}

How can I write the validation on tests from the response body. In the test result I need to view the output parameters like below

PASS "StudentID": 001
PASS "DOB":2015-02-10

Update:

Thank you for your answers. If I need to read from response body and put dynamically in test how can we achieve this instead of passing hard code values?

pm.test("Student ID validation", function () {
    // Parse the response body to JSON object
    var jsonData = pm.response.json();
    
    // Validate Student ID
    pm.expect(jsonData.Studentinfo.Student[0].StudentId).to.eql(**":"**);

    // Log the result
    pm.log("Student ID: " + jsonData.Studentinfo.Student[0].StudentId);
});

pm.test("DOB validation", function () {
    // Parse the response body to JSON object
    var jsonData = pm.response.json();
    
    // Validate DOB
    pm.expect(jsonData.Studentinfo.Student[0].DOB).to.eql(**""**);

    // Log the result
    pm.log("DOB: " + jsonData.Studentinfo.Student[0].DOB);
});

1 Answer 1

1

To validate the response body using tests in Postman, you can use the test script section in the request. Here's an example of how you can write the validation script for the given request and response:

Request Body:

{ "StudentInfo":{ "Student":[ { "StudentId":"001" } ] } }

Response Body:

{ "Studentinfo":{ "Student":[ { "StudentId":"001", "firstname":"John", "Lastname":"Tac", "DOB":"2015-02-10" } ] } }

Test Script:

pm.test("Student ID validation", function () {
    // Parse the response body to JSON object
    var jsonData = pm.response.json();
    
    // Validate Student ID
    pm.expect(jsonData.Studentinfo.Student[0].StudentId).to.eql("001");

    // Log the result
    pm.log("Student ID: " + jsonData.Studentinfo.Student[0].StudentId);
});

pm.test("DOB validation", function () {
    // Parse the response body to JSON object
    var jsonData = pm.response.json();
    
    // Validate DOB
    pm.expect(jsonData.Studentinfo.Student[0].DOB).to.eql("2015-02-10");

    // Log the result
    pm.log("DOB: " + jsonData.Studentinfo.Student[0].DOB);
});

In the above script, we have used the pm.test() function to define two tests. In each test, we have first parsed the response body to a JSON object using the pm.response.json() function. Then, we have used the pm.expect() function to validate the Student ID and DOB values from the response body. Finally, we have used the pm.log() function to log the result.

When you run the request, you can view the test results in the Test Results tab of the response window. The output parameters like "Student ID" and "DOB" will be displayed under the Log section of the corresponding test.

For more information on writing tests in Postman, you can refer to the official documentation here: https://learning.postman.com/docs/writing-scripts/test-scripts/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.