Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
`coverage.py` is an amazing library for determining how much of your code is covered by your tests and how much still needs to be tested. Let's look at how to install it, run it, and get handy reports from it.
Installing coverage.py
pip install coverage
Using coverage.py
Make sure you test file can be run from the command line without the -m unittest
argument.
coverage run tests.py
Generate a report
coverage report
or coverage report -m
if you want the missed lines.
-
0:00
[SOUND] So, how do you know if you've tested everything in your code base?
-
0:08
Sure, you can look at each line of code and
-
0:10
make sure that there's a test that corresponds to it, but that's tedious.
-
0:13
And then remember, every time you have an if or
-
0:16
an l if, you need to test both branches of it.
-
0:19
Yuck.
-
0:20
Thankfully there's an excellent Python library out there named coverage.py
-
0:24
from Gareth Rees and Ned Batchelder.
-
0:26
This amazing tool handles that line by line and branch by branch checking for
-
0:30
you, and gives you back some amazingly useful information
-
0:33
about where you're missing tests.
-
0:35
Let's go see how to use it.
-
0:36
If you're following along at home,
-
0:38
you'll need to install coverage with pip install coverage.
-
0:41
Before we start using coverage, let's add our if name block to our tests.
-
0:48
So we're gonna go down here.
-
0:50
If name is equal to main
-
0:55
unittest.main.
-
0:59
To use coverage,
-
1:00
we need to be able to run our script directly, I mean it's the easiest way,
-
1:04
there are ways of, of configuring things but this is the easiest way.
-
1:09
So, we're going to leave that in there.
-
1:10
Now, coverage itself is actually just as simple to use as running your test is.
-
1:15
We've been running our test suite with the Python interpreter, you know Python -m.
-
1:20
To run them with coverage, what we want to do, let me clear this,
-
1:23
is we actually just do coverage run, and then our script, so test.py.
-
1:30
And we get the same output that we've always gotten.
-
1:33
We've got the six dots and we've got ran six tests and a few seconds or
-
1:37
a few thousandth of a second and everything's okay.
-
1:40
No failing tests.
-
1:41
We already knew that, because we've got them all to pass in the last video, right?
-
1:45
So, so why use coverage?
-
1:46
This is what it does.
-
1:47
This isn't all it does.
-
1:49
Here's the magic of coverage.
-
1:50
So now we can do coverage report, and look at this.
-
1:55
Now we don't really care about what's here inside tests.
-
1:58
It should always be 100%, but
-
2:00
if it's not we really don't care, because it doesn't matter.
-
2:02
That's our tests.
-
2:03
That's fine.
-
2:04
What does matter though is this line, and we can see that we missed 11 statements.
-
2:09
There are 50 statements total, we missed 11 of them.
-
2:12
And our coverage is at 78%.
-
2:14
78's not bad. That's, that's actually pretty decent.
-
2:17
Now what do we mean by covered?
-
2:18
What's covered, what's uncovered?
-
2:20
Covered is when a line of code in our dice.py has a test that corresponds to it.
-
2:27
And our uncovered code is code that isn't tested, 'kay.
-
2:32
So we know that we're missing 11 lines, but what 11 lines are we missing?
-
2:36
Well, to find that we need to run coverage report and let me give it the -m flag.
-
2:43
M stands for missing.
-
2:46
So if we look at this 13 to 14, 13 and 14, 26, 60,
-
2:51
63, 66, 69, and 72 through 75 are not tested.
-
2:55
So, those are the lines that we care about.
-
2:57
Those are the ones that we want to get covered.
-
3:00
So, let's write a test to cover some more of our module.
-
3:03
Let's look at lines 13 through 14.
-
3:05
So, come back over to dice.py.
-
3:09
Come up here and we look at 13 and 14.
-
3:11
So that's this assertion error, so if we try to make too small of a die.
-
3:17
'Kay, so, let's fix that, that's easy enough to test.
-
3:20
So come back over here to our tests, to our die tests and
-
3:25
let's do def test_bad_sides, because that's what we're doing.
-
3:32
We're making a test that has a bad number of sides.
-
3:34
So with self.assertRaises, and it's gonna raise a value error,
-
3:43
we want to do dice.Die with one side.
-
3:48
Okay, so we know that's gonna fail.
-
3:51
But now we need to run our tests and generate our report again.
-
3:55
So back down here, coverage run tests.py.
-
3:58
Why would six not be found in that range?
-
4:03
Let's try running them again.
-
4:05
Okay.
-
4:06
So seven test, everything's good.
-
4:08
So now we'll do coverage report.
-
4:11
We'll do -m again and now we're up to 82% and
-
4:15
we don't have the 13 or 14 as missing.
-
4:20
Those lines are now covered.
-
4:22
So we still have several lines that are uncovered.
-
4:25
Generally, you want to aim for about 90% or better in your coverage.
-
4:30
But if you look at our dice.py, sorry for jumping around so much here.
-
4:35
A lot of these lines that aren't covered are ones that
-
4:39
are aimed at Python's internals, like this repper line.
-
4:42
Testing these are fine, you can right test for these.
-
4:44
And in fact, you should go right test with them.
-
4:46
Go right test for these yourself.
-
4:48
But you don't have to.
-
4:49
These aren't things that you absolutely have to test.
-
4:52
It's never a bad thing though to hit 100% or better.
-
4:56
I honestly use coverage.py on every single large project I build.
-
5:00
It's saves so much time and trouble figuring out what I've tested and
-
5:03
what I haven't.
You need to sign up for Treehouse in order to download course files.
Sign up