30

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.

Frederick Roth
  • 2,748
  • 4
  • 28
  • 42

3 Answers3

32

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

Ned Batchelder
  • 364,293
  • 75
  • 561
  • 662
  • Could you have a look at the information I added in my question? I am not sure whether it is an error on my side or if omit in the [report] block is not working correctly. – Frederick Roth Sep 01 '12 at 13:37
  • 3
    I am facing a similar problem, and created a `coveragerc` file which contains inclusions and omissions. However, nose does not seem to consider it and proceeds to run coverage on all of Python's libraries (despite `cover_pylib` being set to `False` in the configuration file). Any idea how to make coveragerc work with nose? – Boris Feb 24 '13 at 19:05
  • 1
    Ok now after Thomas E Jenkins gave another answer I retested it and the current coverage version does not have this behavior any longer. – Frederick Roth May 07 '13 at 21:33
  • So there is no way to include the .coveragerc file any longer? How does one go about excluding items from a test? – digitaldavenyc Dec 17 '14 at 18:23
  • @digitaldavenyc coverage.py still supports .coveragerc files. If you are having a problem, let's start another thread. – Ned Batchelder Dec 17 '14 at 21:41
14

In your .coveragerc move your omit entry from the [report] section to the [run] section.

  • Exactly. `[report]` suppresses per-file reporting but does not alter the % calculations. `[run]` works for me. – cdunn2001 Jan 18 '14 at 22:45
2

I had a similar situation testing a series of sub-packages within my main package directory. I was running nosetests from within the top directory of my module and Mock and other libraries were included in the coverage report. I tried using --cover-module my_package in nosetests, but then the subpackages were not included.

Running the following solved my problem:

nosetests --with-coverage --cover-erase --cover-package ../my_package

So, if all the code that you want to test is in the same directory, then you can get coverage for it alone by specifying the module path to nosetests. This avoids the need to whitelist each of the submodules individually.

(Python 2.7.6, coverage 4.0.3, nose 1.3.7)