Pythoncoverage.py 排除行

2024-03-13

背景

我有几个使用coverage.py 的Django 项目,并且一直在尝试向我的.coveragerc 配置文件的exclude_lines 部分添加一些额外的表达式。问题是,即使使用正确的正则表达式,也可以在测试器中拾取该行,例如http://www.pythonregex.com http://www.pythonregex.com or http://www.regexr.com http://www.regexr.com它不会导致报告中的行被忽略。

我已经审查了docs http://nedbatchelder.com/code/coverage/config.html并四处搜寻存储库 https://github.com/nedbat/coveragepy但无法找出任何原因来解释为什么我的配置可能不起作用。从文档看来,我的配置与他们所描述的完全一样。

我也尝试过使用姜戈鼻子 https://github.com/django-nose/django-nose版本 1.2,最后一个 PyPI 版本,这将允许异常注入 https://medium.com/@echohack/python-unit-testing-injecting-exceptions-with-mock-and-nose-e46bf579c867但无济于事,至少在版本 1.7 中,确定 Django 视图和 Django REST Framework API 端点的覆盖范围似乎存在一些问题。

我尝试过的

我的配置如下:

[run]
branch = True
omit =
    */tests*
    */migrations/*
    *__init__.py*
    */settings/*
    *wsgi.py*
    *admin.py*

[report]
# Regexes for lines to exclude from consideration
exclude_lines =
    pragma: no cover
    def __repr__
    if self.debug:
    raise AssertionError
    raise NotImplementedError
    (.*)except Exception as e:(.*)
    if 0:
    if __name__ == .__main__.:

我还尝试了以下组合来进行配置的报告部分中的异常处理:

(.*)except Exception as e:
except Exception as e:
except Exception as e:(.*)

下面是一个包含我希望忽略的代码部分的函数示例:

def my_func():
    try:
        # Some logic
        return True
    except Exception as e:
        return defensive_exception(my_func.__name__, e, False)

在上面的示例中,根据文档,我期望以下所有内容except Exception as e:被忽略或至少except Exception as e线本身。然而,情况似乎并非如此。如果有人对我的配置有什么问题或者我需要做哪些不同的事情有一些见解,我将非常感谢您的帮助。


我总是做的是使用pragma: no cover对于你已经拥有的这个exclude_lines.

def my_func():
    try:
        # Some logic
        return True
    except Exception as e:  # pragma: no cover
        return defensive_exception(my_func.__name__, e, False)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Pythoncoverage.py 排除行 的相关文章

随机推荐