#100DaysOfWeb in Python Transcripts
Chapter: Days 9-12: API Star
Lecture: Testing your API with pytest

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Next we're going to write some tests with pytest. So we have been doing all this testing with Postman but it's all manual
0:08 so every time you make a change you have to go through GET, POST, DELETE, and specify the url. It's a lot of work.
0:16 So we can automate that with API Star TestClient. And it's not bound to any framework, so you can use unitest as well but I prefer pytest.
0:26 I'm not going to go too much into pytest, that's going to fill out of course by itself but basically you can just
0:31 write claim functions and make asserts. So you put this in a test file and just run pytest and you actually wrote your first test.
0:40 So we'll see that a bit more in detail. Now what API Star is concerned it makes it pretty easy.
0:46 It provides a test client that supports get, post, put and all the methods and you give it an end point and it returns a response object
0:54 which holds a status code, and a JSON response which can then inspect. So this is pretty easy. Let me just copy this.
1:04 I made a template with all the steps of all the test methods we are going to write. And I'm not going to write them all because that would
1:11 take a lot of time but let's do one or two and then I will just show you the completed code as to get an idea how I do my testing of the API.
1:23 So I copied this from the documentations. Let's do a get request to the route, URL and it should give me a status code of two hundred.
1:32 Let's first look at the response actually. And here I put a break point in there to inspect what the response was I was getting. So I get a response
1:46 status code 200 and you can inspect it output with JSON. So that's the whole list of cars. Alright. And the length is a 1000. And the first object
2:07 is a dictionary, so I can make assertions, right? I can put the first one in car. And I can then say car id = 1
2:24 And that's how you do it in pytest with the assert statement. Back to my tests so I can assert that the length of the car
2:34 and let me actually put this in a variable. I can assert that the length of the cars equals a 1000. I can assert that the type of cars
2:50 is a list, couldn't have been one car. Make expected. And I can assert that car equals expected.
3:07 Ferfum. So this could be a test for the list cars function can even check for the order. So say, first id. I'll actually test the till here.
3:25 Alright they all passed because I have just placeholders. All passed, so these will just pass without having code in them, that's okay.
3:34 We will fill those out. So to test the order, I can do first id and last id. In first id, well the first id we have already here tested
3:55 so not necessary but the last id needs to be 1000. And it works as well. Let's do a POST and also show how an error would look.
4:14 First let's make a new car so, new car dictionary. Manufacturer is Mercedes. Year is 2018. vin is 123. And model is
4:41 well, let's make them another one of these. And then to make a post we can do a client POST to the route URL.
4:59 And we give it data. And the response should be a 201. And the length now of cars should have grown to a 1001.
5:38 Alright, I promised to show an error so let's make this thousand. Whoa, you see if you have an error.
5:46 That's one of the reasons I like pytest is the nice way it shows you the errors. As we saw in the previous lesson to really see if the
5:55 change persisted we need to make another get request to see if the new car is now under that id. That should say yes
6:12 to a hundred. And it should give me the new car. And let me copy this.
6:43 So this is what the car should look like. Returning it back from the API. And I'm asserting that. And that didn't work because JSON is a method
7:00 so I should run it as a method. And the year is actually an integer. And it works. Alright, I hope this gives you a feel how you go about
7:16 testing your API with pytest. Let me just show you the other methods I have written up preparing for this lesson. Let me first see if that works.
7:27 I use a car not found so I need to import it. Okay, that follows code so. Just quickly, I have a test for create car with missing fields.
7:40 So here we give data that's not having a manufacturer model and year so that's invalid. I make a POST request. And then I get a status code 400.
7:49 Remember I didn't really write 400 in my code. And again that's all what the framework does out of the box. So if it throws a validation error
7:58 make sure that it returns a status code of 400. And here I'm actually testing the error message that the API Star framework returns.
8:06 So in this case, the required fields. Then, I have another test where I actually do provide the valid fields but with invalid data.
8:15 So I have a manufacturer that's not in the list. That's written an error. I have a model that's longer than fifty characters.
8:21 And I have a year that's out of boundaries. We get a four hundred status code return and in the JSON we see the errors that API Star throws.
8:30 Then I do a simple get car. Might not be necessary actually, because I also do that after the create but it's always
8:39 I think it's good to have it as a stand alone test. Then I do a CAR_NOT_FOUND so I give an id that's not in the dictionary.
8:45 And you see as we saw in a previous lesson then you get a 404 and the message is the car not found. Then I do an update
8:53 like the post and provide a dictionary of new attributes and their values and I do put request. And again look at how the client gives all these
9:02 API methods out of the box. Really nice, saves a lot of code. And I test for a return code 200.
9:10 Then I test if the object that is returned from that call is the actual new car. And this what I didn't have in the initial version
9:18 but very importantly as I stressed it in the previous lesson as well you really want to then make a get request
9:24 to the API to see if the data actually persisted in you data store. So I'm making a new get request to car number 777.
9:34 And that should also return the expected car. Similarly as test get car not found I have that same test but using a PUT request.
9:47 So I put in an invalid id. And I get a 404 with the same error. And again I do the validation test as well but instead for a POST I do it for a PUT.
9:58 So I put in invalid data and I check the errors that API Star throws. And then I do a delete car I keep a note of what the length of the cars
10:09 is when we start. Delete three cars. When I delete them, they should return 204. Then I make a get request to see if they're still in the dictionary.
10:18 And they should return 404 because they have been deleted. Then I make an assertion of the length of the car's dictionary.
10:25 And that should then of course, be the initial count minus 3 because we deleted three items. So I think this is a pretty thorough testing plan.
10:34 And the great thing is that instead of spending five minutes in Postman every time to do this all manually, I can just run pytest and in a split second
10:44 I just validate my API. So I can go wild, make changes and just run pytest and see if everything is still green.
10:51 So I hope this gave you an idea how to test an API. I do have to say it's quite some work to get this all coded. I mean this script took me a while.
11:00 But once you have this coded you can just reuse this and it becomes pretty easy once you follow this structure.
11:07 So I really recommend to always write test before you code. And in the next video I will tell you what to do for day three and four.

Talk Python's Mastodon Michael Kennedy's Mastodon