Effective PyCharm 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 so we can see a little bit more in action.
0:07 If we've got a simple little program like this one file, 100 lines of code. We want to know how much of our tests actually interacts
0:16 with how much of that program. We can probably reason about it, even though it's not 100% obvious, we can probably figure out that yeah,
0:23 this these tests are exercising the code well enough. If you've got code that is not being run at all by the tests then the
0:31 tests say absolutely nothing about the quality of that code other than maybe it can be parsed by Python. So maybe the syntax is mostly okay,
0:39 so how do we know, you're in a real project We might have hundreds of tests and thousands of lines of Python.
0:44 How do we know what part of code being tested by the test we've written so far, that's code coverage. So we can go up here and we can press
0:53 another one of these buttons we've seen profiling debugging regular running, but now we have run with coverage the first time you run this,
1:00 it might say coverage is not installed. Just click enable bundled coverage and you should be good to go,
1:06 we click this and it takes a little bit longer because it's thinking a whole lot about what's happening and then whoa,
1:11 there's a lot of information on the screen here and check this out. So we've got a like a graphical thing going on over here,
1:16 like an Excel type report, Scroll down into the demos projects, a bunch of this is not covered because it's not really part of this project.
1:25 Let's go to the one that is the tests. Well, guess what? The test ray? And when we ran the test, that doesn't mean anything. Let's check this out.
1:31 So over here, in core we have 75% of the lines and program, we're not exercising that. So that's useful information already.
1:40 If we put that to the side, look at this, this is way better over here. It gives you right within the project,
1:46 a nice report showing you for each level that you go into it. Right? So here's our core definitely tested and now even better remember how we had
1:57 GIT hub showing us on the side here, look at this. So for example, here this green code. This means it was hit by code coverage and this red
2:06 part here means that it wasn't. So that is fantastic. I'm not sure if this comes through what color comes through in the video when it's exported,
2:15 but light green, faint green and faint red here. If we want to improve the code coverage of this actual file here of our core
2:24 Well, we need to make a couple of things happen. We need to make this case run,
2:31 we've run the test but we've never run this and we've run this test but we've never run this. Let's go in,
2:37 quote, right. Another testament to re enable this one. This will be about a non existent table.
2:43 So when I run it, notice this red part should go away right again. Running again with coverage and here we go.
2:52 Look that first part about the non existent table. Now, that's running fantastic. Let's go and finish it off by running that little
3:00 bit there. That's going to be this last test. You can run with coverage. Perfect it, pass and check it out. All of that is good.
3:13 There is something else going on here that still thinks it's not running. But when I look at this file,
3:19 it looks like this is 100% code coverage, as far as I'm concerned.

Talk Python's Mastodon Michael Kennedy's Mastodon