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 • 148,473 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] Find Welcome Back in this video we're going to look at the post in any blog 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 that can have some meta data. It may even have a specific author. It may even have things like created 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 you or you may already have some experience with Python. If not this course may be slightly quick. If you are a complete beginner or 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 and 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 class and I will 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 are going to take in two arguments. Title and content and we're going to define two properties in our object so it's title ancestor 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. The first thing that you want to think about is does the thing we want to test that pinned 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 it post. This method may get more complex so it's good to start with a simple task 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 tests package and we're going to create a new Python package in there and we're going to call it unit and they don't want to be your unit tests. And here is where that question on the pendens 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. So whenever we create a test for something doesn't depend on something else there is a unit test. If we create a test for something that depends on something else that is a different type of test. OK now we're going to look at those different types soon. OK so let's go ahead and read 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 3 each test suite is a class. In this case is 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 the first test case or a 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 on our post. Let's begin by writing our first test. We're going to test if when we create 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 information has to start with the word test on the score. So we're going to do tests on the score and then whatever we want to call our test. In this case I'm going to call it test create post. OK. 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 it will tell us that we've got a failure. So the first thing we're going to do and I'm going to write some code is we 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 self object because we've inherited from test case. And when you say assert equal that test is equal to or title what this is doing is it saying test case make sure that text the string test. The post title that we've created here are the same and we're also going to equal that test content and the content are the same very simple test. All that is doing is he's creating an object and then it's making sure that the properties match. And you would 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 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 the pen 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 in it 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. 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 call projects in one room for example you have a folder that you've opened with PI Charman inside and you've got multiple project and your imports can be sort of messed up. It may not work quite as well so I'd always recommend you keeping each project as simple as possible to make it easy on Pinchem to determine what it's running and how things are working. With that said we are now able to run our tests and 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 the difference test X lost a character that the expected result this test and so on. OK. So we've written our first test. How does that feel. To me writing these tests feels like am like saving for 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've 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 we done here then you'd revert 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 tests 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 on and it helps you make decisions as to whether the things that you're doing are right or not. We can explore this concept much more throughout the course. We'll leave it here that 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.