Skip to content

An afternoon project to help solidify testing endpoints using Postman.

Notifications You must be signed in to change notification settings

DevMountain/endpoint-testing-afternoon

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Project Summary

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!

Setup

  • fork and clone 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.

Step 1

Summary

In this step, we'll import the Postman collection into Postman.

Instructions

  • 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.
  • After importing, you should have a collection called Endpoint Testing Afternoon.

Solution

Step 2

Summary

In this step, we will create a Postman test for fetching all users.

Instructions

  • 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.

Solution

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);
});

Step 3

Summary

In this step, we will create a Postman test for fetching users by ID.

Instructions

  • 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 of 1.
  • Create a test to verify the returned data has an object with the following properties:
    • id equal to 9.
    • 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"

Solution

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);
});

Step 4

Summary

In this step, we will create a Postman test for fetching a user by ID that returns an error.

Instructions

  • 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".

Solution

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");
});

Step 5

Summary

In this step, we will create a Postman test for fetching users with a query.

Instructions

  • 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.

Solution

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);
});

Step 6

Summary

In this step, we will create a Postman test for fetching users with a query that returns an error.

Instructions

  • 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".

Solution

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");
});

Step 7

Summary

In this step, we will create a Postman test for updating a user by ID.

Instructions

  • 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 of 1.
  • Create a test to verify the returned user has an updated object with the following properties:

Solution

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");
});

Step 8

Summary

In this step, we will create a Postman test for updating a user by ID that returns an error.

Instructions

  • 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.".

Solution

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.");
});

Step 9

Summary

In this step, we will create a Postman test for creating a new user.

Instructions

  • 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 of 1.
  • 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 a number.

Solution

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");
});

Step 10

Summary

In this step, we will create a Postman test for creating a user that returns an error.

Instructions

  • 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.".

Solution

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.");
});

Step 11

Summary

In this step, we will create a Postman test for removing a user by ID.

Instructions

  • 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 to 66.

Solution

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);
});

Step 12

Summary

In this step, we will create a Postman test for removing a user that returns an error.

Instructions

  • 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.".

Solution

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.");
});

Step 13

Summary

In this step, we'll restart the node server and run the Postman collection of tests as a whole.

Instructions

  • 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.

Solution

Contributions

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.

Copyright

© 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.

About

An afternoon project to help solidify testing endpoints using Postman.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •