#100DaysOfWeb in Python Transcripts
Chapter: Days 81-84: Unit testing web apps
Lecture: Your turn: Testing a web app
0:00 It's your turn to try your hand at testing some web applications. Here we are in our GitHub repository in 081-084-unit-testing/your-turn.
0:12 There's four days of things to work on for you of course, as usual. The first one, just watch the videos. We spent a fair amount of time on that.
0:19 Check out the videos. Make sure you understand it all really, really well. On the second day, we're going to work our way from
0:25 the inside to the outside of what you might think of as the onion that makes up our web application. The first thing that we're going to look at is
0:32 testing the view models. This has a couple of challenges. First we have to get our web app setup. If you'd like to use the bill tracker and test it
0:41 it already has view models. It's good to go. Go ahead and test that. If you'd rather create view models for
0:46 one of your other web apps and try your hand at that and then test those, feel free. You can do whichever you like
0:53 but we're giving you the bill tracker to work from. Here's some examples on what you can do to test. Remember, when we're testing our view models
1:00 often that view model wants to talk to the database. In this example here, what we're doing is we're saying we want to mock out
1:07 the repository.get_user_by_id and tell it to return this fake user. Remember, if we didn't do that we got the error None type is not callable
1:15 when I was trying to execute the factory to create a new session in SQLAlchemy. We don't want to fix it by making SQLAlchemy
1:21 and the database active, we want to fix it by not talking to the database. That's a good practice in general when you're doing low level unit testing.
1:29 Day three, we're going to move from view models to views. Here we're going to test the views. The example we give below, we're doing a test
1:37 against just the index, the home. You might wonder, "Why are we testing this? We already tested the view models. It kind of does everything."
1:44 But there are some things left to test. For example, you might want to check that if you pass a particular user and a bill
1:52 and the bill does not belong to the user that that turns out to be a 404. We verify that in our view model tests but that doesn't mean that it's
2:00 dealt with properly in the view. What it should be is a 404 it might be a 500 when the thing crashes when it tries to render something on
2:08 the bills in the template. You can verify that. We have not verified when we post a payment that that actually gets done correctly
2:15 so you can check that out as well. These are a couple of things that you can test if you'd like. And then on the last day, we're going to get to
2:23 the outside of the onion and test the entire web app, including interaction with the database as it's going to be.
2:29 What we're going to do is we're going to use webtest and we're going to wrap up our application. Then we're going to call certain URLs.
2:38 Here we're calling just the root of the website. We're expecting a status code 200 and we're asserting a couple of things.
2:44 We'll leave it up to you to decide what URLs to test and what to look for but write a couple of these functional tests.
2:52 After you've gone through all of these you've done most of the type of testing that you're going to do in a web application
3:05 but you're going to go a long, long ways with what you've learned here, and maybe this is enough. When you're done, of course
3:10 share what you've done and let the world know you're still on track with your 100 days.