Template Tests4:15 with Kenneth Love
We also need to make sure that our templates are getting the data they need and are generating the output we want.
assertTemplateUsed(response, 'template/name.html') checks that a given template is used somewhere in the response of the view.
assertContains(response, 'string') checks that a given string is somewhere in the text of a response.
Hopefully, you wrote those other view tests. 0:00 I know testing can be hard at first, but once you write a few, 0:02 it becomes a lot easier. 0:05 Tests generally follow the same patterns, too. 0:06 So once you've written tests for one app, writing them for 0:08 another app is a lot more obvious. 0:11 We've tested our models, we've tested our views, and our URLs. 0:12 Now let's test our templates. 0:16 There won't be a lot to test here, but 0:18 it's good to make sure that our templates work like we're expecting them to. 0:20 First though let's look at how I wrote those other view tests. 0:23 Which, there we go. 0:28 I think we can get them all on screen there. 0:30 So this is the one we wrote earlier, the course_list_view. 0:32 And then there's the course_detail_view. 0:35 And the step_detail. 0:38 I suppose I should've called that view, as well. 0:39 So, let's look at each of these. 0:43 The weirdest part in these is probably the kwargs. 0:45 And, maybe the assertEqual. 0:48 So, when we're trying to get courses detail. 0:50 We have to pass it some kwargs for the URL. 0:53 And, we have to say kwargs. 0:55 And then, we give it the pk and this. 0:58 There's another way of doing it. 1:00 Where you do like that and then in here is where you put the kwargs. 1:01 I find that less explicit and I don't like doing it that way. 1:08 I find it much easier to just do it this way and not have to worry about it. 1:12 So anyway, that's how we use reverse there. 1:16 And here, same idea but we have to do course_pk and step_pk. 1:18 And then, like I said, this is different. 1:22 We have assertIn and we have this courses object. 1:24 And we know course is gonna be a query set or a list, effectively. 1:28 On the two detail ones, we don't get that. 1:32 We get a single object. 1:34 So, we make sure that course is equal to whatever's in our context as course, 1:36 or that our step is equal to whatever is in our context as step. 1:41 So it's a little bit different. 1:45 We have to pay attention to what that object is. 1:46 And really, though, that's fine. 1:50 But now though, let's use these views to test our templates. 1:51 So we'll make sure that our templates are getting the right templates. 1:56 We're gonna use two new assertions here. 2:00 We're gonna use the assert contains, and 2:03 we're gonna use the assert template used assertions. 2:05 So first, let's update our course list view test, 2:09 to make sure that we're using the template that we want. 2:14 So self.assertTemplateUsed. 2:16 And we're gonna make sure that response, R-E-S-P, 2:21 used courses/course_list.html. 2:26 Now, the template used can be anywhere in the stack of templates, so 2:30 we could check to make sure that it used the layout template if we wanted, so 2:33 that we know it's like, hey, it's fetching this layout template. 2:37 It's just anywhere in that stack of templates. 2:41 This should be a fairly, somewhat obvious, assertion. 2:44 And let's also make sure that 2:48 the name of the first course is printed out somewhere on the page. 2:51 So we're going to do self.assertContains. 2:55 And we're gonna make sure that the response contains self.course.title. 2:57 Now, we don't want to provide any of the surrounding HTML, 3:04 because that might change. 3:08 We don't want to have to update our tests just because we changed some HTML, or 3:09 our design team changed some HTML. 3:13 or whatever. 3:14 So assertContains, make sure that the final rendered output of the template, 3:16 the text that comes back in the response, goes back to the browser or 3:21 the client, has whatever string we've passed into it. 3:23 And in this case, we're making sure that the name of our course, 3:27 the title of our course, is somewhere on the page. 3:29 Now, you have to be a little careful with this one to make sure 3:32 that whatever you're looking for 3:36 isn't something that's going to pop up in the standard, like, HTML or something. 3:38 Like looking for each one, or doc type, 3:43 is often really tricky because, you know where those are going to show up, right. 3:47 Okay, so let's look and see if all of our tests pass. 3:52 Cool. 3:59 All five tests passed. 3:59 That's great. 4:00 That means that our project is fairly well tested. 4:01 Great work. 4:05 At this point, you should feel pretty good about adding similar template assertions 4:06 to both of the other view tests. 4:09 You go ahead and do that, and I'll be right back with one last surprise. 4:11
You need to sign up for Treehouse in order to download course files.Sign up