Building data-driven web apps with Flask and SQLAlchemy Transcripts
Chapter: Testing web apps
Lecture: Pareto principle and testing with sitemaps

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Let's look at one more thing
0:01 that will catch a ton or errors for us and
0:03 it's something you want to have anyway.
0:06 That's a site map.
0:07 So if we just run our application
0:09 and pull it up over here
0:11 I've now added a site map.
0:13 As I mentioned at the opening, site maps
0:16 are here to talk to search engines.
0:18 Say here are all the pages I would
0:20 like you to search and discover on our site.
0:23 And some of those are static
0:24 like here's the home page, here's an account login
0:26 and account register and then there's
0:28 all the data driven stuff.
0:30 Here's the AMPQ project and apters and argparse
0:33 and all you know you would have all of them.
0:36 On the real you would have 140, 160
0:40 whatever the number is now
0:42 thousand packages listed here.
0:45 So this is something you may want to cache
0:46 somehow, right.
0:47 This could be kind of intense to generate this
0:50 but none the less if it exists and you want
0:52 it to exist so that you can use it for search engines
0:55 we can use it for testing. And the idea is that
0:57 if we just request, say, this
1:00 if that page comes up, there's a good
1:02 chance that everything worked.
1:04 If there's a problem, there's a good
1:06 chance that this is going to result in
1:08 a crash 500 error so on.
1:10 So by simply just going through and requesting
1:12 every URL on here, we're doing sort of a smoke test.
1:16 And it turns out a lot of times
1:17 web apps break hard, not soft right.
1:19 If somethings wrong its not a little bit wrong
1:22 it's BOOM you know.
1:23 None type does not have this attribute
1:25 type of crashes, things like that.
1:27 So were going to go and use that for our test.
1:29 Now let me just show you real quick.
1:30 I just added these in here so that
1:33 you don't have to go and generate them.
1:36 So here's a simple way to do that
1:37 we'll create a site map and remodel.
1:41 Its going to come in here and get a
1:43 just a call all packages.
1:44 I've passed the limit, in reality
1:46 you probably don't want to that
1:47 but just I don't want it to go crazy
1:49 if you have too much data.
1:50 So anyway, we're passing in this limit here.
1:53 I could probably make the updated text a little bit better.
1:57 There we go. So this is just going to be telling
1:59 part of site map has to talk about when it was last updated.
2:02 So um, we can figure out what
2:04 would a reasonable value be there. But
2:06 this is just going to be a fallback text
2:08 just for simple version, we're not actually
2:10 updating the data. So we'll get our packages
2:11 back and we're just going to feed it through
2:13 a template, over here, like so
2:17 let's go in and just interrate over
2:19 all the packages and generate them as well
2:21 as have the base static files.
2:24 All right. So with all of this in place
2:27 we can go write some tests against it.
2:29 Let me just show you that.
2:30 We're not going to write them from scratch
2:31 we'll just have a look.
2:32 Over here in our in our test we now have
2:34 site map tests. And what we can do
2:36 is we can come in, and say
2:39 let's just test this site map URL.
2:41 First we're going to get the text of the site map.
2:44 We're using the integration style here and
2:47 we're even letting this one hit the database.
2:48 Because we actually want the data to drive
2:51 what the packages are, for example.
2:54 And then we're going to use the XML features
2:56 in Python to do an x-path query to find
3:00 all the locations, this is going to be all the URL's.
3:03 Actually we want to request a relative path
3:05 so we're going to do a little replace on them here
3:08 and then we're going to get our URL's
3:11 and finally we're going to just loop over every URL
3:14 and just call client.get.
3:16 Now do we need, we have 140 thousand packages
3:19 do we need to call that function, that that view
3:21 140 thousand times? Probably not, if it's totally
3:24 broken it's probably going to crash for most of them.
3:26 So what we do is, we say we're actually going to
3:29 only request one project
3:32 and then we're going to bail out.
3:33 So if you've got like say a thousand categories
3:35 You might just only get one category.
3:37 You've got a thousand books if your a bookstore
3:40 only request one book. Things like that to kind of
3:42 speed it up. In reality, to get the site map text
3:45 is super easy we just do a get against the thing
3:48 you just saw. But we do a quick text replace
3:51 on the name space, I think Exemel names spaces
3:54 Suck and it makes the the x-path queries
3:57 up here harder so we can just drop that before
3:59 we get back the text, okay. And that's it.
4:02 I've already added this over to our _all_tests here
4:06 so when we run our _all_tests we should see
4:10 down here, that, that's running. Right there.
4:14 So see we tested /account/login, /account/register
4:18 and an AMPQ. That's because I told it to bail out
4:21 and not hit all the other packages.
4:24 Well that's it for testing.
4:25 You saw that we have three main categories of testing
4:27 in our web apps and of course there's others
4:29 as we also noted in the more prod unit test world.
4:34 But testing your web apps is important.
4:36 If your not going to do any testing
4:38 I encourage you to at least do the site map testing
4:40 it's going to actually catch a lot of the
4:42 errors that you would run into.