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