我正在努力达到100%的覆盖率。
我有文件(app/ifaces.py):
import netifaces
class NoIPException(Exception):
pass
def get_local_ips():
...(code here)我还有个测试:
import pytest
import mock
import netifaces
from app import ifaces
def test_get_local_ips_normal_case():
....当我手动运行测试时:
py.test -v -cov应用程序-cov-报告术语缺失
它报告100%的代码覆盖率: app/ifaces 16 0 100%
但是,当我将它作为“自运行”添加到测试中时,它报告说前六行没有包括在内:
if __name__ == "__main__":
import sys
pytest.main("-v %s --cov app/ifaces.py --cov-report term-missing" % sys.argv[0])报告:
Name Stmts Miss Cover Missing
--------------------------------------------
app/ifaces 16 4 75% 1-6如何添加自运行测试以获得与手动py.test执行相同的结果?结果之间有什么区别?为什么在第二种情况中没有报道app/ifaces.py中的6行呢?
谢谢。
发布于 2016-01-19 17:13:28
好吧,我找到理由了。
当从测试本身调用pytest时,所有导入都已经完成,因此不计算它们。
为了覆盖它,它们需要在pytest-cov执行过程中导入。
我的解决方案是使用pytest夹具进行导入: 1.从测试程序的顶部删除“从应用程序导入ifaces”。2.增加固定装置:
@pytest.fixture
def ifaces():
from app import ifaces
return ifaces3.使其可作为测试的变量:
def test_get_local_ips_normal_case(ifaces)
....https://stackoverflow.com/questions/34882426
复制相似问题