To compare the response body with your database data in Postman tests, you can make use of the Postman's built-in test script feature. Here is a sample test script that can help you compare the response body with your database data:
// Get the response body
let responseBody = pm.response.json();
// Get the student ID from the request body
let studentId = pm.request.json().StudentInfo.Student[0].StudentId;
// Query the database to get the student data
let dbData = getStudentDataFromDB(studentId);
// Compare the response body with the database data
pm.test("Response body matches the database data", function () {
pm.expect(responseBody.Studentinfo.Student[0].StudentId).to.equal(dbData.StudentId);
pm.expect(responseBody.Studentinfo.Student[0].firstname).to.equal(dbData.FirstName);
pm.expect(responseBody.Studentinfo.Student[0].Lastname).to.equal(dbData.LastName);
pm.expect(responseBody.Studentinfo.Student[0].DOB).to.equal(dbData.DOB);
});
// Function to query the database to get the student data
function getStudentDataFromDB(studentId) {
// Write your database query here to get the student data based on the student ID
// Return the student data as an object
return {
StudentId: "001",
FirstName: "John",
LastName: "Tac",
DOB: "2015-02-10"
};
}
In this test script, the responseBody
variable gets the response body from the API response, and the studentId
variable gets the student ID from the request body. Then, the getStudentDataFromDB()
function queries the database to get the student data based on the student ID, and returns the student data as an object. Finally, the test script compares the response body with the database data using the pm.expect()
method.
Note that you will need to replace the getStudentDataFromDB()
function with your actual database query to get the student data from your database. You can also modify the test script as per your requirements to handle different scenarios and conditions.
I hope this helps you in comparing the response body with your database data in Postman tests!