In this project, we will learn the basics of endpoint testing using Postman. Postman is a REST client that we will be making HTTP requests from. We will learn how to write Postman tests for the responses from the HTTP requests. You can read more information about Postman tests by clicking here.
forkandclonethis repository.cdinto the root of the project.- Run
npm installto get the project dependencies. - Run
nodemon.
In this step, we will import a collection of requests to Postman.
- Open Postman.
- Click on the
importbutton located in the top left corner of Postman.- The file you are importing is inside of the
postman_collectionfolder in this repo.
- The file you are importing is inside of the
- After importing, you should have a collection called
Endpoint Testing Mini Project. If you click on it, the list of requests should expand/close.
In this step, we will create a Postman test for fetching all students.
- Click on the first request from the collection
GET - All Students.- When you click the blue
Sendbutton you should see all the student data in theBodytab.
- When you click the blue
- Click on the
Teststab under the request url. - Create a test that checks that the status code is
200. - Create a test that checks that 20 students are returned.
- Click on
Sendagain to see if your tests pass or fail.
GET - All Students
const response = pm.response.json();
pm.test("Status code is 200", function() {
pm.response.to.have.status(200);
});
pm.test("All 20 student objects in response", function() {
pm.expect(response.length).to.eql(20);
});In this step, we will create a Postman test for fetching students by ID.
- Click on the second request from the collection
GET - Student by Id. - Click the
Sendbutton.- You should see only the student data for the student with the id of
9.
- You should see only the student data for the student with the id of
- Create a test that checks that the status code is
200. - Create a test that checks that student
9has the following information:idequal to9.studentequal to"Patsy Daunay".email_addressequal to"pdaunay8@about.com".phoneequal to"(806) 2654555".current_gradeequal toA.
GET - Student by Id
const response = pm.response.json();
const student9 = {
id: 9,
student: "Patsy Daunay",
email_address: "pdaunay8@about.com",
phone: "(806) 2654555",
current_grade: "A"
};
pm.test("Status code is 200", function() {
pm.response.to.have.status(200);
});
pm.test("Correct object in response for ID 9", function() {
pm.expect(response).to.eql(student9);
});In this step, we will create a Postman test for fetching students by email.
- Click on the third request from the collection
GET - Students by Email. - Click the
Sendbutton.- You should see the student who's email equals
gdee@clickbank.net.
- You should see the student who's email equals
- Create a test that checks that the status code is
200. - Create a test that checks that the student's email is equal to
gdee@clickbank.net.
GET - Student by Email
let response = pm.response.json();
pm.test("Status code is 200", function() {
pm.response.to.have.status(200);
});
pm.test("Gilbert's student object in response", function() {
pm.expect(response.email).to.eql("gdee@clickbank.net");
});In this step, we will create a Postman test for fetching students by name.
- Click on the fourth request from the collection
GET - Students by Name - Click the
Sendbutton.- You should see only the student data where their name contains
la.
- You should see only the student data where their name contains
- Create a test that checks that the status code is
200. - Create a test that checks
Abey Laynardappears in the results.
GET - Students by Name
const res = pm.response.json();
pm.test("Status code is 200", function() {
pm.response.to.have.status(200);
});
pm.test("Abey Laynard student object in response", function() {
const studentExists = response.some(
student => student.student === "Abey Laynard"
);
pm.expect(studentExists).to.be.true;
});In this step, we will create a Postman test for fetching students by grade.
- Click on the fifth request from the collection
GET - Students by Grade. - Click the
Sendbutton.- You should see only the student data where their grade equals
C.
- You should see only the student data where their grade equals
- Create a test that checks that the status code is
200. - Create a test that checks that all returned students have a grade of
C.
GET - Students by Grade
const res = pm.response.json();
pm.test("Status code is 200", function() {
pm.response.to.have.status(200);
});
pm.test('Correct students returned for "C" grade', function() {
const correctGrades = response.every(
student => student.current_grade === "C"
);
pm.expect(correctGrades).to.be.true;
});In this step, we will create a Postman test for fetching students by phone.
- Click on the sixth request from the collection
GET - Students by Phone. - Click the
Sendbutton.- You should see only the student data where their phone contains
608.
- You should see only the student data where their phone contains
- Create a test that checks that the status code is
200. - Create a test that checks that all returned students have a
phoneproperty that contains608.
GET - Students by Phone
const res = pm.response.json();
pm.test("Status code is 200", function() {
pm.response.to.have.status(200);
});
pm.test("All objects contain '608'", function() {
const checkPhoneNumbers = response.every(student =>
student.phone.includes("608")
);
pm.expect(checkPhoneNumbers).to.be.true;
});In this step, we will create a Postman test for updating a student's grade.
- Click on the seventh request from the collection
PUT - Update Grade. - Click the
Sendbutton.- You should see the student data where the
idequals15and thegradeequalsA-.
- You should see the student data where the
- Create a test that checks that the status code is
200. - Create a test that checks that the id is equal to
15. - Create a test that checks that returned data has a
gradeofA-.
PUT - Update Grade
const res = pm.response.json();
pm.test("Status code is 200", function() {
pm.response.to.have.status(200);
});
pm.test("Returns student with correct Id", function() {
pm.expect(response.id).to.eql(15);
});
pm.test("Correctly updates grade to A-", function() {
pm.expect(response.current_grade).to.eql("A-");
});In this step, we will create a Postman test for adding a new student.
- Click on the eighth request from the collection
POST - Add Student. - Click the
Sendbutton.- You should see student data for a student with an
idcreated by the server and properties that match the data sent in the body.
- You should see student data for a student with an
- Create a test that checks that the status code is
200. - Create a test that checks the student has an
id. - Create a test that checks if the student matches the expected schema.
- Create a test that checks that student has the following information:
studentequal to"Tim Allen".email_addressequal to"tim@homeimprovement.com".phoneequal to"(408) 8674530".
POST - Add Student
const res = pm.response.json();
const schema = {
title: "Student",
type: "object",
properties: {
id: {
type: "integer"
},
student: {
type: "string"
},
email_address: {
type: "string"
},
phone: {
type: "string"
},
current_grade: {
type: "string"
}
},
required: ["id", "student", "email_address", "phone", "current_grade"]
};
pm.test("Status code is 200", function() {
pm.response.to.have.status(200);
});
pm.test("Student was created", function() {
pm.expect(response.id).to.exist;
});
pm.test("Student should match schema", function() {
pm.expect(tv4.validate(response, schema)).to.be.true;
});
pm.test("Student has correct information", function() {
pm.expect(res.student).to.eql("Tim Allen");
pm.expect(res.email_address).to.eql("tim@homeimprovement.com");
pm.expect(res.phone).to.eql("(408) 8674530");
});In this step, we will create a Postman test for removing a student.
- Click on the ninth request from the collection
DELETE - Remove Student. - Click the
Sendbutton.- You should see the student data of the student with the
idof18.
- You should see the student data of the student with the
- Create a test that checks that the status code is
200. - Create a test that checks that the return student
idis equal to18.
DELETE - Remove Student
const res = pm.response.json();
pm.test("Status code is 200", function() {
pm.response.to.have.status(200);
});
pm.test("Student w/ ID 18 removed", function() {
pm.expect(res.id).to.eql(18);
});In this step, we'll restart our node server to get the default data set. We'll then run the entire postman collection to see them all pass as a whole.
- Click on the right arrow next to the collection name.
- Click the blue
Runbutton. - Select the
Endpoint Testing Mini Projectcollection. - Click the blue
Run Endpoint..button.
If you see a problem or a typo, please fork, make the necessary changes, and create a pull request so we can review your changes and merge them into the master repo and branch.
© DevMountain LLC, 2017. Unauthorized use and/or duplication of this material without express and written permission from DevMountain, LLC is strictly prohibited. Excerpts and links may be used, provided that full and clear credit is given to DevMountain with appropriate and specific direction to the original content.










