Writing our first test

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
Writing our first test

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 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, a 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. So each post is not going to have its own author only title and content. Now you're 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 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 class. I would do class post and then I 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 init method we're going to take in two arguments, title and content and we're going to define two properties in our object self, dot title and self dot 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'll 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 web API 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 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, the 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 our 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. Okay. And we're going to look at those different types soon. Okay, so let's go ahead and create our post test class. In Python a 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, it's called post test. And it always has to inherit from a particular other class, which is 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 our 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 and indeed with most testing libraries in Python is going to be a function. And that function has to start with the word test underscore. So we're going to do test underscore and then whatever we want to call our test. In this case, I'm going to call it test create post. Okay. And in here, if the test succeeds, meaning no errors are raised. Then the test will pass. Otherwise the test will fail. And the unit test library when we run this test will complain and will tell us that we've got a failure. Okay. So the first thing we're going to do and I'm going to write some code is we're going to create a new post. And this post is going to be a post object that has a title text and a content of test content. And then we're going to use the test case API, which remember is the object because we've inherited from test case and we're going to say assert equal. That test is equal to p dot title. So what this is doing is it's saying test case. Make sure that test, the string test and the post title that we've created here are the same. Okay. And we're also going to assert equal that test content. And p dot content are the same. Very simple test. All this is doing is is creating an object and then it's making sure that the properties match. And you may think, well, this is idiotic. I write unit tests all the time. And again, this is so that this test will fail when you change the init method. When you change the init 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're testing them as soon as you 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 you have some tests and then you've got unit test 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. And 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 call projects in one, and for example, you have a folder that you've opened with PyCharm, 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 PyCharm project as simple as possible to make it easy on PyCharm 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 just 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 error. 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 is test and so on. Okay. So we've written our first test. How does that feel? To me, writing these tests feels like I'm 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 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. It has a mistake. So then presumably you would catch this at some point and you would say, Hey, what have you done here? And you'd revert it and then everything would be okay. So that's the purpose of tests. It is. Primarily to catch any mistakes that arise after the fact. And texts 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 it 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.