coverage.py 测量函数和类定义吗?

2024-05-14 10:04:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图实现一个基本python模块的100%覆盖率。 我用Ned Batchelder的覆盖率.py模块来测试它。在

1 class account(object):
2   def __init__(self, initial_balance=0):
3     self.balance = initial_balance
4   def add_one(self):
5    self.balance = self.balance + 1

这些是测试。在

^{pr2}$

这是我得到的报道。在

    COVERAGE REPORT =
    Name                    Stmts   Miss  Cover   Missing
   -----------------------------------------------------
   __init__                    1      1     0%   1
   account1                    5      3    40%   1-2, 4
   account2                    7      7     0%   1-7

如我们所见,第1-2行和第4行是不包括的定义。 执行其余的行。在


Tags: 模块pyselfaddobjectinitdef覆盖率
2条回答

按照jcollado的回答:

我对Django nose有这个问题,它只覆盖测试使用的行。在

为了修复它,我首先启动manage.py,然后启动测试。 .coverage文件将包含这两个报告。在

我的第一个命令是a custom,它打印我的项目设置。示例:

coverage run ./manage.py settings && ./manage.py test myapp

我想你的问题在FAQ中有描述:

Q: Why do the bodies of functions (or classes) show as executed, but the def lines do not?

This happens because coverage is started after the functions are defined. The definition lines are executed without coverage measurement, then coverage is started, then the function is called. This means the body is measured, but the definition of the function itself is not.

To fix this, start coverage earlier. If you use the command line to run your program with coverage, then your entire program will be monitored. If you are using the API, you need to call coverage.start() before importing the modules that define your functions.

相关问题 更多 >

    热门问题