Spot Coverage Gaps Using the Gutter

Let the IDE help you spot coverage gaps in your testing.

Visual coverage to the rescue.

You know you need good tests. You know that means code coverage, but it's fiddly to set up and not integrated into your flow. Meet visual coverage.

In Depth

Note: Code coverage is a PyCharm Professional feature.

Writing tests for your code is good, but how do you know when you've done enough testing? "Code coverage", via the coverage package, combines your tests and the execution of your code to see and report what spots get hit.

PyCharm makes this easy to set up. You just run the Run with Coverage action, for example by clicking the button in the toolbar, and PyCharm runs your tests with the bundled (or you can use a coverage that is installed in your project interpreter.)

Once coverage runs, you'll get a tool window showing a filesystem-like view of your project with statistics at each level.

But better yet, and the subject of this tip, your editor's gutter will be color-coded with coverage information: green for covered and red for not covered. If you checked the preference for "branch coverage", the gutter decorations will include branches in your code.

Those gutter decorations are not just visual: click a line in the gutter to get a popup with some statistics.

Full Video