单元测试中的代码覆盖率 [英] Code coverage percentage in unit testing

查看:94
本文介绍了单元测试中的代码覆盖率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类,有3个方法(在python中).

I have a class, having 3 methods(in python) .

class MyClass:
    def A(self):
          .......
    def B(self):
         ........
    def C(self):
         ........

我只为一种方法A编写了单元测试用例.该单元测试涵盖了方法A的每一行.也就是说,我们没有if ... else或任何分支构造.

I have written unit test case for only one method A. This unit test covers each line of the method A. That is we don't have any if...else or any branching constructs.

代码覆盖率是多少?

同样,如果我为覆盖所有行的类的第二种方法编写另一个单元测试用例.现在的代码覆盖率是多少?

Again if I write another unit test case for 2nd method of the class covering all lines. What would be the code coverage percentage now?

推荐答案

我自己得到了答案:-)

I got the answer myself :-)

代码覆盖率全部取决于运行覆盖率的模块或文件.可以说,如果我们按照我提出问题的方式来对一个文件进行覆盖.每种方法的每一行都将考虑代码覆盖率.

Code coverage is all depends on which module or which files you are running the coverage for. Lets say if we run coverage for one file the way i had framed my question. Each line in each method will be accounted for code coverage.

现在按照我的问题,我只涵盖一种包含20行的方法.其他2种方法还有80条线(3种方法中总共100条线).因此,如果我为文件运行了覆盖范围.我只会得到20%的代码覆盖率.

Now as per my question i am covering only one method contain 20 lines. Other 2 methods have another 80 lines (total 100 lines in 3 methods). So if i ran coverage for my file. I would get code coverage of only 20%.

在python中,我们可以在pycharm终端中运行:coverage run -m py.test my_file.py要获取报告,请运行命令:coverage report -m py.test my_file.py

In python we can run (in pycharm terminal) like :coverage run -m py.test my_file.py To get the report run the command :coverage report -m py.test my_file.py

要针对整个模块(在所有软件包中)运行,请使用:coverage运行-m py.test和coverage报告-m py.test

To run for entire module (in all packages) use : coverage run -m py.test and coverage report -m py.test

这篇关于单元测试中的代码覆盖率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆