Giter Site home page Giter Site logo

endpoint-testing-afternoon's Introduction

endpoint-testing-afternoon

Unit Testing - Day 2 - ( Postman )

Project Summary

In this project, we will learn about 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.

Postman docs: https://www.getpostman.com/docs/postman/scripts/test_scripts

Step 1

  • Run npm install in the terminal to install dependencies.
  • Run nodemon in the terminal. The package.json file has already been configured to run the server file on the execution of the nodemon command. Do not change the port from 3535.

Step 2

Summary

We will import a collection of requests to 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. If you click on it, the list of requests should expand/close;

Step 3

Summary

In this step we will be writing tests to check the response that comes back from the server.

Instructions

GET - All Users

  • Click on the first request (GET - All Users). You will not need to change the method, request url, or the body of the request. All the needed request information has already been filled out.
  • Click the blue Send button and look at the response that we get. The server will be sending back an array of all user objects.
  • Select the Tests tab, located under the request url. We will be writing the tests in the box below.
  • Tests:
    • Should be getting a status code of 200 if the request is successful. In the snippets section on the right, click on Status code: Code is 200. This will insert a snippet that tests whether or not the status code is 200.

      • Take a look at the snippet we just added. This is how we will structure and write most of our Postman tests.
      
      pm.test('description of test here', function() {
        // What we want tested here
        pm.expect( [data being tested] ).to.eql( [value] )
      })
      
    • Next, check if the data that came back as a response is an array.

    • NOTE: You will need to parse the response to check any values: pm.response.json().

      Solution
      pm.test('Response is array', function() {
          pm.expect(Array.isArray(pm.response.json())).to.eql(true);
      })
      
    • We know that we have exactly 100 users right now. Test to make sure 100 user objects got returned.

      Solution
      pm.test('100 user objects in reponse', function() {
          pm.expect(pm.response.json().length).to.eql(100)
      })
      

GET - User By ID

Time to move on to the next request: GET - User By ID. Select the request and go to the Tests tab. This request hits an enpoint that returns a specific user by the user's ID.

Write tests for the following:

  • Status should be 200.

  • Should respond with an array of 1 user object.

  • The user with an ID of 9 is a test user that was put in our database and will never be deleted by a user.

    • Response object should have values of:
        {
            "id": 9,
            "first_name": "Tatum",
            "last_name": "Vell",
            "email": "[email protected]",
            "city": "Youngstown",
            "state": "Ohio",
            "phone": "(330) 6802507"
        }
    
    Solution
    const res = pm.response.json();
    
    pm.test("Status code is 200", function () {
        pm.response.to.have.status(200);
    });
    
    pm.test('Length of respose should be 1', function() {
        pm.expect(res.length).to.eql(1)
    })
    
    pm.test('Correct user info returned for ID 9', function() {
        const tatum =     {
            "id": 9,
            "first_name": "Tatum",
            "last_name": "Vell",
            "email": "[email protected]",
            "city": "Youngstown",
            "state": "Ohio",
            "phone": "(330) 6802507"
        }
        let correctInfo = true;
        for (let prop in tatum) {
            if (tatum[prop] !== res[0][prop]) correctInfo = false;
        }
        pm.expect(correctInfo).to.eql(true);
    })
    

GET - User By ID (ERROR)

This test is checking what gets returned when an ID is not correctly sent.

Write tests for the following:

  • When text is sent instead of a valid ID, the status code should be 400.

  • Message sent: User id sent must be a number

    • HINT: To check the text sent as the body of the response, use:
    pm.expect(pm.response.text()).to.include("String you expect");
    
    Solution
    pm.test("Status code is 400", function () {
        pm.response.to.have.status(400);
    });
    
    pm.test('Message: User id sent must be a number', function () {
        pm.expect(pm.response.text()).to.include("User id sent must be a number");
    });
    

GET - Search W/ Query

You can search for any user objects using queries. You can search with as little as just one letter. Example:

  • ?firstName=jo
  • ?lastName=R
  • ?email=jacey14@
  • ?city=new
  • ?state=ca
  • ?phone=801

Test for the following:

  • Status should be 200.

  • Since we have a test user in the database with the letter 't' (Tatum Vell) in their first name, the response length should always be greater than zero (given the search query: ?firstName=t).

    Solution
    pm.test("Status code is 200", function () {
        pm.response.to.have.status(200);
    });
    
    pm.test('Length of response > 0; given: firstName=t', function() {
        let greaterThanZero = true;
        if (pm.response.json().length < 1) greaterThanZero = false;
        pm.expect(greaterThanZero).to.eql(true);
    })
    
    

GET - Search W/ Query (ERROR)

Test for the following:

When a query parameter is misspelled, an error message will be sent.

  • Status should be 400.

  • Message sent in body: Improper query sent in request

    Solution
    pm.test("Status code is 400", function () {
        pm.response.to.have.status(400);
    });
    
    pm.test("Correct error message", function () {
        pm.expect(pm.response.text()).to.include("Improper query sent in request");
    });
    

PUT - Update User By Id

This endpoint will update a user, by the given ID, with the information sent in the body.

  • Response: user object that was updated.

Test for the following:

  • Status should be 200.
  • User with ID 23 should have the following information updated:
Solution
pm.test("Status code is 200", function () {
    pm.response.to.have.status(200);
});

pm.test('Email and city updated for user w/ ID 23', function() {
    let res = pm.response.json();
    let infoUpdated = true;
    if (res[0].email !== '[email protected]' || res[0].city !== 'Pittsburg') {
        infoUpdated = false;
    }
    pm.expect(infoUpdated).to.eql(true);
})

PUT - Update User By Id (ERROR: text)

Write tests for when text is sent instead of a number for the user ID.

Test for the following:

  • Status code should be 400.
  • Error message: Error with user ID in request.
Solution
pm.test("Status code is 400", function () {
    pm.response.to.have.status(400);
});

pm.test("Error message: Error with user ID in request.", function () {
    pm.expect(pm.response.text()).to.include("Error with user ID in request.");
});

PUT - Update User By ID (ERROR: User not found)

Write tests for when an ID is used that does not exist in the database.

Test for the following:

  • Status code should be 404.
  • Error message: User not found.
Solution
pm.test("Status code is 400", function () {
    pm.response.to.have.status(404);
});

pm.test("Error message: User not found.", function () {
    pm.expect(pm.response.text()).to.include("User not found.");
});

POST - Create New User

New user information will be sent in the body of the request.

  • Response: user that was created.

Test for the following:

  • Status code should be 200.
  • User created should have the following key/value pairs:
    {
      "first_name": "Bruce",
      "last_name": "Wayne",
      "email": "[email protected]",
      "city": "Gotham",
      "state": "New Jersey",
      "phone": "(856) 6044252"
    }
    
  • An ID should have been auto-generated for the new user created. Make sure the ID exists and is a valid number.
Solution
let res = pm.response.json();

pm.test("Status code is 200", function () {
    pm.response.to.have.status(200);
});

pm.test('User was added with correct user info', function() {
    let newUser = {
    "first_name": "Bruce",
    "last_name": "Wayne",
    "email": "[email protected]",
    "city": "Gotham",
    "state": "New Jersey",
    "phone": "(856) 6044252"
  }
  let correctUserInfo = true;
  for (let prop in newUser) {
      if (newUser[prop] !== res[0][prop]) correctUserInfo = false;
  }
    pm.expect(correctUserInfo).to.equal(true);  
})

pm.test('ID exists and is a valid number', function() {
    pm.expect( typeof res[0].id ).to.eql('number');
})

POST - Create New User (ERROR: Incomplete body)

Write tests for when a new user is being created and all needed information is not sent.

Test for the following:

  • Status code should be 400.
  • Error message: All needed user info was not sent in the body of request.
Solution
pm.test("Status code is 400", function () {
    pm.response.to.have.status(400);
});

pm.test("Error message: All needed user info was not sent in the body of request.", function () {
    pm.expect(pm.response.text()).to.include("All needed user info was not sent in the body of request.");
});

DELETE - Remove User

The endpoint will remove a user by the given user ID.

  • Response: the user object that was removed.

Test for the following:

  • Status code should be 200.
  • The ID of the object that was removed is 66 (ID that was sent in request).
Solution
pm.test("Status code is 200", function () {
    pm.response.to.have.status(200);
});

pm.test('Removed user has ID of 66', function() {
    pm.expect(pm.response.json()[0].id).to.eql(66);
})

DELETE - Remove User (ERROR: User does not found)

Write tests for when an ID is used to remove a user and that ID does not match anyone in the database.

Test for the following:

  • Status code should be 404.
  • Error message: No user with an ID of 508.
    • Only test for part of the message: No user with an ID of. The ID in the message will change based on what ID is sent.
Solution
pm.test("Status code is 404", function () {
    pm.response.to.have.status(404);
});

pm.test("Error message: No user with an ID of", function () {
    pm.expect(pm.response.text()).to.include("No user with an ID of");
});

DELETE - Remove User (ERROR)

This request is attempting to delete a user, but is sending five instead of the number 5 as the ID parameter.

Test for the following:

  • Status code should be 400.
  • Error message: Error with user ID in request.
Solution
pm.test("Status code is 400", function () {
    pm.response.to.have.status(400);
});

pm.test("Error message: Error with user ID in request.", function () {
    pm.expect(pm.response.text()).to.include("Error with user ID in request.");
});

Step 4

  • Restart nodemon.
  • Run the entire collection of requests and tests.
    • Click on the right arrow next to the collection name.
    • Click the blue Run button. The collection runner will open.
    • Select the correct collection from the list on the left. Then click the blue button in the bottom at the bottom of the left side-menu.

You should see all of your tests passing. Make sure you restart the server every time you re-run the collection runner.

endpoint-testing-afternoon's People

Contributors

joeblank avatar devlemire avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.