Just Put, Post, and Delete.

Great now we will start testing our post, put and delete endpoints. If you have not done previous steps, or haven’t tested your get endpoints. I would recommend to go back to previous steps.

Now let’s get’s going by testing our create product endpoint. We will create sample data. Put this in the body of the request, and set it to application/json. Which will be sample data we will test.

Go to the body tab and copy the data in the picture.

If you are having problems seeing the sample data in the picture, here is the sample data, or you can create your own as long it has a name, price, and description. NOTE: You’ll have change what your checking in the request body.

{
"name": "Basketball",
"description": "A Nice Basketball",
"price": 50
}

Now we will test the response to check if it is less than 200 milliseconds. Also we are gonna check if the requestBody has a name, and description. We are gonna make a test pass and make a test fail. Here is a article about test cases.

First go to your tests tab in postman, and now assign a variable called resTime, and requestBody which are assigned to the pm.response.responseTime and pm.request.body.raw. NOTE: pm.request.body.raw is a string.

//Set the body of the response to variable, since we are gonna test if they have the right properties.
const requestBody = pm.request.body.raw;
//Set the responseTime of the response to a variable, since we are gonna test the amount of time to takes for the response.
const resTime = pm.response.responseTime;

Lets check if the resTime is less than 200 ms, using the .to.be.below.

//Check the amount of time it takes for creating the data.
pm.test('Check if the amount of time it takes to create the data is less than 200ms', () => {
//Use the to.be.below if it is less than a certain number, in this case if it is less than 200 milliseconds.
pm.expect(resTime).to.be.below(200);
})

Then we will check if the requestBody has a certain string, which will be properties we are checking for. We will check if it includes “name”:” Basketball”.

//Since the raw data of the body is a string. 
//Check if it includes name: Basketball.
pm.test('Name of the created product is named correctly.', function() {
pm.expect(requestBody).to.include('"name": "Basketball"');
})

Now we will make the test purposely fail. By checking if it has a description with a value called Basketball.

//Now make it purposely make it fail by checking if description is equal to Basketball.
pm.test('New created product has right description.', function() {
pm.expect(requestBody).to.include('"description": "Basketball"');
})

Great congratulations you know how to do test a post endpoint using postman!

Let’s do our final post endpoint which will be the login. We will not need a register endpoint, because users will use their favorite social media to login via Auth0, something we will configure down the road.

First input this sample data like we did earlier. Copy and paste this sample data.

{
"sub": "github|23343715",
"nickname": "AliA1997",
"name": "Ali Qassem Alhaddad",
"picture": "https://avatars1.githubusercontent.com/u/23343715?v=4"
}

We will do the same we will check if the response time is less than 200 milliseconds.

//Set the request body from the request body to a variable, since we are gonna check if a auth0_id is being passed in
const requestBody = pm.request.body.raw;
//Set the time of the response to a variable, since we are checking if the responseTime is lower than a specified number.
const resTime = pm.response.responseTime;
//Initiate the test to test if the responseTime is lower than a given number.
pm.test('Check is response is lower than 200 milliseconds', () => {
pm.expect(resTime).to.be.below(200);
})

Then we will check if current properties are contained in the request body. Also we make the test purposely by checking if our name, (Ali Qassem Alhaddad) is our Auth0_id.

//Now test if the request body has a auth0_id, via include.
pm.test('Check if the requestBody string includes a auth0_id', () => {
pm.expect(requestBody).to.include('"sub": "github|23343715"')
})
//Now make the test purposely fail, by checking if auth0_id contains the wrong property
pm.test('Check if requestBody string includes auth0_id to have wrong property', () => {
pm.expect(requestBody).to.include('"sub": "Ali Qassem Alhaddad"')
})

Now run your tests, and you should have 2/3.

Thats it for our post endpoints, let now get started testing our put endpoints.

We will test update product which will update the current product. We will have sample data just like we did in our post endpoint, and we will have 3 correct tests, and 1 test that will fail.

Lets first start putting in our test data in the body tab.

{
"name": "Soccer Ball",
"description": "A nice soccer ball",
"price": 10
}

We will start assigning our variables that we need to perform the tests. In our tests tab. We will have a variable assigned to the url which will be used to check if we are passing in the correct id.( Do a get for a product, and copy paste the id from one of the products to update it) Then we will have resTime and requestBody like we did in our post tests.

//Testing if we got the right id, by setting the requestUrl to request.url.toString()
const requestUrl = pm.request.url.toString();
//Test if we got a right name from our requestBody
const requestBody = pm.request.body.raw;
//Test if the responseTime is less than 200 ms, must get responseTime.
const resTime = pm.response.responseTime

Great let’s start running our tests. So we will start testing our response time to check if it is under 200 milliseconds and check if our request body has name: Soccerball. Again if you have your own custom sample data, you just need to have at least three tests pass.

//Test if the responseTime is less than 200ms
pm.test('Test if responseTime is less than 200 milliseconds', () => {
pm.expect(resTime).to.below(200);
})
//Test if the request body contains right property.
pm.test('Test if the name is Soccerball by checking if it includes name: Soccerball', () => {
pm.expect(requestBody).to.include('"name": "Soccer Ball"')
})

Now we will check the url of the request if it contains the id we passed in.

//Test if the url includes the correct id.
pm.test('Test if the url includes the correct id', () => {
pm.expect(requestUrl).to.include('5b61d670a0b6670c2dd85d25');
})

To finish it off, we will start creating our test we want to purposely fail.

//Now make the test purposely fail.
pm.test('Test if the responseTime is more than 200 milliseconds', () => {
//Use to.above to check if the responseTime is greater than 200 milliseconds
pm.expect(resTime).to.above(200);
})

Now run your tests, and you should have 3/4.

Awesome one more test we can move to the frontend, and start creating our component. We’ll start on our delete endpoint to test, we will not need a body since we do not need to pass anything to the body.

We will start by doing a get on our /api/product endpoint, and copy the id of the product you want to delete and paste in the url. Then we will go to our tests tab in our delete endpoint, and starting typing our tests.

First assign the variable, so we would a variable assigned the pm.response.json(), and also would it assigned to the pm.response.url.

//Get the current response from the postman global object which will be of type json.
let response = pm.response.json();
const requestUrl = pm.request.url;

We are gonna have two tests that pass, and one test that fails. We’ll start our tests with a very simple one checking if the response status is 200. Then we will check if the url contains the correct id.

//Do the pm.test to perform a test with a note, which can be anything, and a callback performing the test.
pm.test('check the status code is 200', () => {
//Use chai language chains to improve the readability of the test on the pm object.
pm.response.to.have.status(200);
})
//Do a pm.test to perform a test with a note, which can be anything, and a callback performing the test.
pm.test('Check is the url has the right id', () => {
//Convert the requestUrl to a string, and see if it includes the correct id.
pm.expect(requestUrl.toString()).to.include('5b3dbaff2bae8e1a2ad6ed59');
})

Finally to finish our testing off, we will make our test purposely fail, by checking if the response is an array, which it isn’t it is a object.

//Now we are gonna make it purposely fail.
pm.test('check if response is a array', () => {
//Use chai language chains to improve the readability of the test on the response VARIABLE.
//Test if the response is an object
pm.expect(response).to.be.an('array');
})

Good Job! We just finished our endpoint testing for our application. Now we will start the react side of things in step 5A.

Here is the my github for reference and the postman collection.

Now we will setup our frontend!

Happy Coding!!!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store