我目前尝试使用模拟库在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]是否可以通过--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.
如果有帮助,我会使用3.5.2版的Coverage.py.
I use Coverage.py, version 3.5.2 if this is any help.
推荐答案创建一个.coveragerc文件,该文件排除了报告中不需要的内容: nedbatchelder/code/coverage/config.html
Create a .coveragerc file that excludes what you don't want in the report: nedbatchelder/code/coverage/config.html
更多推荐
如何使用鼻子测试将模拟包从python覆盖率报告中排除
发布评论