How write API Tests with Postman in 3 easy steps

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
one of the most exciting features in postman is the possibility of writing tests so if you are tired of manually testing your api's then this video is perfect for you I'll take you from zero to being able to test almost any common scenario in just a couple of videos hi there this is Valentine and thanks a lot for joining me for this tutorial in case you're totally new to postman and you're just basically getting started or you don't know exactly what postman is and how everything is working I highly recommend you check out this video that I will link right here it's an introduction to postman and it will quickly get you started with the most basic things you can do with postman and then when you're interesting and testing you can just come back to this tutorial and take it right from here now in this video we'll have a look at an API and work with four different HTTP methods get post put and delete and we'll inspect the response status code and the response body and figure out how to write different kind of tests to make sure that the API is working the way we expect it to work now in case your project or whatever you're working on is a bit more complex there are two things you can do right now in the beginning the first of them is to check out the description here below I'll be posting a lot of links and everything I talked about you will find a link right there well in the description you will find a link to the postman quick reference guide and this is a document I created and it's basically a collection of a lot of useful things that you will need in postman so every time you don't know how to do something you can quickly check out a quick reference guide and figure out how to how you can do that the second thing you can check out is my online course on postman which is basically a journey for you as a tester as the developer no matter what you're doing the course will take you from zero to building requests writing tests dealing with continuous integration dealing with workflows and a lot of other stuff all along the course there will be assignments and so on so you may want to check that out as well the link is in description ok now let's get started with this tutorial for this tutorial I'll be using a web service called HTTP bin dot-org and it provides different kind of endpoints we can play around and one of these endpoints are the ones I mentioned introduction as well they get post put and delete endpoints so we're gonna play a bit with these endpoints and see how they work and how we can make some assertions on them so basically the three steps to writing tests in postman are step number one and that is to get the request to work in the first place the step number two is to understand a response body and step number three is to write the actual test so the first step is to construct a request and to manually do the step of inspecting what's going on so you probably want to say that you will get the status 200 and that the response body looks the way you expect it and in this case I'm using HTTP bender cord as I mentioned and I'm using the get endpoint and I'm passing an optional query parameter and in case I want to work with the query parameters I can do it by this panel you I get sube did I get and basically what HTTP bin is doing is passing this information back to me so let's try and have a look on how we can write our first assertion so I'm gonna switch here to the test tab and everything that happened and everything that's happening inside the test step this is JavaScript code and if you haven't done that already you should get um understanding on how JavaScript generally works and I'm sure you'll find dozens of tutorials and stuff like that it's also important to know that you can use pretty modern JavaScript here so I'm talking at least es6 so if you're looking for any tutorials look for JavaScript es6 as a version let's get started with the first assertion we can see here in the right part I have these snippets and these are some just basic examples but most of the time they are just enough to get you started and you can easily browse around them to see what you can find there and one of the most important and basic assertions that you can do while testing restful api is this is simply check the status code so if the if the API is properly working the status code is one of the first things that you want to have in your tests and now I just click status code is 200 and basically this is how a test looks like and what this test will do is to simply check if the status code from this response is 200 so let me send his request once drag this a bit into view so what you will see here is that the test result is 200 and it's also quite important all the time to make sure that your tests fail so in order to get this to fail out right here instead of get something random and I will get another status code and that is 404 not found and this is basically the test that my code here that the certian that are done works properly so ivory so I will just remove that let's run it again and we're all back now let's try to understand how this all works so every test in postman needs to start with p.m. dot test and PMD test is a function that takes two arguments the first argument is a string and that is the name of the test you will see it right here status code is 200 and the second part is a callback function and inside this callback function which you can see starts here and ends here you will write assertions and this is our first assertion that we'd done here but we can write multiple assertions inside the callback function now it's important to notice that the language that has been used here is a language that you can easily speak so this reads out as pm that responds to have status 200 and the whole point of the postman test is to write them in a language that makes sense reading them even after you have written them or if you're reading tests written by somebody else now let's have a look at more advanced scenarios we already have here a response body and you would like to make some assertions on the response body as well so how about for example checking one of the arguments that we've submitted here what if I want to check is good if it is true and for that we are gonna quickly use another snippet so I'm going to open the snippets panel and so I'm gonna click response body Jason value check and then another piece of code has been generated I previously said that the first step is to get a request to work now the second thing you need to know about is that you need to parse the response body so basically everything that comes back from the server is just a piece of data it doesn't have any meaning and the way you do that in postman is that you define a variable in this case this has been automatically generated by postman with a snippet it's called JSON data but you can name it as you like and what's happening is that p.m. response is the response body and it's being parsed as Jason now the response here is in the JSON format so this is why this works but if the response is another format and that's another tutorial you will need to first parse that response body so for example if you are getting back XML you need to parse that response to have a JavaScript object and the JavaScript object that we are actually working with because we can only work with objects because this is JavaScript code and the json data object here is a representation of what we have here in the json format okay now when this code is generated it's usually put inside a callback function but this can happen anywhere so our channels get put it outside here and let's actually use let because var is a bit outdated so what has been generated so far is something like that JSON data that value to equal this is basically the syntax now let's try to see how we can actually get to this is good part here and I'll show you a trick on how you can get it now the first thing is that I'm gonna just get it out of here and I'm gonna use a method called console at log and this gives me the possibility of logging to the console anything so every time I'm not sure what's going on what I'm getting back from what I'm getting back after parsing my response I can use something like console log to get a bit more detailed information and right here below you can click on this and you will open the postman console great now when you send a request next time there will be something locked here so this is this JSON data is an object and this object has some properties and one properties arguments arcs and the other properties headers this object has properties on its own and the way you navigate in JavaScript when you're using properties is by using a dot to connect those nested properties so let me show you exactly what I mean now the first object that we want to work with is JSON data so we want so we know that we want to get the is good from the arguments inside JSON data so the first property we need to access is arcs so I'm going to put here a dot going to write args and then because I want to access a property of the arcs object I'll put in that property name and that is is good but let's first have a look on how it's looking right now so now I only have this object which has two properties so whenever you are unsure about what to do you just use the console.log the console.log will tell you how it's going now getting decent data argh that is good and if I'm correct and the console should indicate true okay so now going back to our assertion we know that we have the data that we want to assert so this is the second important step as I said you need to parse the data and you need to get to the data that you want to assert because otherwise it makes absolutely no sense to get into this testing if you do not have a way on how to get that information and this expects how it works is basically you give in an option you give in expect something that usually you get from outside but let me just hard code it to explain you how it works so you basically expect one for example to equal one okay this is an expectation it will always be true because nothing changes here and of course if you expect two to equal one that will not work and the exertion will say that you expect it to to equal one which obviously is not the case now what we will do here is to remove this from console.log and put it right inside our expectation here and now we're gonna assert something to equal one and we expected here true to deeply equal one which is definitely not the case so once again let's say here decent data that arcs is good to equal true so let's have a look at this hmm this is still failing and you're probably wondering why and maybe you're scratching your head don't worry about it I will explain if you read more closely the error you will see here that there's a bit that there's a bit difference so you expect it true but between quotes to deeply equal true without codes so what those simple quotes in JavaScript mean is that you are dealing with a string so you basically asserting that true as a word as a string equals a boolean and they are in JavaScript different data types now every time you're unsure about what kind of data type you have you can either let such an assertion fail but you can take advantage of another important feature so if I'm going back to console I can write something like type off and gets my information and when I'm inspecting the console I will see here that this is actually a string the same happens if I remove this property now you'll have an object so you will know what kind of object you know what kind of JavaScript type this is so we know that now is good is actually not boolean even though it looks like a boolean but it's actually a string so I'm gonna write here it's good to equal true that's great now both both tests work just as well oh I don't need this console.log and I'm gonna leave this Jason data outside because I can write additional assertions here now let's imagine that we want to test is bad and that will equal false and the number of tests here remain the same even though I have different assertions and if I make this assertion fail you will tell you exactly which test failed and which was the expectation that failed now writing test in this way is a good way to organize things around there's nothing saying that you cannot combine this together for example you can even put this assertion here that's absolutely no problem the only thing you need to remember when using Jason data is that you need to first define it no I'll let you do the rest of it for example how about checking that headers cache-control is no cash and write a comment on how you sold it okay guys thanks for watching this video and if you enjoyed it please give it a thumbs up you can continue with the next video by clicking right here come on click right here don't keep me waiting
Info
Channel: Valentin Despa
Views: 159,401
Rating: undefined out of 5
Keywords: postman, api-tests, postman-es6, postman-javascript, postman-tests, newman
Id: Qlvbc6kIBOk
Channel Id: undefined
Length: 18min 49sec (1129 seconds)
Published: Thu Apr 26 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.