Effective PyCharm (2021 edition) Transcripts
Chapter: Unit testing
Lecture: Concept: Coverage
0:00 Code coverage goes hand in hand with testing,
0:02 it tells you do you just happen to have tests or are those tests actually testing
0:07 what's important about your program and how much So the official definition code coverage is a
0:13 measure used to describe the degree to which the source code of a program is executed
0:18 when a particular test suite runs we saw that PyCharm has fantastic support built in
0:24 for a code coverage. All you have to do is go to your test file
0:28 or test folder. You want to run,
0:30 go down a right click on it and you can expand this out and say run
0:33 py test in test.py with coverage.
0:37 Once you already have a run configuration,
0:39 you can just go up here and click the shield icon to run the test with
0:44 coverage again, remember pressing CTRL+R or whatever your run command hotkey is will not
0:49 run it with coverage. So you need to press that button or use the hotkey
0:52 specifically for that option. The first time you run it may give you a warning
0:57 and say whoops you don't have coverage py or you should use the bundled one.
1:01 So I feel like a safe option is just click enable bundled coverage unless you want
1:05 to make sure that coverage is included in the requirements.txt
1:08 for other people running again,
1:11 it should work and you'll get to really cool things.
1:14 You'll get kind of excel like view on the right big report showing you whatever level
1:20 you are navigated into your project,
1:22 what is covered. What is not covered,
1:24 how much and so on but even better on the left is we have this super
1:28 cool view into the project structure and hierarchically showing us well in the projects we got
1:34 6% and unit testing we've got 66% encore.
1:37 We got 93% as you dive down.
1:39 So really, really nice. This is my favorite way to just get a quick
1:43 view and then once you say want to see the details about what are the 7%
1:47 of uncovered lines in core.py double click that.
1:50 And you'll end up here in the editor on the left here you'll see green sections
1:55 This is where our tests actually ran that code And possibly red sections like this
2:00 one We had no test for trying to pass in a non existent table.
2:05 So the air condition never ran and line 34 was never run so we might want
2:10 to write more tests. Same thing for line 37.
2:14 So if we write some more tests,
2:15 the ones that check for the error conditions in particular right?
2:19 The one that tries to book a non existent table or try to double book a
2:22 table. We rerun it with code coverage notice on the left here,
2:25 everything is good, totally green. Now we have got 100% code coverage on 'core.py'.