Unit testing a REST API

Jose Salvatierra
A free video tutorial from Jose Salvatierra
Founder of Teclado and Software Engineer
4.6 instructor rating • 9 courses • 209,036 students

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 November 2019

  • Everything you need to know about automated software testing with Python (and how to enjoy testing, too!)
  • Common pitfalls and best practices when writing automated tests
  • How to 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 can call models, and then we're going to create a new python file, which is going to be a test item. OK, so now we have here our test case file, and we can begin by doing the usual important 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 unity 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 a test methods that you think we need. And make sure that the test methods you create are unit tests. OK, so hopefully you were OK doing that whenever I do this sort of thing to 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, great, our class and then create our test methods. Now what do we have in our Iten model that we have to test? Well, our model, as you can see, has a few things here that are related to sexual 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 these attributes are indeed part of the class. We have an innate method, we may want to test that out. We have a Jason method there that returns a dictionary and we may want to test that to. As a 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. 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 chasten method. So we're going to have test grade item and test item. Jason? OK. Once again, if you've not given this ago, I'd recommend you post the video now and give a go at how you might write these two unit tests. I would go and create a sample item, as we can see, the item has a name and a price that we have to pass in so we can pass in the name on the price. And then all we have to do is assert that these two properties are our correct. So we can assert equal that the item name is equal to test. We can assert that the item price is equal to nineteen ninety nine. 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 make 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 and 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, uh, in the cert 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 can, of course, and have it on a new line there. OK. 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 Tetteh, understands the name of the item after creation does not equal the contractor argument, but you can see here a test is not equal to test, and that is the problem. OK. OK, let's go ahead and test the item. Jason. We've got our item, which is going to be an item on. 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 we can see the juice and export of the item is incorrect, received that and expected that format item that Jason expects. Perfect. You can potentially. They even have something like this, they're. Now, this font is really big, so it's spanning, spanning a lot of my screen there and I have to scroll, but this is because this font size twenty four or something like that for recording, as you program yourself, you'll have a more reasonable font size and they will be totally fine. These error messages are not excessively long by any means. OK, 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, um, testable things. That is the ITER model that we are testing and the expected output of our tests. And then we just compare them. And then we've given a nice error message that 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.