Unit testing a REST API

A free video tutorial from Jose Salvatierra
Founder of Teclado and Software Engineer
Rating: 4.6 out of 5Instructor rating
9 courses
298,725 students
Unit testing a REST API

Learn more from the full course

Automated Software Testing with Python

Learn about automated software testing with Python, BDD, Selenium WebDriver, and Postman, focusing on web applications

13:24:44 of on-demand video • Updated August 2023

Everything you need to know about automated software testing with Python (and how to enjoy testing, too!)
Avoid common pitfalls and implement best practices when writing automated tests
Write complete system tests using Python and tools like Postman
Automate your application testing by setting up a continuous integration pipeline using Travis CI
Browser-based acceptance testing using Behave and Selenium WebDriver
English [Auto]
Hi and welcome back. In this video, we're going to be writing our first set of unit tests. We're going to start by writing unit tests for our models just because those are going to be a bit simpler, a bit easier to write than the resource tests. So let's begin by going into our tests package and creating a new Python package there, which I'm going to call unit. And in there, we're going to create a new Python package, which we're going to call models. And in there, we're going to create a new Python file, which is going to be test item. Okay, so now we have here our test case file and we can begin by doing the usual importing test case and from models item import item model, we're going to be testing the item model in this test. So we'll have to import that, of course. Now you know how to begin creating the unit test. We have to create a class that inherits from test case and we have to create our test methods. I'd encourage you to give it a go, create the test methods that you think will need. And make sure that the test methods you create are unit tests. Okay. So hopefully you were okay doing that. Whenever I do this sort of ask a question, I'd recommend you pause the video and code and then come back to the video and I'll write the code here. And the way I'd go about it is. Something like this. Create our class and then create our our test methods. Now, what do we have in our item model that we have to test? Well, our item model, as you can see, has a few things here that are related to SQL alchemy. We're not going to be able to test them. We could potentially test that these attributes exist. And but we have to trust that Python works so we don't have to test that. These attributes are indeed part of the class. We have an init method. We may want to test that out. We have a Json method there that returns a dictionary and we may want to test that too. As unit tests. The rest of these. We cannot test using only unit tests because they depend on the database. So we'd have two options and one option we could mock the database and that would be not too trivial. So it would be a bit more complicated. And the other option is to not write these tests as unit tests and instead write them as integration tests. That would be the other option. So let's go back into the test and we're going to write two tests, one for the init method and one for the Json method. So we're going to have test create item and test item. Jason. Okay. Once again, if you've not given this a go, I'd recommend you pause the video now and give a go at how you might write these two unit tests. But I would go and create a sample item. And as you can see, the item has a name and a price that we have to pass in so we can pass in the name and the price. And then all we have to do is assert that these two properties are correct. So we can assert equal that the item name is equal to test. We can assert that the item price is equal to 1999. Now, this is some people would say that you don't have to test this because your init method is a very simple method, but it's important to have a test that creates a model and sort of makes sure that things are correct. Because if you then go and change your init method and for example you change one of the properties in there, one of the property names, for example, instead of price, you rename it to cost. Then you may see some errors elsewhere in your program. So it's always interesting to have this there to remind you of what things should be like. Now we could run this method. But there's also a third argument that I wanted to make you aware of in the assert methods of the test case, which is that you can pass in a third argument here, and that is the error message that will be raised if these two things are not equal. So if these two things are not equal, you're going to get an error message that's quite generic, but you can write your own custom error message at the end as a third argument if you want, and then you can get a bit more information or a bit nicer error message if this does fail. So, for example, name of the item. You could write something like that and you could, of course, have it on a new line there. Okay. And similarly. You could have something like that in here. And the price of the item after creation. There we go. So now if these two fail, you'll get a nice error message and we can verify that by just running this test. You can see that it passes. If we change one of those things, we should get an error message that says test. Is not equal to Tet, and it says the name of the item after creation does not equal the constructor argument. You can see here that test is not equal to test and that is the problem. Okay. Okay, let's go ahead and test the item Json. We've got our item, which is going to be an item model. And we've got our expected. Output for the thing we're going to test. And the expected output is a dictionary of name and price. And then all we have to do is assert equal for item Jason and expected. And then we can say the Jason export of the item is incorrect. Received that and expected that format item dot Jason expected. Perfect. You can potentially. They even have something like this there. Now this font is really big, so it's spanning. It's spanning a lot of my screen there and I have to scroll. But this is because this font is size 24 or something like that For recording. As you program yourself, you'll have a more reasonable font size and this will be totally fine. These error messages are not excessively long by any means. Okay, so once again, we can run this test method there and we should see that it passes and all that we've done is done a very common pattern when testing is we've created our testable things. That is the item model that we are testing and the expected output of our test. And then we just compare them and then we've given a nice error message there at the end. This is really common and you should generally make sure that each test is only testing one thing. In this case, our test has two assertions, but they're both testing whether the item was created correctly. And in here we don't have to test that the item model properties are set correctly because those are tested in this other method so we can jump straight through to testing the method that we do want to test. So that's everything for this video. We've created our two unit tests. Now, in the next video, we're going to create some integration tests. So I'll see you there.