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