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

How to exclude mock package from python coverage report using nosetests(如何使用nosetests从python覆盖率报告中排除模拟包)
本文介绍了如何使用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覆盖率报告中排除模拟包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

patching a class yields quot;AttributeError: Mock object has no attributequot; when accessing instance attributes(修补类会产生“AttributeError:Mock object has no attribute;访问实例属性时)
How to mock lt;ModelClassgt;.query.filter_by() in Flask-SqlAlchemy(如何在 Flask-SqlAlchemy 中模拟 lt;ModelClassgt;.query.filter_by())
FTPLIB error socket.gaierror: [Errno 8] nodename nor servname provided, or not known(FTPLIB 错误 socket.gaierror: [Errno 8] nodename nor servname provided, or not known)
Weird numpy.sum behavior when adding zeros(添加零时奇怪的 numpy.sum 行为)
Why does the #39;int#39; object is not callable error occur when using the sum() function?(为什么在使用 sum() 函数时会出现 int object is not callable 错误?)
How to sum in pandas by unique index in several columns?(如何通过几列中的唯一索引对 pandas 求和?)