Effective PyCharm 2025 Transcripts
Chapter: Testing
Lecture: What Code is Covered by Tests
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
So you feel like you've tested your code, right? All of it? Now, how much code, how much test code you write and how much test coverage and how much of your program you actually feel need tested can vary a lot depending on the type of app, how complex that code is, how central it is to it.
0:21
Do you need to test the about page on your web app or the about box in your desktop app? Probably not. But if it's core to your application, you should test it. So do you know how well that part that you really want to test is tested? Well, luckily, PyCharm can tell us using something called code coverage. Now, let me cover, let me cancel this out, comment this out here so that we're not using all of our tests.
0:49
And then let's run our tests here. And do they pass? Well, how about that? They sure do pass. Amazing. But how much of our code in this core library is actually tested?
1:00
So what we can do is we can come up here and we can run it, we can debug it as we saw, or we can click over here and hit run with coverage. And this will tell us what portions of our code is executed when the tests run, and what portions are not even touched ever.
1:17
If you find a portion of code that is never ever run when you run all of the tests, you're not testing that part of code, are you? We can also do profiling, which is understanding the CPU and memory performance of our application.
1:33
We're not going to do that now, although we could, but we want to focus on this answering this question of how much of our code is tested. Well, let's find out we run this. And it says, well, you're going to need coverage.py from a Ned Batchelder.
1:45
You want to install it or enable the built in one. Let's see what happens if I click install.
1:51
It says it failed. Why did it fail? Because it's done by UV and PyCharm. Although it supports UV, it supports UV somewhat. So UV pip install coverage.
2:03
There we go. And let's try to run it again with coverage. Here we go. All right, it ran just the same. Perfect. And does it tell us stuff down here? Well, we wrote an XML report.
2:15
You want a flashback? XML. There it is. We don't need to see that though. We don't care. PyCharm reads that and the other information. And then it just gives us pretty awesome reports here.
2:27
Check this out. So, well, the tests, 100% of the tests were run when we ran 100% of the tests. Amazing. They theoretically could be conditionals and stuff, but you don't really care about that number.
2:38
You want to look at your program. Well, the program itself is like a front end to it. We didn't test that. That's fine. 96% of our core library is covered.
2:49
Well, that's pretty cool. What parts are covered and what parts aren't? So let's go over here and open this up. So, right, we've got this report over here on the left, which is really nice.
3:01
We've got this specific report on the right, but let's just open this up in the editor and push that away for a minute.
3:11
Notice we scroll through there's green, green, green gap, because that's just a space that doesn't really execute.
3:19
But look at line 33 and 34, red. This line of code, this raised table error was never, ever executed.
3:30
So we've done the test as the table booked. It's always been false. So we just carried on and done the thing.
3:36
Let's see what else is here. All right. Well, it's just this one. What do we need to test this portion of code?
3:42
Well, we need to give it an invalid table, a table that's unavailable, and try to book it. What test did I comment out?
3:50
Well, well, well, test that you cannot book a booked table. Let's put this back and write, we're trying to book a table that we've already booked.
4:00
And so that should trigger that error handling and that checking so that we don't double book anyone.
4:06
Let's start in again, see if there's any difference. Do you want to display? Yes, let's replace it.
4:13
So now we have a another test here, this one. And look at that. Our test coverage went from 96 or 97, whatever it was, up to 100%.
4:24
And we go back to this line here, where we're doing our test, this portion, line 33 in particular, has now been executed.
4:33
How awesome is that? So now, yes, every bit of code is tested. Every conceivable combination of possibilities tested.
4:41
We don't know, probably not. But we know at least every bit of code has got some bit of execution during a test.
4:50
And that's pretty excellent. So using code coverage is super, super simple. Just know you got to install it and then you run it. Coverage. That's it.