問題描述
我目前嘗試使用mock庫在python中編寫一些基本的鼻子單元測試.
I currently try to use the mock library to write some basic nose unittests in python.
完成一些基本示例后,我現(xiàn)在嘗試使用 nosetests --with-coverage
,現(xiàn)在我有了模擬包,我嘗試模擬"的包顯示在覆蓋率報(bào)告中.有沒有可能排除這些?
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)
還有測試用例:從模擬導(dǎo)入補(bǔ)丁
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")
我現(xiàn)在得到 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,我現(xiàn)在知道了 .coveragerc 文件,謝謝!
Thanks to Ned Batchelder I now know about the .coveragerc file, thanks for that!
我創(chuàng)建了一個(gè)包含以下內(nèi)容的 .coveragerc 文件:
I created a .coveragerc file with the following content:
[report]
omit = *mock*
現(xiàn)在我在覆蓋率報(bào)告中的模擬輸出是:
Now my output for mock in the coverage report is:
mock 1240 1240 0% 16-2356
它不再涵蓋模擬包,但仍顯示在報(bào)告中.
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.
推薦答案
創(chuàng)建一個(gè) .coveragerc 文件,在報(bào)告中排除您不想要的內(nèi)容: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
這篇關(guān)于如何使用nosetests從python覆蓋率報(bào)告中排除模擬包的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!