Python覆盖徽章,如何获取它们?

时间:2020-12-22 16:56:22

I am using Python coverage to test my apps. Looking at other developers on GitHub I see they have a small badge which shows the percentage of coverage. Using coverage, how can I generate these badges?

我正在使用Python覆盖来测试我的应用程序。看看GitHub上的其他开发者,我看到他们有一个小徽章,显示了覆盖率。使用覆盖范围,我如何生成这些徽章?

The coverage badge is the one I'm looking at below.

我正在看下面的覆盖徽章。

Python覆盖徽章,如何获取它们?

Update: There are packages that generate badges i.e. nose-htmloutput! Cool

更新:有包生成徽章,即nose-htmloutput!凉

5 个解决方案

#1


4  

If you want to generate badges on your own, you could try to load the total coverage percentage and then create an image, someting like this:

如果您想自己生成徽章,可以尝试加载总覆盖率百分比,然后创建一个图像,如下所示:

from PIL import Image, ImageDraw, ImageFont
from coverage import coverage

cov = coverage()
cov.load()
total = cov.report()

# total = 79.0

im = Image.new("RGB", (120, 20))
fnt = ImageFont.load_default()
d = ImageDraw.Draw(im)

d.text((10, 5), "coverage:", fill=(255, 255, 255), font=fnt)
d.rectangle([(80, 0), (150, 20)], fill=(220, 0, 0))
d.text((90, 5), "{:.0f}%".format(total), fill=(0, 0, 0), font=fnt)

Python覆盖徽章,如何获取它们?

#2


7  

You can click on those badges and it'll generally take you to the service that provides them.

您可以单击这些徽章,它通常会将您带到提供它们的服务。

The coverage badge is provided by https://coveralls.io/:

保险范围徽章由https://coveralls.io/提供:

Coveralls is a web service to help you track your code coverage over time, and ensure that all your new code is fully covered.

工作服是一种Web服务,可帮助您跟踪代码覆盖率,并确保完全覆盖所有新代码。

There is but one prerequisite:

只有一个先决条件:

  • Your code must be hosted on GitHub
  • 您的代码必须托管在GitHub上

Once you have signed up and included the required configuration and integrations or packages when developing, you are given a image URL to include in your project documentation; the python-coveralls project has:

在开发后注册并包含所需的配置和集成或软件包后,您将获得一个图像URL以包含在项目文档中; python-coveralls项目有:

.. image:: https://coveralls.io/repos/z4r/python-coveralls/badge.png?branch=master
    :target: https://coveralls.io/r/z4r/python-coveralls

in their README for example, which renders as:

例如,在他们的自述文件中,它呈现为:

Python覆盖徽章,如何获取它们?

#3


1  

Based on the answer by Carsten, there is now a MIT licensed tool on PyPI for generating SVG coverage badges:

基于Carsten的答案,现在有一个关于PyPI的MIT许可工具用于生成SVG覆盖徽章:

https://github.com/dbrgn/coverage-badge
https://pypi.python.org/pypi/coverage-badge

https://github.com/dbrgn/coverage-badge https://pypi.python.org/pypi/coverage-badge

#4


1  

You can use Badge which is hosted at http://badge.kloud51.com

您可以使用在http://badge.kloud51.com上托管的徽章

The source code is available at Github: https://github.com/SavandBros/badge you can look at the code and see how it's been generated if you'd like to learn about it.

源代码可以在Github上找到:https://github.com/SavandBros/badge你可以查看代码,看看它是如何生成的,如果你想了解它。

#5


1  

I have written a python badge generation package that produces badges very visually similar to the main badge services. It is highly flexible, you can import and use in your python code, or run from the command line. It is simple, and self-contained.

我编写了一个python徽章生成包,可以在视觉上与主徽章服务类似地生成徽章。它非常灵活,您可以在您的python代码中导入和使用,或从命令行运行。它简单,独立。

You can set the badge label and value, and you can set the color based on thresholds. There are pre-built settings for pylint, coverage, and pipeline success, but you can create any badge you like.

您可以设置徽章标签和值,也可以根据阈值设置颜色。有针对pylint,coverage和管道成功的预建设置,但您可以创建自己喜欢的任何徽章。

Here is a link to the github project with more detailed documentation: https://github.com/jongracecox/anybadge

以下是github项目的链接,其中包含更详细的文档:https://github.com/jongracecox/anybadge

Install with pip install anybadge

使用pip install anybadge安装

Example python code:

示例python代码:

import anybadge

# Define thresholds: <2=red, <4=orange <8=yellow <10=green
thresholds = {2: 'red',
              4: 'orange',
              6: 'yellow',
              10: 'green'}

badge = anybadge.Badge('pylint', 2.22, thresholds=thresholds)

badge.write_badge('pylint.svg')

Example command line use:

命令行使用示例:

anybadge --label pylint --value 2.22 --file pylint.svg 2=red 4=orange 8=yellow 10=green

#1


4  

If you want to generate badges on your own, you could try to load the total coverage percentage and then create an image, someting like this:

如果您想自己生成徽章,可以尝试加载总覆盖率百分比,然后创建一个图像,如下所示:

from PIL import Image, ImageDraw, ImageFont
from coverage import coverage

cov = coverage()
cov.load()
total = cov.report()

# total = 79.0

im = Image.new("RGB", (120, 20))
fnt = ImageFont.load_default()
d = ImageDraw.Draw(im)

d.text((10, 5), "coverage:", fill=(255, 255, 255), font=fnt)
d.rectangle([(80, 0), (150, 20)], fill=(220, 0, 0))
d.text((90, 5), "{:.0f}%".format(total), fill=(0, 0, 0), font=fnt)

Python覆盖徽章,如何获取它们?

#2


7  

You can click on those badges and it'll generally take you to the service that provides them.

您可以单击这些徽章,它通常会将您带到提供它们的服务。

The coverage badge is provided by https://coveralls.io/:

保险范围徽章由https://coveralls.io/提供:

Coveralls is a web service to help you track your code coverage over time, and ensure that all your new code is fully covered.

工作服是一种Web服务,可帮助您跟踪代码覆盖率,并确保完全覆盖所有新代码。

There is but one prerequisite:

只有一个先决条件:

  • Your code must be hosted on GitHub
  • 您的代码必须托管在GitHub上

Once you have signed up and included the required configuration and integrations or packages when developing, you are given a image URL to include in your project documentation; the python-coveralls project has:

在开发后注册并包含所需的配置和集成或软件包后,您将获得一个图像URL以包含在项目文档中; python-coveralls项目有:

.. image:: https://coveralls.io/repos/z4r/python-coveralls/badge.png?branch=master
    :target: https://coveralls.io/r/z4r/python-coveralls

in their README for example, which renders as:

例如,在他们的自述文件中,它呈现为:

Python覆盖徽章,如何获取它们?

#3


1  

Based on the answer by Carsten, there is now a MIT licensed tool on PyPI for generating SVG coverage badges:

基于Carsten的答案,现在有一个关于PyPI的MIT许可工具用于生成SVG覆盖徽章:

https://github.com/dbrgn/coverage-badge
https://pypi.python.org/pypi/coverage-badge

https://github.com/dbrgn/coverage-badge https://pypi.python.org/pypi/coverage-badge

#4


1  

You can use Badge which is hosted at http://badge.kloud51.com

您可以使用在http://badge.kloud51.com上托管的徽章

The source code is available at Github: https://github.com/SavandBros/badge you can look at the code and see how it's been generated if you'd like to learn about it.

源代码可以在Github上找到:https://github.com/SavandBros/badge你可以查看代码,看看它是如何生成的,如果你想了解它。

#5


1  

I have written a python badge generation package that produces badges very visually similar to the main badge services. It is highly flexible, you can import and use in your python code, or run from the command line. It is simple, and self-contained.

我编写了一个python徽章生成包,可以在视觉上与主徽章服务类似地生成徽章。它非常灵活,您可以在您的python代码中导入和使用,或从命令行运行。它简单,独立。

You can set the badge label and value, and you can set the color based on thresholds. There are pre-built settings for pylint, coverage, and pipeline success, but you can create any badge you like.

您可以设置徽章标签和值,也可以根据阈值设置颜色。有针对pylint,coverage和管道成功的预建设置,但您可以创建自己喜欢的任何徽章。

Here is a link to the github project with more detailed documentation: https://github.com/jongracecox/anybadge

以下是github项目的链接,其中包含更详细的文档:https://github.com/jongracecox/anybadge

Install with pip install anybadge

使用pip install anybadge安装

Example python code:

示例python代码:

import anybadge

# Define thresholds: <2=red, <4=orange <8=yellow <10=green
thresholds = {2: 'red',
              4: 'orange',
              6: 'yellow',
              10: 'green'}

badge = anybadge.Badge('pylint', 2.22, thresholds=thresholds)

badge.write_badge('pylint.svg')

Example command line use:

命令行使用示例:

anybadge --label pylint --value 2.22 --file pylint.svg 2=red 4=orange 8=yellow 10=green