简体   繁体   English

如何从 pytest-cov 获取每个子包的聚合覆盖率?

[英]How to obtain aggregated coverage per subpackage from pytest-cov?

I'm using pytest in combination with the pytest-cov plugin on a large code base.我在大型代码库上将 pytest 与pytest-cov插件结合使用。 In general I'm aware of the various output report generators and the html output offers great value when analyzing coverage information on a low-level, ie, when looking into individual files and their particular line coverage.总的来说,我知道各种 output 报告生成器和 html output 在分析单个文件的覆盖率信息时提供了很大的价值,即在查看单个文件时,尤其是它们的行覆盖率时。

I'm also aware of the "grand total" coverage that pytest reports, which is of course aggregated over the entire project.我还知道 pytest 报告的“总”覆盖率,这当然是在整个项目中汇总的。

When I haven't figured out though is how to obtain coverage information in between the grand total and file level.当我还没有弄清楚如何在总计和文件级别之间获取覆盖信息时。 Let's say the top level package name is foo and it contains thousands of files structured into various (sometimes deeply nested) sub-packages.假设顶级 package 名称是foo ,它包含数千个文件,这些文件被结构化为各种(有时是深度嵌套的)子包。 During development of a certain subpackage say foo.bar.baz (which may be ~hundred files large itself), it would be very useful if I could obtain a coverage specifically for foo.bar.baz .在开发某个子包时说foo.bar.baz (它本身可能有大约数百个文件),如果我能获得专门针对foo.bar.baz的覆盖范围,那将非常有用。 Unfortunately, the "file level" reporting doesn't help in that case, because mentally aggregating all the files in foo.bar.baz is almost impossible.不幸的是,“文件级别”报告在这种情况下无济于事,因为在精神上汇总foo.bar.baz中的所有文件几乎是不可能的。

Is there an easy way to obtain the coverage on a per sub-package basis?有没有一种简单的方法来获得每个子包的覆盖范围?

You can get the data in JSON form (using coverage json ), and then post-process it yourself.您可以获取 JSON 形式的数据(使用coverage json ),然后自己进行后处理。 coverage.py doesn't have a way to do it. coverage.py 没有办法做到这一点。

An example of doing this is goals.py which offers limits and checks on subsets of your files.这样做的一个例子是goals.py ,它提供了对文件子集的限制和检查。 Maybe it can help you.也许它可以帮助你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM