#100DaysOfWeb in Python Transcripts
Chapter: Days 81-84: Unit testing web apps
Lecture: Writing the functional tests

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Now let's test our whole application end to end. More of a functional test. So over here, we've got all of our tests
0:08 and we've got our view tests and view model tests for just the default section. Let's go add some tests over here.
0:15 We'll call this site test or something like that. Now, instead of starting from scratch remember we do have this test thing that was generated
0:23 by the template, by the cookie cutter when we created it. And we have this little bit right here. Let's take that and put it into our site test.
0:31 We should really get rid of this we don't really need it anymore. So what's going on? Well, we need to import unittest obviously.
0:37 We also need to have web tests, not just imported but actually installed. Notice over here, in our requirements
0:44 we only have the run time production things. We don't have an additional development or testing set of requirements. Let's change that.
0:55 Add a new text file, called requirements-dev.txt. Here's a cool trick. Say we're going to use the requirements. .txt file, in that mode
1:06 and then we're going to have webtest. And that's it. Now, if we pip install -r requirements-dev.txt It's everything else except for our new web tests.
1:17 Okay great, so that error went away. Although it says, we have this issue going on here. Let's just call this app. Cool, so we've got this all set up.
1:25 We're going to basically call the main method. Remember what this does, it's been a while. This is the entire start up for the web app.
1:34 We're going to create it, and start it up configure the database, anything that happens there. Then we're going to wrap it in a TestApp
1:41 instead of a WSGI app. That will let us do things like app.get, to this URL. Now, notice it's looking for Pyramid in the body.
1:50 This is, not going to work so well. So let's call this, first of all, site tests and test roots, that's probably okay. Let's go to our tests, all tests
2:01 and go over here and just do one more import. Import all of those. Once again, if we've run all of our tests let's see what happens.
2:08 Well, everything's good except for that one not so much. It says false is not true. Hmm, it's being sort of philosophical isn't it
2:16 false is not true, that's so amazing. Well, what it's really saying, is if we go back to the right place is that Pyramid is not in the body.
2:25 Well what is, let's look for some more interesting things that we might to look for in homepage.
2:31 So we come over here, and we can look, and it says okay we have this Dannie Easom, and we have unpaid bills. Let's look for those two things.
2:40 So we'll have this one, and we'll have unpaid bills. Let me just check on the capitalization. Yes, this all looks good.
2:47 Now our tests, that is not our test our tests should all work, perfect. You see a little red here
2:53 but that's just a warning, actually if you look inside inside of webtest, I don't think we care about that.
2:59 But it looks like everything we're doing, is working. So the last thing to do to sort of summarize this before we get to it.
3:06 This lets you go and issue requests which goes through the routing through the entire infrastructure.
3:12 And this actually, right here, is hitting the database. So this is more of a functional test. Now, one other thing that we might do
3:18 is we had a site map, that listed out all the data driven URLs then we could just request every single page on there. Why would we do that?
3:26 Well it would be a great way to just make sure at least the pages don't crash. You'll often find, if something's wrong with the page it's really wrong.
3:33 So for example, we could go to here to site map.xml. As you can see, there's a ton of stuff here. We could just go through and you know
3:41 in this example, hit each one of these. We don't have a site map. I'll give you a quick example of that later.
3:47 But, lets just sort of simulate something to that effect. Let's suppose, you're going to test the site map. And over here, we might have
3:55 instead of using the site map we'll just come up with a list of URLs. Okay, so we'll have / and let's see, what else do we have...
4:03 We have the details for that bill and let's go details for that bill. So we just had each one of these you could say for url in URLs.
4:16 We'll go over here, and just make sure we get a 200 back. And we actually don't care about the response. We could check it out if we want.
4:21 But in this case, we don't. If we run this one more time notice that we hit a bunch of stuff. Alright, we're already loading the data
4:27 so this is just the app startup speaking here. But it looks like that worked. You could put as many URLs in here as you want.
4:34 If you have a site map, you can just take the site map and verify the entire site is hanging together. Let me just drop a quick example in here.
4:42 So in another course, we actually did this I call this other, and this over here we actually go thorough and talk about.
4:48 So I'm just going to leave you with this link. That actually shows how to get the site map how to generate the site map, how to go through
4:55 it in an efficient way, all sorts of things like this. If this is helpful for you, go ahead and grab it and run with it.
5:02 Maybe this is enough to give you an idea. So here's how we can do functional tests end to end tests on our web application.
5:09 That's it, we've tested all three levels. We've tested the view models. Then the views, then the functional tests. That's most inner to most outer
5:17 in terms of how things fit together.


Talk Python's Mastodon Michael Kennedy's Mastodon