Building Data-Driven Web Apps with Pyramid and SQLAlchemy Transcripts
Chapter: Testing web apps
Lecture: Sitemaps testing in action
0:00 Let me show you the sitemap I've added to our website here. Again, I don't want to write it from scratch cause the details aren't that valuable.
0:08 It's just repetition of what we've been doing. But let me show you what's happening here. So we actually added this UtilsController
0:14 and we have two things, robot.txt which tells search engines how to search the site. And notice we're changing the content type
0:21 to plain text as it should be. And then this one, we're saying the content type is xml and we're going to generate it from the sitemap.xml.
0:30 We saw that before but let's have a quick look. Robots, by the way, super easy. You explicitly disallow stuff
0:37 with not disallowed is allowed in so, search everything. And then here, we're going to say generate the static URL's that we know about.
0:46 And then we're going to dynamically go through this. Your site might have categories and books, and comments, and whatever. Put em all in here.
0:55 Whatever you want search engines to find you put them in here. So let's go see that real quick. So we can go to robots.txt, and there's that.
1:02 And we can go to sitemap.xml. And here you can see these are the static URL's and then these are all of our packages:
1:11 Awscli, Logs, Babel, BeautifulSoup, et cetera, et cetera. All right, so this is in place. Once this is in place then we can use it for testing, right?
1:23 So let's go down to our tests and here's the site map test. We're going to create one of these functional tests
1:29 that goes and grabs an app that's already built. It's a little bit confused there isn't it? There we go. So it's going to use just one app
1:38 for all the tests it does here. And just like we saw before it's going to call main create a test app, and give it back.
1:45 Want to do that on setup and that's great but then what we do is get the site map by issuing a get there. And then I'm going to drop the namespace
1:54 so we don't have to do it in our queries and just return the text. And then our tests become, get the text
1:59 load it up as xml, do this x path query on it and then we'll just print out a little info. So let's run this.
2:07 I've also added it to the alltests so it'll appear. And here you go you can see we're testing potentially 99 URL's.
2:15 Got the local host, account login, account register project /, you know, here's the first one that we hit. We took away that part of the break out
2:24 for testing the packages we'd hit em all. So it's up to you how you want to do that but this can be really helpful.
2:30 It's like I said, often when these pages fail or these methods fail, they fail hard and you get some 500 server error
2:37 and this would definitely catch it cause we're checking there all 200 a-okay. One thing to be aware of
2:43 when you go and request basically every URL on your system we'll go into the database at this point and doing the other things they might do
2:51 so you might want to be careful here and mock that stuff out as we saw but it's going to be quite involved
2:58 because you're basically mocking out everything. Can totally do it and it may be worthwhile or it may not be.
3:03 You'll have to try and see whether it's worth it. So this definitely counts as some kind of integration test but it's really easy to do.
3:11 You can see the whole implementation is 57 lines long and you have it here in code so you can just copy and adapt.
3:18 Really nice. All right, so that wraps up testing. We've seen the different levels, and the different ways of wrapping the web framework infrastructure
3:26 around our tests, providing it to our tests as well as replacing some of the foundational items like data access.