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!
fork
andclone
this repository.cd
into the root of the project.- Run
npm install
. - Run
nodemon
to 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
import
button located in the top left corner of Postman.- The file you are importing is inside of the
postman_collection
folder 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 Users
request. - Click on the
Send
button 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 ID
request. - Click on the
Send
button 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
Array
with a length of1
. - Create a test to verify the returned data has an object with the following properties:
id
equal to9
.first_name
equal to"Tatum"
.last_name
equal to"Vell"
.email
equal to"[email protected]"
city
equal to"Youngstown"
.state
equal to"Ohio"
.phone
equal to"(330) 6802507"
GET - User by ID
const expectedObject = {
id: 9,
first_name: "Tatum",
last_name: "Vell",
email: "[email protected]",
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
Send
button 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 Query
request. - Click on the
Send
button 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
Send
button 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 ID
request. - Click on the
Send
button 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
Array
with a length of1
. - Create a test to verify the returned user has an updated object with the following properties:
email
should equal"[email protected]"
.city
should 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 '[email protected]'", function() {
pm.expect(user.email).to.eql("[email protected]");
});
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
Send
button 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 user
request. - Click on the
Send
button 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
with a length of1
. - Create a test to verify the returned user has the following data:
first_name
equals"Bruce"
.last_name
equals"Wayne"
.email
equals"[email protected]"
.city
equals"Gotham"
.state
equals"New Jersey"
.phone
equals"(856) 6044252"
.
- Create a test to verify the returned users has an
id
property 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: "[email protected]",
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
Send
button 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 user
request. - Click on the
Send
button 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
id
is 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
Send
button 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 arrow
next to the collection name. - Click the
Run
button. - Select the correct collection from the list on the left.
- Click the
blue button
in 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.