如何使用nosetests从python覆盖率报告中排除模拟包

时间:2022-02-01 16:54:35

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

我目前尝试使用模拟库在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?

在完成一些基本的例子后,我现在尝试使用nosetests --with-coverage,现在我有了模拟包,我试图'模拟掉'的包显示在覆盖率报告中。是否有可能排除这些?

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

我现在得到以下输出的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

有没有办法排除模拟包和imaplib包,而不必通过--cover-package = PACKAGE手动将所有包列入白名单

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

感谢Ned Batchelder,我现在知道.coveragerc文件了,谢谢!

I created a .coveragerc file with the following content:

我创建了一个包含以下内容的.coveragerc文件:

[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.

如果这是任何帮助,我使用Coverage.py,版本3.5.2。

3 个解决方案

#1


32  

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

创建一个.coveragerc文件,该文件排除了报告中不需要的内容:http://nedbatchelder.com/code/coverage/config.html

#2


14  

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

在.coveragerc中将省略条目从[report]部分移动到[run]部分。

#3


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.

我有一个类似的情况测试我的主程序包目录中的一系列子包。我在我的模块的*目录中运行nosetests,Mock和其他库包含在coverage报告中。我尝试在nosetests中使用--cover-module my_package,但后来没有包含子包。

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.

因此,如果您要测试的所有代码都在同一目录中,那么您可以通过指定nosetests的模块路径来单独获取它的覆盖范围。这样就无需单独将每个子模块列入白名单。

(Python 2.7.6, coverage 4.0.3, nose 1.3.7)

(Python 2.7.6,coverage 4.0.3,nose 1.3.7)

#1


32  

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

创建一个.coveragerc文件,该文件排除了报告中不需要的内容:http://nedbatchelder.com/code/coverage/config.html

#2


14  

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

在.coveragerc中将省略条目从[report]部分移动到[run]部分。

#3


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.

我有一个类似的情况测试我的主程序包目录中的一系列子包。我在我的模块的*目录中运行nosetests,Mock和其他库包含在coverage报告中。我尝试在nosetests中使用--cover-module my_package,但后来没有包含子包。

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.

因此,如果您要测试的所有代码都在同一目录中,那么您可以通过指定nosetests的模块路径来单独获取它的覆盖范围。这样就无需单独将每个子模块列入白名单。

(Python 2.7.6, coverage 4.0.3, nose 1.3.7)

(Python 2.7.6,coverage 4.0.3,nose 1.3.7)