Writing our first test

Jose Salvatierra
A free video tutorial from Jose Salvatierra
Founder of Teclado and Software Engineer
4.6 instructor rating • 9 courses • 208,669 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] Welcome back. In this video, we're going to look at the post. In any blog, a post has two basic attributes, a title and some content now, depending on which platform you go to, the post can also have a lot more attributes. For example, it can have some metadata. It may even have a specific author. It may even have things like credit dates, updated dates. It can have images like a hero image and so on for this app that is going to be a console based application. Very simple. Just to get you started testing Python apps. We're going to stick to its most basic representation. So our post is going to be a class that contains two properties, title and content. And a blog is going to contain multiple posts and the blog is going to have the author, and so each post is not going to have its own author, only title and content. Now, are you may already have some experience with Python, if not, this course may be slightly quick if you are a complete beginner, in which case I apologize in advance. But if you do have some experience with Python, it should be possible for you to create a post class that has two properties, title and content. I'd recommend you pause the video and go ahead and create this class if you can. Hopefully, you managed and if you did, this is how I would go about creating the post that would do class post and then it would define an init method and the init method is the constructor. This is what gets called when you initialize a new object of class post. And in this method, we're going to take in two arguments, title and content, and we're going to define two properties in our object self, not title and self content. And that is what this class may look like now. We know that this is likely correct. But we want to write some tests for it. There are a couple of things that we have to think about before we start writing any test, and the first thing that you want to think about is. Does the thing we want to test depend only on itself or does it have any external dependencies? Does it depend on any other files or any other systems like a database or a webpage or anything like that? This is the first question you have to ask yourself. Even for the simplest of things. And naturally, here we can see that this depends on nothing. This is, um, just a class that gets instantiated by something and then it ends up with two properties, title and content. This is a very simple method, and you may be tempted not to test it. But. As you develop this blogging application and you add more things to a post, this method may get more complex. So it's good to start with a simple test that you can then expand as your code grows. So what we're going to do is we're going to go over to the tests folder that has tests package and we're going to create a new Python package in there and we're going to call it unit. And these are going to be a unit tests. And here is where that question on dependencies comes from. For any methods or functions or classes you want to test that don't depend on anything else. You're going to put them in the unit tests. And so whenever we create a test for something that doesn't depend on something else. That is a unit test if we create a test for something that depends on something else, that is a different type of test. OK, and we're going to look at those different types assoon. OK, so let's go ahead and create our post-test class. In Python, particularly in the unit test framework that we're going to be looking at in this course and the unit test framework comes with Python three. Each test suite is a class in this case is called post-test. And it always has to inherit from a particular other class, which is a test case and test case is part of the unit test library. So we're going to do from unit test import test case. Now that we have it, what we've created here is a first test case or set of tests. Now we are going to test our post, so presumably we're also going to have to import that. So let's do it. So we've got our test case and our post, let's begin by writing our first test, we're going to test if when we create a post object, the correct properties get set on it. Every test that you create with unit test unended, with most testing libraries in Python is going to be a function. And that function has to start with the word test on the score, so we're going to do a test on the score and then whatever we want to call our test. In this case, I'm going to test create post, OK? And in here, if the test succeeds, meaning no errors are raised. Then the test would pass. Otherwise, the test will fail and the units library, when we run this test, will complain and will tell us that we've got a failure. OK, so the first thing we're going to do and I'm going to write some code is we'll create a new post. And this post is going to be a post object that has a title test and a content of test content. And then we're going to use the test case API, which remember is the stealth object, because we've inherited from test case, I'm going to say a equal. That test is equal to the title. So what this is doing is it saying test case, make sure that test the strict test and the post title that we've created here are the same. OK. And we're also going to assert equal that test content. And the content are the same. Very simple test, all this is doing is is creating an object, and that is making sure that the properties match. And you may think, well, this is idiotic, I write and it tests all the time, and again, this is so that this test will fail when you change the unit method, when you change the unit method, the test will fail and you'll be reminded that you have to check other parts of your system as well to make sure that nothing has broken. Similarly, if any other tests depend on the post and you are testing them, as soon as your post changes, those tests will also fail and you'll be made aware of that. So let's run this test in some instances, you can just right click on the post test and run the unit test. This depends on your project structure. If you follow the project structure, exactly what I've done here, you have a project folder and in it you have some tests and then you've got unit tests and then post-test. So it's a very simple structure here. Then you should be able to just. Right. Click on the post-test class and run the unit tests. As you can see, the test has passed and on that note, if you have a complex project where you have, for example, multiple what I'd called projects in one, um, for example, you have a folder that you've opened with Pichon, and inside it, you've got multiple projects and your imports can be sort of messed up. It may not work quite as well. So I'd always recommend you keeping each patient project as simple as possible to make it easy on Pichon to determine what it's running and how things are working. With that said, we are now able to run our tests and I'll show you what happens if we make the test fail. This is what happens. We see a test failure and down here it tells us that there is an assertion ever. Because test X is not equal to test. And then it gives us here a bit of the difference test X is lost a character, the expected result, this test and so on. OK. So we've written our first test. How does that feel? To me, running these tests feels like like saving for saving money for retirement, you know, this is security. It is making sure that as your system changes, you have a little something reminding you of the things that you've done in the past. And also the tests are the source of truth of how your system should work. What this test is saying is when you create a post. The title and the content should be equal to what you defined up top. If you have somebody else in your team that comes along and maybe makes a mistake and says, hey, you know, I think the title of the post should always be. And Ralph. And then you run your tests, it's going to fail, even though this test is seems completely correct, it's going to fail because the implementation of the thing that you're testing is wrong has a mistake. So then presumably you would catch this at some point and you would say, hey, what have you done here? Then you'd reverse it and then everything would be OK. So that's the purpose of tests. It is primarily to catch any mistakes that arise after the fact. And Texas can also be helpful to sort of design the system in a way that is simple, if writing a test is very hard and that may mean that the thing you're testing is too complex. So it also gives you a bit of insight and helps you make decisions as to whether the things that you're doing are right or not. We're going to explore this concept much more throughout the course. So I'll leave it here. I think you guys are getting bored, but this is just a bit of insight into why we test and we've written our first test, so let's continue and write some more. I'll see you in the next video.