Effective PyCharm (2021 edition) Transcripts
Chapter: Unit testing
Lecture: Measuring test quality with code coverage

Login or purchase this course to watch this video and the rest of the course contents.
0:00 These air conditioned tests were great but let's just comment them out just for a moment
0:03 so we can see a little bit more in action.
0:06 If we've got a simple little program like this one file,
0:10 100 lines of code. We want to know how much of our tests actually interacts
0:15 with how much of that program.
0:17 We can probably reason about it,
0:18 even though it's not 100% obvious,
0:20 we can probably figure out that yeah,
0:22 this these tests are exercising the code well enough.
0:25 If you've got code that is not being run at all by the tests then the
0:30 tests say absolutely nothing about the quality of that code other than maybe it can be
0:34 parsed by python. So maybe the syntax is mostly okay,
0:38 so how do we know, you're in a real project
0:40 We might have hundreds of tests and thousands of lines of python.
0:43 How do we know what part of code being tested by the test we've written so
0:47 far, that's code coverage. So we can go up here and we can press
0:52 another one of these buttons we've seen profiling debugging regular running,
0:55 but now we have run with coverage the first time you run this,
0:59 it might say coverage is not installed.
1:01 Just click enable bundled coverage and you should be good to go,
1:05 we click this and it takes a little bit longer because it's thinking a whole lot
1:08 about what's happening and then whoa,
1:10 there's a lot of information on the screen here and check this out.
1:12 So we've got a like a graphical thing going on over here,
1:15 like an Excel type report, Scroll down into the demos projects,
1:20 a bunch of this is not covered because it's not really part of this project.
1:24 Let's go to the one that is the tests.
1:26 Well, guess what? The test ray?
1:27 And when we ran the test,
1:28 that doesn't mean anything. Let's check this out.
1:30 So over here, in core we have 75% of the lines and program,
1:35 we're not exercising that. So that's useful information already.
1:39 If we put that to the side,
1:40 look at this, this is way better over here.
1:43 It gives you right within the project,
1:45 a nice report showing you for each level that you go into it.
1:50 Right? So here's our core definitely tested and now even better remember how we had
1:56 GIT hub showing us on the side here,
1:59 look at this. So for example,
2:01 here this green code. This means it was hit by code coverage and this red
2:05 part here means that it wasn't.
2:07 So that is fantastic. I'm not sure if this comes through what color comes through
2:12 in the video when it's exported,
2:14 but light green, faint green and faint red here.
2:18 If we want to improve the code coverage of this actual file here of our core
2:23 Well, we need to make a couple of things happen.
2:27 We need to make this case run,
2:30 we've run the test but we've never run this and we've run this test but we've
2:33 never run this. Let's go in,
2:36 quote, right. Another testament to re enable this one.
2:41 This will be about a non existent table.
2:42 So when I run it, notice this red part should go away right again.
2:48 Running again with coverage and here we go.
2:51 Look that first part about the non existent table.
2:54 Now, that's running fantastic. Let's go and finish it off by running that little
2:59 bit there. That's going to be this last test.
3:03 You can run with coverage. Perfect it,
3:08 pass and check it out. All of that is good.
3:12 There is something else going on here that still thinks it's not running.
3:16 But when I look at this file,
3:18 it looks like this is 100% code coverage, as far as I'm concerned.