How to exclude mock package from python coverage report using nosetests

Frederick Roth picture Frederick Roth · Aug 29, 2012 · Viewed 12.7k times · Source

I currently try to use the mock library to write some basic nose unittests in python.

After finishing some basic example I now tried to use nosetests --with-coverage and now I have the mock package and the package I tried to 'mock away' are shown in the coverage report. Is there a possibility to exclude these?

Here is the class I want to test:

from imaplib import IMAP4

class ImapProxy:
    def __init__(self, host):
        self._client = IMAP4(host)

And the testcase: from mock import patch

from ImapProxy import ImapProxy

class TestImap:
    def test_connect(self):
        with patch('ImapProxy.IMAP4') as imapMock:
            proxy = ImapProxy("testhost")
            imapMock.assert_called_once_with("testhost")

I now get the following output for nosetests --with-coverage

.
Name         Stmts   Miss  Cover   Missing
------------------------------------------
ImapProxy        4      0   100%   
imaplib        675    675     0%   23-1519
mock          1240    810    35%   [ a lot of lines]

Is there any way to exclude the mock package and the imaplib package without having to manually whitelisting all but those packages by --cover-package=PACKAGE

Thanks to Ned Batchelder I now know about the .coveragerc file, thanks for that!

I created a .coveragerc file with the following content:

[report]
omit = *mock*

Now my output for mock in the coverage report is:

mock                     1240   1240     0%   16-2356

It does not cover the mock package any longer but still shows it in the report.

I use Coverage.py, version 3.5.2 if this is any help.

Answer

Ned Batchelder picture Ned Batchelder · Aug 30, 2012

Create a .coveragerc file that excludes what you don't want in the report: http://nedbatchelder.com/code/coverage/config.html