pytest-cov插件计算单元测试代码覆盖率

工作需要,领导让统计单元测试的代码覆盖率,试了试coverage.py库,发现测试代码和被测代码好像必须在同一目录下才行,如果不在同一目录就会找不到模块的错误,我在fileb目录下执行了coverage run test_mypath.py,结果如下图所示:

需要把被测试文件mymath.py和测试文件test_mymath.py放到同一目录下,才能执行coverage run,然在在用coverage report -m命令查看覆盖率,效果如下:

可以看到,test_mymath.py执行率只有43%,按理测试文件应该是100%的,执行coverage html生成html报告查看代码的详细执行情况如下:

可以看到测试文件和被测试文件都只执行了定义的代码,没有走函数的内部代码,因为我的测试文件里代码有问题:

# from fileb import mymath

import mymath

def test_subtract():
    result= mymath.subtract(1, 2)
    assert result==-1

def test_add_integers():
    """
    Test that the addition of two integers returns the correct total
    """
    result = mymath.add(1, 2)
    assert result==3


因为没写成测试类的形式,测试代码只有定义,没有调用执行,而pytest-cov就可以自动执行测试文件的全部代码,执行命令pytest --cov=./ 

效果如下,

可以看到,测试文件test_mymath.py全部执行了。注意导包方式,用from . import  mymath,直接import mypath会报错,

如果想用coverage run 命令让测试文件代码正确执行,应该用测试类的方式,如下:

注意,这回导包直接用import mypath导入,不能用from . import  mymath,有点坑....

# from filea import mymath
import mymath
import unittest

# from . import  mymath

class TestAdd(unittest.TestCase):
    """
    Test the add function from the mymath library
    """
    def test_subtract(self):
        result= mymath.subtract(1, 2)
        self.assertEqual(result,-1)

    def test_add_integers(self):
        """
        Test that the addition of two integers returns the correct total
        """
        result = mymath.add(1, 2)
        self.assertEqual(result, 3)

    def test_add_floats(self):
        """
        Test that the addition of two floats returns the correct result
        """
        result = mymath.add(10.5, 2)
        self.assertEqual(result, 12.5)

    def test_add_strings(self):
        """
        Test the addition of two strings returns the two string as one
        concatenated string
        """
        result = mymath.add('abc', 'def')
        self.assertEqual(result, 'abcdef')
if __name__ == '__main__':
    unittest.main()

---------------------------------

说正题,主要是讲pytest-cov这个测试带代码覆盖率的插件。

安装直接pip install pytest-cov就行。

首先我的项目的目录结构如下:

dc_dm_cac目录下包含了一些要被测试的文件比如tree.py commands.py,还包含了tests文件夹,里面全是测试文件,现在我想要一次性执行全部的测试文件,来看看代码的测试覆盖率。

我进入到dc_dm_cac目录下的终端,执行 pytest --cov=./ ,--cov后面的这个./代表要执行的目录,我这里表示当前目录下的所有test开头的文件。有些教程上我看有写的 pytest --cov=xxx.py 实际测试貌似不能加.py,直接写模块名,类似这样:pytest --cov=./tests/test_tree

执行 pytest --cov=./ 结果如下图:

 

52个测试全部通过了,不过报了100多个警告,不知道为啥。。。

可以看到test文件执行率全是100%,被测试文件有些覆盖率不到100%,比如history.py,有4行没有被执行到。

想要知道具体哪三行,我们可以打出html报告,显示出详情,可在加个--cov-report=html 参数,

执行命令,pytest --cov=./ --cov-report=html 

在dc_dm_cac 文件夹下生成了一个htmlcov文件夹,

打开里面的index.html文件,在点进history,可以看到红色部分就是没有测试到的代码

右上角还有一个小键盘展示快捷键的:

 

可以按j和k切到上一行或者下一行没被测试的代码。

 

有时候我们不需要显示测试文件的覆盖率,或者不想看到哪个文件的覆盖率,可以通过配置文件来忽略一些文件,

在tests目录下新建一个.coveragerc文件,

 

文件内容如下:

[run]
omit =
    tests/*
    undo.py
    __init__.py

第一行的tests/* 表示忽略tests文件夹下的所有文件

第二行表示忽略undo.py

第三行忽略的 __init__.py

然后执行pytest --cov=./  --cov-config ./tests/.coveragerc

其中--cov-config ./tests/.coveragerc就表示使用哪个配置文件

执行结果如下,此时就看不到test文件夹下的文件、undo.py和__init__.py文件的覆盖率了

 

最后再说一个参数-s,如果想在测试代码是使用print打印出信息,需要添加-s,不然测试文件正常执行的话不会打印出信息,但是如果测试没有通过会打印出信息,

最后执行➜  ~ pytest --cov=./ --cov-report=html --cov-config ./tests/.coveragerc -s

可以看到成功打出了想要的这些信息。

 

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值