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


Talk Python's Mastodon Michael Kennedy's Mastodon