Django has a great testing framework that tells you more about your tests. In this video, learn how to use it.
- [Instructor] Now when running our test, it's not always great that we can run them and say, oh, well nothing showed up, that must have meant everything is working. It'd be nice to get some sort of feedback. And for great feedback, Jango has an awesome testing framework, and for us to tap into it, let's go ahead and create an app inside of our Jango project. So let's go ahead and do an ls. See where we're at. Looks like we need to move into our project. Hash that. So I'm going to do a cd into hashthat. Once we're inside of that directory, we're going to say jango-admin and we want to do startapp, and the app that we want to call this is hashing. We'll go ahead and hit Enter on this. Now if we do an ls, we should see that we have a new directory here called hashing. That's great. And inside of that directory of hashing, we have a place that we can test. So let's go ahead and move back over to our code editor here. We can see that we have this hashthat folder. Inside of there, there's this new directory hashing, and we move down, there's a file called tests.py, and this is where we're going to move our testing code to. So let's go ahead and go back to our func_tests. Let's copy all the text that we have there, and paste it now inside of our test folder, and let's recreate our test, but using Jango's testing framework. So it's important to keep this top line here that we import the test case from Jango. We're going to move selenium just right next to it there. And then we're going to create a class. Now ultimately we're going to be creating two classes. One for functional tests, one for unit tests. Let's start first with the functional test. Just type out class FunctionalTest, and we'll go ahead and do parentheses around this, and it's going to come from TestCase. There we go, and then we'll do a colon there to finish that. Now it's important, and let's add, for naming sake here, FunctionalTestCase so that we know it's a test case. There's nothing special about the name FunctionalTestCase. This could have been NickTestCase. It's totally fine. It didn't even have to end in TestCase, but to me, this is a very descriptive way of saying what's going on inside this class. It's our functional test cases. So we've got that class. Inside of our class we need to have two special functions. And they're called setup and tear down. They're basically called before all the tests are run and then after the tests are run. So for our setup, we want to get the browser ready to rock and roll. So let's make a new function here. We're going to call it setUp. Inside of there we need to pass the parameter self, and we'll do a colon. And like I said, what we want to do here is get our browser ready to rock and roll. So this lien of code where we say browser is equal to webdriver.Firefox. Let's go ahead and cut that and paste that inside of our function. Like I said, there's another special function, and that is tear down. That's what happens after we've run all the tests, and it'd be nice, after we ran a test, if we could go ahead and close the browser so we don't have to manually go close that ourself. So let's do def tearDown. Again, pass self inside of there. Let's do the colon. And the code that we want to write here is self.browser, and we're going to say .quit on that. So that'll automatically shut down and close the browser. Now with this, I'm just realizing here in our setup, we got to make sure that we have a self.there so that this browser is a property here on the class so that we can change that and edit it throughout the tests. So with this in place, we have our setup function, the tear down function. In between these two, this is where we need to make the functions to say, okay we want to test this or that. So let's go ahead and create our first test. We're going to say def and we're going to call this test_there_is_homepage. Again, feel free to call this whatever you'd like, but I feel like this is an appropriate name for this. Really describes what's going on. So inside of this test, we simply just want to do what we have already written. So I'll cut these two lines of code. Paste these in. So we're going to visit the following URL and assert that we should be able to find the word install there. So with this in place, let's go ahead and test out these new functions now. Before we do, the way that Selenium tests things, and the way that Jango tests them is a little bit different. So we don't want to use just the regular old assert here. We want to move to a different version of that. So we're going to say self.assertIn, and then with parentheses here, we're going to be looking for the following word install. Right, just like we had before. So we're going to say look for the word install in. So we'll do a ,self.browser.page_source. So let's go ahead and save this. Now, let's go ahead and move back to our terminal. And this is how you test things using Jango. This is going to be the big command that we're going to be using over and over again. We're just going to run python manage.py test, and what this does, is it goes through our whole projects, finds any apps that have testing files, and runs those tests. So we'll go ahead and hit Enter on this. Okay you can see it's going to fire up Firefox, and look at that, it closed this for us. But here, we have a little bit more information about what's going on here. So it's saying hey, we tried to run this, we could not find something called browser. Oh I'm sure we forgot to say self.browser somewhere. Oh here we go, in the test_there_is_homepage we forgot to say self.browser. Okay, so with that in place, this is why tests are so great. If you mistype something, you can find the fix for that. Let's go ahead and move back. Run that test one more time, and Firefox open, closes, and look, even though we passed the test, it gave us the information. It says we ran one test and everything was okay. It took four seconds to run that awesome information. Okay, well give yourself a nice pat on the back. You passed your first functional test, and you learned how to use Jango's testing framework.
- TDD philosophy
- Comparing functional and unit tests
- Writing a test in Selenium
- Writing a unit test
- Testing page data
- Hash testing
- Writing a test for user input
- Testing a POST request
- AJAX implementation
- TDD vs. testing after