Mastering PyCharm Transcripts
Chapter: Unit testing
Lecture: Concepts: Coverage

Login or purchase this course to watch this video and the rest of the course contents.
0:01 Code coverage goes hand in hand with unit testing,
0:05 one validates the other, it's great to have unit test,
0:08 but to say your test passed, well that only has meaning
0:11 if you have significant code coverage on your application.
0:14 So we saw that code coverage is a measure
0:17 used to describe the degree where the source code of the program
0:20 is tested by a particular set of tests.
0:23 To do this in PyCharm, it's super easy
0:26 there's a little button right there.
0:28 So you can just click that or sometimes depending on how your tests are set up
0:32 you can right click and say run this with coverage.
0:35 So this is really great, and then you get some output,
0:40 however, the first time you try it on a particular project,
0:43 it might not work, it might not have coverage.py installed
0:48 so that's super easy to do, pip install coverage.py
0:51 or just click that little button and a little hyperlink right there,
0:55 and it will install it for you.
0:57 Then click the button again and you'll run it with coverage.
1:00 That results in some code coverage results coming out,
1:03 chances are very good that you'll have
1:06 close to 100% coverage of your test,
1:08 that doesn't tell you anything, that just means the tests were run,
1:11 you want to look at the subject under test,
1:13 so in this case the thing that is really important is core.py
1:16 and notice it's 93% covered and we have two tests,
1:20 these are the happy path tests,
1:22 and what we didn't really cover in this particular stage here
1:25 where we took the screenshot, is we didn't test the error handling
1:28 so there's like 7% of error handling just not covered,
1:32 mostly the error handling altogether set which represents 7%.
1:35 But notice what we have on the right a kind of tabular view
1:39 and then over in the projects,
1:41 we also have the coverage right in the project view,
1:44 that's really actually super cool,
1:47 so we have 93% of lines covered here
1:50 and you can see over there as well.
1:53 However, the real value, one of the real cool features that makes it really useful
1:58 is the results also show up in the editor,
2:01 so here you can see this is greenish,
2:04 I'm not sure what it comes out in the video after processing,
2:07 but it's a slight, slight green, it's very subtle
2:09 but that means that function, every bit of it
2:12 was tested by our test, that's cool.
2:15 However that part right here, that line 34
2:19 that was not tested, neither was that.
2:22 So it's telling us we need some more tests,
2:26 so let's go write some more tests
2:29 and basically if we write two more tests, as we did
2:32 that test the attempt to book a table with the wrong id
2:36 and attempt to double book the table,
2:39 that'll run the two missing lines,
2:41 we did that here and now we have 100% code coverage on our core stuff,
2:45 that's pretty awesome, like I said,
2:47 you don't have to have 100% code coverage
2:49 but for the core essence of your app,
2:51 you should probably have test for that
2:54 and you can really determine what you need to do super easily with PyCharm
2:58 and especially in the editor like this.