View Tests6:26 with Kenneth Love
We get to our model data through our views so they're equally important to test. Bonus, we can test our URLs at the same time.
django.core.urlresolvers.reverse() takes a URL name and reverses it to the correct URL string. More information
self.client acts like a web browser and lets you make requests to URLs, both inside and outside of your Django project.
assertEqual(a, b) checks that
b are equal to each other.
assertIn(a, b) checks that
a is contained in
I hope that you took the time to write a test for the step model. 0:00 If you did, you probably found that you had to create a course too. 0:03 This is a good time to use the setup method in your test cases. 0:06 If you're not sure what I mean, check out the attached workspace, or 0:09 go watch the Python testing course. 0:12 Or just hang on, cuz we're gonna need it for 0:14 the test we're going to write in this video. 0:16 We've tested our models, so now let's write a test for our CourseListView. 0:18 One nice thing about writing the view test, like I'm about to show you. 0:22 Is that you don't have to write the test for the URL, and a test for the view. 0:25 You can test both at once. 0:29 Some people will say that this is messy. 0:30 And your tests should only test a single thing. 0:32 This is true, but URLs and views are so tightly bound in Django, 0:34 that it's more trouble than I think it's worth to separate them at test time. 0:38 So testing views, is not the same as testing models. 0:42 So, we need to create another test class, of course. 0:45 So, class course views tests. 0:50 This will also extend the tests case and we're gonna use a setUp method, 0:54 so that we can create a couple of things before we start actually writing tests. 1:00 So we'll say self.course equals Course.objects.create. 1:06 And the title will be Python Testing. 1:13 And the description will be Learn to write tests in Python. 1:17 Then we'll do self.course2, Course.objects.create. 1:23 Title will be New Course. 1:30 Description will be Misspelled a new course, exciting. 1:34 And then we'll say step is step.objects.create. 1:42 Title is introduction to doc tests, 1:50 description is learn to write tests in your doc strings and 1:55 the course will be self.course. 2:03 All right, so the setup method, if you haven't taken Python testing is run at 2:08 the beginning of each test and it just creates some things for us. 2:13 So, let's write our test. 2:24 So we're gonna test the course list view, 2:27 and make sure that it shows both of these courses. 2:30 Now to do that, we're going to have to import the reverse function. 2:33 So, let's come up here, and it's from Django.cor.urlresolvers, import reverse. 2:38 And reverse let's us reverse a URL name. 2:45 So we get the name of the URL, we want to go hey, I've got this name, 2:47 what's the actual route that this goes to and the view ultimately that goes to. 2:52 So, it's able to do that for us. 2:58 So, test_course_list_view and it's going to take self. 3:01 And if you're wondering that import for reverse, I do have it memorized but 3:06 it has taken me years to get it memorized. 3:10 Okay so, when we're testing views, 3:13 we get a pretty cool thing, which is called self.client. 3:16 Self.client is kind of like a web browser, it lets you make web requests to a URL and 3:19 then it gives us back the status code and the HTML that come from that URL. 3:25 If it goes to a view in our Django app, we get back some other useful info, too. 3:31 So let's go ahead and do that. 3:35 We're gonna say resp. 3:36 R-E-S-P, there's no E-C-T on this one. 3:37 Just for response, it's a pretty common abbreviation that I use. 3:42 Client.get. 3:45 And then we're going to reverse courses:list. 3:47 So we're gonna reverse our namespaced list URL. 3:53 To start off with, let's just make sure this comes back as a 200. 3:57 So we'll do self.assertEqual. 4:01 The resp.status_code is equal to 200. 4:06 And I wanna make sure that both of our courses are in the view. 4:13 And this is one of the handy things that we get to do with a Django view. 4:17 Our resp object has an attribute named context. 4:21 And this is the dictionary. 4:26 Of all the values that we passed into our template when it gets rendered. 4:28 Remember doing that in our views.pi. 4:32 Let's look at our views.pi real quick just to see it. 4:34 So here's our course list this is what we're gonna hit. 4:38 And right here is this dictionary and we have a thing called courses in it. 4:40 That's what we're gonna test. 4:44 So we can check to see if something is in there So, 4:46 we're gonna do self.assertIn, and we're gonna assert 4:51 that self.course is in resp.context['courses']. 4:56 And then we're going to assertIn self.course2, resp.context. 5:05 Courses. 5:13 So both of those things should be in there. 5:15 We're checking the views context to make sure that it renders the template that 5:18 both of those things are there. 5:22 And we're also making sure that we get back a successful status code. 5:24 So let's run that test. 5:27 So python manage.pi test. 5:30 Three tests pass. 5:34 So there's one here. 5:35 There's the one for testing the step creation that I wrote in between videos. 5:38 I hope you wrote it too. 5:42 And the course creation one that we wrote last time. 5:44 So that's great. 5:46 All of our tests pass. 5:47 Remember way back at the beginning of this episode, I said we'd get URL testing for 5:49 free with this method? 5:53 If you didn't see where that comes into play, 5:54 it's when we use the reverse function to get the URL. 5:56 Since we go through the URL to get to the view, if the URL ever stops working, 5:59 our test will fail. 6:03 All right. 6:04 One more testing topic to cover. 6:05 Before we move on to that though, you should definitely go back and 6:07 write tests for the other two views in our app. 6:09 Make sure that only a single course shows up on a course detail page. 6:12 And make sure that only a single step shows up in the step detail view. 6:16 These will require passing keyword arguments to reverse for the URL though. 6:19 So they're a little bit trickier. 6:23 I'll link to the docs in the teacher's notes. 6:24
You need to sign up for Treehouse in order to download course files.Sign up