Spot Coverage Gaps Using the Gutter

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

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 coverage.py (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.


Related Resources

Speed Up Coverage with Configuration File
Speed up your "visual coverage" with a config file and correct working directory.
Split Screen Without Tabs
Get your code and tests side-by-side without resorting to tabs.
Auto-Run Tests
Get into testing mode by telling PyCharm to automatically re-run tests as you type.