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 • 147,828 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] Harm will come 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 and a bit easier to write than the resource tests. So let's begin by going into our test 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 with which we can tackle models and in the end we're going to get 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 importing test case from models. Item import item all 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 a test message 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 I 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 a class and then create our our test methods. Now what do we have in our item model that we have to test. Well our model is you can see has a few things here that are related to sexual alchemy. We're not going to be able to test them then we could potentially test that these attributes exist 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 the unit tests. The rest of these. We cannot test using only unit tests because they depend on the database so we would have two options and one option we could mark the database now 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 unit's method and one for the Jason method. So we're going to have test item 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 and the price and then all we have to do is assert that these two properties are 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 make sure that things are correct because if you then go and change your in its 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 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 is 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 you know if the item you could write something like that. And you could of course have it on a new line there. And similarly you could have something like that in here. And the price of the item creation. There we go. So not all of these to fail you'll get a nice error message and we can verify that by just running this task. 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 10. And he says the name of the item after creation doesn't equal the constructor argument. You can see here a test is not equal to test and that is the problem OK let's go ahead and test the item. Jason we've got an 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 that Jason X part of the item is incorrect received that and expected that format item that Jason expected perfect. And you can potentially even have something like this. Now this font is really big. So it's spanning is finding a lot of my screen there and I have to scroll. But this is because this Fondas size 24 or something like that for recording as you program yourself you have a more reasonable font size and this would 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 testable things. That is the item model that we're 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 or two unit tests. Now in the next video we're going to create some integration test so I'll see you there.