HTML Reports4:23 with Kenneth Love
`coverage.py`'s reports are great but sometimes you want something more visual or a page you can share with other people. Let's see how to generate HTML reports from `coverage.py`.
coverage html will generate the HTML report. By default, it'll live in the
To serve HTML files (and CSS, JS, etc) directly from Python, we used the
http.server module through
python -m http.server.
So coverage.py is pretty awesome, huh? 0:00 Well let's look at one last feature of this little library to see 0:02 a way that it's even more amazing. 0:05 Coverage's reports are pretty great. 0:07 But sometimes I like a visual reference to where I'm covered and where I'm not. 0:10 So actually, instead of generating a report just in the terminal, you know, 0:14 just this coverage report that we've been doing, we can actually do coverage html. 0:18 And if I refresh over here, just so you can see what's here. 0:27 We now have this html cov, folder. 0:35 So, because of one of the limitations of workspaces with how it pays 0:39 attention to Python and non-Python, stuff like that, 0:43 we have to do something a little bit different here. 0:46 So, what we're actually gonna do, we're gonna do Python -m http.server and 0:48 this serves up a very basic HTTP server. 0:53 And then we're gonna go to our preview. 0:57 And we have 8,000 here so we want 8,000 here. 1:00 And then we're gonna go into htmlcov. 1:01 Look at this, this is a nice, pretty view. 1:04 We can see that my dice thing has 50 statements. 1:09 Not any of them are missing. 1:12 We're at 82%. 1:13 Overall we're at 89%. 1:14 But here's what's really cool, here's what's awesome. 1:16 I can click here where it says dice. 1:18 And now I'm looking at my dice file, and I can see the lines that are not covered, 1:22 because they're red and highlighted, which I'm sure you guessed that. 1:26 And the lines that are covered because they're red. 1:29 Now if I had told coverage.py to skip certain lines. 1:32 Then they'd be, they would appear in yellow. 1:36 I'll leave that for you to find inside the docs 1:38 which of course I will link to in the teacher's notes. 1:41 So you can see that we have a few different things that aren't tested. 1:43 So for one thing is we don't have this dunder add method being tested or 1:48 this r add for reverse add, or contains or get, 1:51 we have a lot of things that aren't really tested. 1:55 So, let's write a test that does this. 1:57 So go back over here, to our test.py. 2:01 And down here at the bottom of our roll tests, 2:03 lets add a new test and we'll say def test_adding. 2:11 And we're gonna say self.assertEqual, 2:18 self.hand1 plus self.hand3. 2:23 And that should be equal to the sum of 2:28 self.hand1.results plus the sum of self.hand3.results. 2:32 All right, kind of a long test but yeah, it works. 2:41 So then we're gonna come down here and we're gotta kill this. 2:48 And then we have to run our tests again. 2:51 So coverage run tests.py. 2:53 And then we have to generate the HTML again, coverage html. 2:59 And then we have to serve our stuff again, 'kay? 3:03 But then we can come and we can refresh this and 3:07 we see that add and radd are now tested. 3:12 Now, we only meant to test add, why did radd get tested? 3:15 Well it has a lot to do with these internals here. 3:18 When we call this int(self) plus other, it comes up here to end. 3:21 Calls this, that's fine, that's how we get the int self but 3:25 when it gets to this plus other, it doesn't know what it's doing. 3:28 It doesn't know how to add a number to a roll. 3:30 So it calls a reverse add which says, hey, 3:34 see if you can figure out how to add yourself to me. 3:36 When it gets to reverse add, it goes oh, I turned myself in to an end. 3:38 So, we go, we run through that. 3:43 We're not going to go in to all these internals of how Python's magic 3:45 methods work. 3:48 That's for a later course. 3:48 But you can kind of see the movement of stuff. 3:50 But having these HTML reports is amazingly handy for 3:54 figuring out what you need to cover. 3:57 Those HTML reports are mind blowing the first time you show them to someone. 4:00 I've seen so many jaws drop. 4:03 I'll just assume that yours did too, okay? 4:05 Well with that, we're done with another course. 4:07 If you haven't written tests before either in Python or 4:09 in another language, I hope you'll start to. 4:12 It was a part of development that I shunned for a long time. 4:14 And I was way worse off for it. 4:17 Testing makes your code safer, smarter, and much more reliable. 4:18 Happy testing. 4:22
You need to sign up for Treehouse in order to download course files.Sign up