如何使用nosetests从python覆盖率报告中排除模拟包 [英] How to exclude mock package from python coverage report using nosetests

查看:23
本文介绍了如何使用nosetests从python覆盖率报告中排除模拟包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前尝试使用mock库在python中编写一些基本的鼻子单元测试.

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

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

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?

这是我要测试的课程:

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")

我现在得到 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]

有什么方法可以排除 mock 包和 imaplib 包而无需通过 --cover-package=PACKAGE

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

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

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

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

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.

如果有帮助,我使用 Coverage.py,版本 3.5.2.

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

推荐答案

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

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

这篇关于如何使用nosetests从python覆盖率报告中排除模拟包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆