有没有可能让一个装潢师忽略这个覆盖范围?

2024-05-11 18:26:57 发布

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

我构建了一个简单的装饰器,希望它能使覆盖率忽略应用它的任何内容。从经验上看,它没有效果。我知道关闭代码块覆盖范围的唯一方法是# pragma: no cover——有没有办法将其转移到业务逻辑中

def deco(fn):
    return fn  # pragma: no cover

@deco
def uncover_me(x):
    if x > 1:
        return 3
    return 5


uncover_me(3)
uncover_me(0)

Tags: no代码内容returndef覆盖率装饰cover
1条回答
网友
1楼 · 发布于 2024-05-11 18:26:57

在.coveragerc文件中,可以指定将排除行的正则表达式(如Advanced exclusion中所述)

如果正则表达式与引入代码子句的行匹配,则整个子句被排除。排除装饰器将排除整个函数

这应该适合您:

# in .coveragerc
[report]
exclude_lines =
    @deco
    pragma: no cover

从文档中:

Note that when using the exclude_lines option in a configuration file, you are taking control of the entire list of regexes, so you need to re-specify the default “pragma: no cover” match if you still want it to apply.

相关问题 更多 >