gpt4 book ai didi

python - coverage.py 是否测量函数和类定义?

转载 作者:IT老高 更新时间:2023-10-28 21:11:36 29 4
gpt4 key购买 nike

我正在尝试实现基本 python 模块的 100% 覆盖。我使用 Ned Batchelder 的 coverage.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

这些是测试。

class TestAccount(unittest.TestCase):
def test_create_edit_account(self):
a = account1.account()
a.add_one()

这是我得到的覆盖率报告。

    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 行没有被覆盖,它们是定义。其余的行都被执行了。

最佳答案

我认为 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.

关于python - coverage.py 是否测量函数和类定义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8636828/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com