1

我对我拥有的一些 ctypes 代码有疑问,其中 pytest-coverage 过度夸大了我的“真实”测试覆盖率。

这是模块的样子(使用虚拟示例):

# mymod.py
add = clib.add
add.arg_types = [c_int, c_int]
add.res_type = c_int

问题是,当我运行时pyest_coverage,我的代码覆盖率显示为 100%,即使在我的单元测试中从未调用过python 。 add另有说明,如果我不这样做

#tests/test_mymod.py
assert mymod.add(1,2) == 3

我希望我的报道反映这一点,这意味着我想反映add“没有真正经过测试”。

选项

我的模块承受不起任何性能损失。我想到的一件事是这样做:

def add(a, b):
    # probably really bad performance wise if called many times!!!
    _add = clib.add
    _add.arg_types = [c_int, c_int]
    _add.res_type = c_int
    return _add(a,b)

或者

_add = clib.add
_add.arg_types = [c_int, c_int]
_add.res_type = c_int
def add(a, b):
    # questionable performance!!
    return _add(a,b)

但我担心这两种重组,为了测试,会影响代码的性能

有什么想法可以欺骗 pytest 或 pytest-coverage 不计add为已覆盖,除非某些测试函数实际调用add

4

0 回答 0