具有Python覆盖范围的Gitlab CI中的覆盖范围徽章始终未知

时间:2019-05-14 16:52:31

标签: python code-coverage gitlab-ci badge coverage.py

我正在尝试使用专用于Python的badge来在私有Gitlab CE安装(v11.8.6)中显示针对Python项目的报道coverage.py。但是,徽章始终显示unknown

这是我的.gitlab-ci.yaml文件中的相关工作:

coverage:
    stage: test
    before_script:
        - pip3.6 install coverage
        - mkdir -p public
    script:
        - coverage run --source=tk_skills_service setup.py test
        - coverage report | tee public/coverage.txt
    artifacts:
        paths:
            - public/coverage.txt
    coverage: '/TOTAL\s+\d+\s+\d+\s+(\d+%)/'

我希望徽章在此URL上显示实际的覆盖范围,所以这是我在项目设置中输入的General / Badges下的内容:

http://<privategitlaburl>/%{project_path}/badges/%{default_branch}/coverage.svg?job=coverage

我使用Gitlab页面阅读了these instructions。但是,我不想仅出于此目的使用页面,而我正在处理一个Python项目。

根据CI / CD设置和this post中的示例,coverage entry中的正则表达式应该起作用。我可以通过在本地尝试来确认:

$ grep -P "TOTAL\s+\d+\s+\d+\s+(\d+%)" public/coverage.txt
TOTAL                                           289     53    82%

我还在Test coverage parsing / CI/CD下的项目设置中的字段Pipeline settings中尝试了相同的正则表达式,但是同一页面上显示的标志仍显示unknown

我对文档不是很清楚,因为它没有描述整个过程。 how to use a badge创建后很明显,并且有手册可以在页面上发布覆盖率报告,但是从提取分数到显示徽章似乎没有明确的路径。

我应该在coverage文件中使用.gitlab-ci.yaml条目还是在管道设置中填写正则表达式?

无论哪种方式,Gitlab CI都应该基于此来更新coverage徽章,还是我需要使用诸如coverage-badge之类的其他工具来这样做?

提取的覆盖率分数应报告在哪里;如何确定我的正则表达式是否有效?

3 个答案:

答案 0 :(得分:1)

我终于为我的python项目显示了覆盖率徽章,而不是今天的百分比。这是我的.gitlab-ci.yml中的相关内容:

public class MainActivity extends FlutterActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //https://github.com/flutter/flutter/issues/64001 
    Window window = getWindow();
    window.setStatusBarColor(0x00000000);
    window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);

}

我还使用正则表达式gpov中列出的正则表达式,在回购CI / CD设置>常规管道>测试覆盖率解析中,我在阅读this以及对{{3}的倒数第二条评论后发现}:

job:
 script:
  - 'python -m venv venv'
  - '.\venv\Scripts\activate'
  - 'python -m pip install -r requirements.txt'
  - 'coverage run --source=python_project -m unittest discover ./tests'
  - 'coverage report --omit=things_that_arent_mine/*'
  - 'coverage xml'
 artifacts:
  reports:
   cobertura: 'coverage.xml'

在回购常规设置>徽章中,我的徽章链接为:

^TOTAL.*\s+(\d+\%)$

我的徽章图片网址是:

http://gitlab-server/%{project_path}/-/jobs

我不太了解Cobertura报告工件的用途(我认为它专门与this有关),但是我在那里,因为教程将我带到了那条路。我确实确认从.gitlab-ci.yml中删除以下内容不会破坏工作页面上的标志或覆盖范围编号:

http://gitlab-server/%{project_path}/badges/%{default_branch}/coverage.svg

在删除或评论时:

  - 'coverage xml'
 artifacts:
  reports:
   cobertura: 'coverage.xml'

会破坏证件以及显示在仓库的CI / CD作业页面上的覆盖范围编号。我还尝试了一些在merge requests中测试过的regex变体,但唯一不会导致gitlab响起的是gcovr。

希望这会有所帮助,将这个徽章用于python项目所需的东西拼凑起来既困难又艰巨。

编辑: 还只是想出了如何在覆盖率百分比数字上添加一些性感的精度。如果您将.gitlab-ci.yml的覆盖率报告行更改为:

- 'coverage report --omit=things_that_arent_mine/*'

CI / CD设置>常规管道>测试覆盖率中的正则表达式解析为:

- 'coverage report --omit=things_that_arent_mine/* --precision=2'

这应该为您提供一个非常准确的承保范围,实际上只有您我会关心。但是天哪,我们将确定覆盖率是99.99%还是100%。

答案 1 :(得分:0)

我也深入研究了这一点。正如您所说:命令 coverage report 产生类似于以下的输出:

[...]
tests/__init__.py              0      0   100%
tests/test_ml_squarer.py       4      0   100%
tests/test_squarer.py          4      0   100%
----------------------------------------------
TOTAL                         17      2    88%
test_service run-test: commands[4] | coverage xml

并且根据保存的 regex 表达式,它只是查找 88% 旁边的 TOTAL。我使用了 pytest-cov (Python) 的推荐,即 ^TOTAL.+?(\d+\%)$

regex expression

所以目前运行 coverage xml 对我来说似乎是可选的。但是,它不适合我使用:GitLab Community Edition 12.10.11

答案 2 :(得分:0)

在我上面的问题上花了三天时间,所以我想我会发布我的工作配置。我的项目是一个pyscaffolding项目,使用tox,当你向分支推送提交时触发管道,并将pip包推送到github包库。

  1. 徽章链接是:http://gitlab.XXXX.com/XXmeXX/python-template/-/commits/develop

  2. 徽章图片是:http://gitlab.XXXX.com/XXmeXX/python-template/badges/develop/coverage.svg

  3. 正则表达式与上述相同。

  4. PYPIRC 是一个环境变量,它看起来像一个 .pypirc 文件并指向我的内部 pip 注册表。

  5. I used this tutorial

  6. updated -> --cov-report xml 在我的 setup.cfg 中,我很确定 b/c 我不需要覆盖命令,但我没有测试过,因为我写了这篇文章。下次我进去时会检查一下。

我的 gitlab-ci.yml:

build-package:
  stage: deploy
  image: python:3.7
  script:
   - set
   - cat $PYPIRC > /tmp/.pypirc
   - pip3 install twine setuptools setuptools_scm wheel tox coverage
   # build the pip package
   - python3 setup.py bdist_wheel
   # $CI_COMMIT_TAG only works with the tagging pipeline, if you want to test a branch push directly, pull from fs
   - VERSION=$(python setup.py --version)
   # You can issue ls -al commands if you want, like to see variables or your published packages
   # - echo $VERSION
   # - ls -al ./dist |grep whl
   - tox
   - coverage xml -o coverage.xml
   # If you want to put artifacts up for storage, ...
   # - mkdir public
   - python3 -m twine upload --repository pythontemplate ./dist/my_python_template-${VERSION}-py2.py3-none-any.whl --config-file /tmp/.pypirc
 artifacts:
   reports:
     cobertura : 'coverage.xml'
   when: always
#   If you were copying artifacts up for later.
#  paths:
#     - public
 only:
  - branches

我学到的最重要的事情是,首先您将其显示为作业列表中的“Coverage”列,然后您就知道您正在正确解析所有内容并且正则表达式正在工作。从那里您可以处理覆盖范围 xml 和徽章链接。

相关问题