Over a million developers have joined DZone.

Unittest's Code Coverage in PyCharm

DZone 's Guide to

Unittest's Code Coverage in PyCharm

· Web Dev Zone ·
Free Resource

PyCharm‘s my favorite IDE in years. Granted, learning how to use it can be like the first few minutes of Flight of the Navigator, but whenever I begin a new kind of task, PyCharm surprises me with the depth of its feature set.

Today was my first day at 10gen. One of my first tasks is to assess the Mongo Python driver’s unittesting coverage. (Summary: coverage is pretty good, but not great.) Nosetests and coverage.py can give me an overview, but how awesome would it be if I could see which lines of code are exercised by the unittests in my IDE?

PyCharm, as of the October 4 Early Access Preview, can run unittests with code coverage analysis within the IDE. Here’s how.

pymongo‘s unittests are all in the tests/ directory, as they ought to be, so in PyCharm you can simply open the Project pane and right-click the tests/ directory and choose “Run with Coverage”:

Run tests

PyCharm will show you your tests’ outcomes in a nice tree diagram:

Test Results

It also displays which lines were exercised by your unittests, and which were not, in two places. First, in your source files themselves, by adding a green (exercised) or red (omitted) bar to the left of the source lines:

Source coverage

You can see here that I started my local Mongo server without IPv6 support, so the IPv6 unittest was skipped.

You can also see test coverage in your Project pane, neatly rolling up coverage statistics by file and by directory:

Code coverage rollup

If you close the project and re-open it, you’ll lose your code-coverage display, but you can redisplay it with Tools->Show Code Coverage Data. PyCharm will show you coverage data from recent runs and let you re-display it.

As always with PyCharm, it takes a little fiddling and Googling to get all this to work, but the result is simply brilliant. I hope this post helps you find your way through a valuable feature.

Source: http://emptysquare.net/blog/unittests-code-coverage-in-pycharm/


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}