Unittests’ code coverage in PyCharm

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.

  • Anonymous

    How did you get the IDE to show green/red along the side? I am using PyCharm 2.0.2 and I can run tests with coverage and get the stats in the project view. But I can’t get the editor to show the green/red.

    • http://emptysquare.net/ A. Jesse Jiryu Davis

      Not sure, Jonathan — it’s still working for me in PyCharm 2.1 beta. Check the documentation, PyCharm is extremely (too?) configurable:

      http://www.jetbrains.com/pycharm/webhelp/code-coverage-2.html

      • Anonymous

        I searched on the JetBrains forums and it appears that a few people have this issue: http://devnet.jetbrains.net/thread/433497 – I did find the configuration options and they were all set. I’ll open a ticket with JetBrains.

        I’ll have to look at more of your blog – we are just switching to Python at work, so a few of us are just learning it. So far I love it (coming from Java myself, other teammates coming from Perl).