In this project, we will practice the basics of endpoint testing in Postman. Using a provided Postman collection, we'll create tests for a server's endpoints.
If you need to refresh on Postman's documentation: Click Me!
forkandclonethis repository.cdinto the root of the project.- Run
npm install. - Run
nodemonto start up the server.- The server will run on port
3535, do not change this port.
- The server will run on port
In this step, we'll import the Postman collection into 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 Afternoon.
In this step, we will create a Postman test for fetching all users.
- Click on the
GET - All Usersrequest. - Click on the
Sendbutton to see the returned data. - Create a test to verify the returned status code is
200. - Create a test to verify the returned data is an
Array. - Create a test to verify the returned data has a length of
100.
GET - All Users
const responseJSON = pm.response.json();
pm.test("Status code is 200", function() {
pm.response.to.have.status(200);
});
pm.test("Returned data is an array", function() {
pm.expect(Array.isArray(responseJSON)).to.eql(true);
});
pm.test("Returned data has a length of 100", function() {
pm.expect(responseJSON.length).to.eql(100);
});In this step, we will create a Postman test for fetching users by ID.
- Click on the
GET - User by IDrequest. - Click on the
Sendbutton to see the returned data. - Create a test to verify the returned status is
200. - Create a test to verify the returned data is an
Arraywith a length of1. - Create a test to verify the returned data has an object with the following properties:
idequal to9.first_nameequal to"Tatum".last_nameequal to"Vell".emailequal to"tvell8@wisc.edu"cityequal to"Youngstown".stateequal to"Ohio".phoneequal to"(330) 6802507"
GET - User by ID
const expectedObject = {
id: 9,
first_name: "Tatum",
last_name: "Vell",
email: "tvell8@wisc.edu",
city: "Youngstown",
state: "Ohio",
phone: "(330) 6802507"
};
const responseJSON = pm.response.json();
pm.test("Status code is 200", function() {
pm.response.to.have.status(200);
});
pm.test("Returned data is an Array with length of 1", function() {
pm.expect(Array.isArray(responseJSON)).to.eql(true);
pm.expect(responseJSON.length).to.eql(1);
});
pm.test("Returned data is expected", function() {
pm.expect(responseJSON[0]).to.eql(expectedObject);
});In this step, we will create a Postman test for fetching a user by ID that returns an error.
- Click on the
GET - User by ID ( error )request. - Click on the
Sendbutton to see the returned data. - Create a test to verify the returned status is
400. - Create a test to verify the returned message is
"User id sent must be a number".
GET - User by ID ( error )
pm.test("Status code is 400", function() {
pm.response.to.have.status(400);
});
pm.test("Returned error message is expected", function() {
pm.expect(pm.response.text()).to.eql("User id sent must be a number");
});In this step, we will create a Postman test for fetching users with a query.
- Click on the
GET - User with Queryrequest. - Click on the
Sendbutton to see the returned data. - Create a test to verify the returned status is
200. - Create a test to verify the return data set has a length greater than
0.
GET - User with Query
const responseJSON = pm.response.json();
pm.test("Status code is 200", function() {
pm.response.to.have.status(200);
});
pm.test("Return data has a length greator than 0", function() {
pm.expect(responseJSON.length > 0).to.eql(true);
});In this step, we will create a Postman test for fetching users with a query that returns an error.
- Click on the
GET - User with Query ( error )request. - Click on the
Sendbutton to see the returned data. - Create a test to verify the returned status is
400. - Create a test to verify the returned message is
"Improper query sent in request: citty=new york".
GET - User with Query ( error )
pm.test("Status code is 400", function() {
pm.response.to.have.status(400);
});
pm.test("Returned error message is expected", function() {
pm
.expect(pm.response.text())
.to.eql("Improper query sent in request: citty=new york");
});In this step, we will create a Postman test for updating a user by ID.
- Click on the
PUT - Update user by IDrequest. - Click on the
Sendbutton to see the returned data. - Create a test to verify the returned status is
200. - Create a test to verify the returned data is an
Arraywith a length of1. - Create a test to verify the returned user has an updated object with the following properties:
emailshould equal"garey@ilovecode.com".cityshould equal"Pittsburg".
PUT - Update user by ID
const responseJSON = pm.response.json();
pm.test("Status code is 200", function() {
pm.response.to.have.status(200);
});
pm.test("Returned data is an Array with a length of 1", function() {
pm.expect(Array.isArray(responseJSON)).to.eql(true);
pm.expect(responseJSON.length).to.eql(1);
});
const user = responseJSON[0];
pm.test("Returned email is 'garey@ilovecode.com'", function() {
pm.expect(user.email).to.eql("garey@ilovecode.com");
});
pm.test("Returned city is 'Pittsburg'", function() {
pm.expect(user.city).to.eql("Pittsburg");
});In this step, we will create a Postman test for updating a user by ID that returns an error.
- Click on the
PUT - Update User by ID ( error )request. - Click on the
Sendbutton to see the returned data. - Create a test to verify the returned stats is
400. - Create a test to verify the returned message is
"Error with user ID in request.".
PUT - Update User by ID ( error )
pm.test("Status code is 400", function() {
pm.response.to.have.status(400);
});
pm.test("Returned error message is expected", function() {
pm.expect(pm.response.text()).to.eql("Error with user ID in request.");
});In this step, we will create a Postman test for creating a new user.
- Click on the
POST - Create userrequest. - Click on the
Sendbutton to see the returned data. - Create a test to verify the returned status code is
200. - Create a test to verify the returned data is an
Arraywith a length of1. - Create a test to verify the returned user has the following data:
first_nameequals"Bruce".last_nameequals"Wayne".emailequals"bruce@scarybat.com".cityequals"Gotham".stateequals"New Jersey".phoneequals"(856) 6044252".
- Create a test to verify the returned users has an
idproperty that equals anumber.
POST - Create user
const responseJSON = pm.response.json();
const user = responseJSON[0];
const expectedUser = {
id: user.id,
first_name: "Bruce",
last_name: "Wayne",
email: "bruce@scarybat.com",
city: "Gotham",
state: "New Jersey",
phone: "(856) 6044252"
};
pm.test("Status code is 200", function() {
pm.response.to.have.status(200);
});
pm.test("Returned data is an Array with a length of 1", function() {
pm.expect(Array.isArray(responseJSON)).to.eql(true);
pm.expect(responseJSON.length).to.eql(1);
});
pm.test("Returned user is expected", function() {
pm.expect(user).to.eql(expectedUser);
});
pm.test("Returned user id is a number", function() {
pm.expect(typeof user.id).to.eql("number");
});In this step, we will create a Postman test for creating a user that returns an error.
- Click on the
POST - Create user ( error )request. - Click on the
Sendbutton to see the returned data. - Create a test to verify the returned status code is
400. - Create a test to verify the returned message is
"All needed user info was not sent in the body of request.".
POST - Create user ( error )
pm.test("Status code is 400", function() {
pm.response.to.have.status(400);
});
pm.test("Returned error message is expected", function() {
pm
.expect(pm.response.text())
.to.eql("All needed user info was not sent in the body of request.");
});In this step, we will create a Postman test for removing a user by ID.
- Click on the
DELETE - Remove userrequest. - Click on the
Sendbutton to see the returned data. - Create a test to verify the returned status code is
200. - Create a test to verify the returned user's
idis equal to66.
DELETE - Remove user
const user = pm.response.json()[0];
pm.test("Status code is 200", function() {
pm.response.to.have.status(200);
});
pm.test("Returned user ID is equal to 66", function() {
pm.expect(user.id).to.eql(66);
});In this step, we will create a Postman test for removing a user that returns an error.
- Click on the
DELETE - Remove user ( error )request. - Click on the
Sendbutton to see the returned data. - Create a test to verify the returned status code is
404. - Create a test to verify the returned message is
"No user with an ID of 508.".
DELETE - Remove user ( error )
pm.test("Status code is 404", function() {
pm.response.to.have.status(404);
});
pm.test("Returned error message is expected", function() {
pm.expect(pm.response.text()).to.eql("No user with an ID of 508.");
});In this step, we'll restart the node server and run the Postman collection of tests as a whole.
- Restart the
sever. - Click on the
right arrownext to the collection name. - Click the
Runbutton. - Select the correct collection from the list on the left.
- Click the
blue buttonin the bottom at the bottom of the left side-menu.
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.














