【问题标题】:Sonarqube client fails to parse pytest coverage resultsSonarqube 客户端无法解析 pytest 覆盖结果
【发布时间】:2019-07-26 12:33:17
【问题描述】:

我正在尝试为 python 项目设置 gitlab ci 管道,但我遇到了一些无法解析 coverage.xml 文件的 sonarqube 客户端问题

我得到的错误如下:

INFO: Python test coverage
INFO: Parsing report '/builds/core-tech/tools/nlu/mix-nlu-middleware/server/tests/cov.xml'
WARN: Invalid directory path in 'source' element: /bolt-webserver/bolt
WARN: Invalid directory path in 'source' element: /bolt-webserver/tests
ERROR: Cannot resolve the file path 'base.py' of the coverage report, the file does not exist in all <source>.
ERROR: Cannot resolve 404 file paths, ignoring coverage measures for those files
INFO: Sensor Cobertura Sensor for Python coverage [python] (done) | time=74ms
INFO: Sensor PythonXUnitSensor [python]
INFO: Sensor PythonXUnitSensor [python] (done) | time=20ms
INFO: Sensor JaCoCo XML Report Importer [jacoco]

覆盖文件 (cov.xml) 以此开头:

<?xml version="1.0" ?>
<coverage branch-rate="0" branches-covered="0" branches-valid="0" complexity="0" line-rate="0.3476" lines-covered="10369" lines-valid="29833" timestamp="1564079534753" version="4.4.2">
    <!-- Generated by coverage.py: https://coverage.readthedocs.io -->
    <!-- Based on https://raw.githubusercontent.com/cobertura/web/master/htdocs/xml/coverage-04.dtd -->
    <sources>
        <source>/bolt-webserver/bolt</source>
        <source>/bolt-webserver/tests</source>
    </sources>
    <packages>
        <package branch-rate="0" complexity="0" line-rate="0.55" name=".">
            <classes>
                <class branch-rate="0" complexity="0" filename="base.py" line-rate="0.5955" name="base.py">
                    <methods/>
                    <lines>
                        <line hits="1" number="1"/>
                        <line hits="1" number="2"/>
                        <line hits="1" number="3"/>
                        <line hits="1" number="4"/>
                        <line hits="1" number="5"/>
                        <line hits="1" number="7"/>
                        <line hits="1" number="9"/>
                        <line hits="1" number="10"/>
  .......................

声纳是这样调用的:

- sonar-scanner -Dsonar.projectKey=mix-nlu-middleware -Dsonar.sources=./server -Dsonar.host.url=$SONAR_SERVER_HOST -Dsonar.login=$SONAR_LOGIN -Dsonar.python.coverage.reportPaths=server/tests/cov.xml -Dsonar.junit.reportPaths=server/tests/junit-report.xml

项目树如下所示:

.
+-- CONTRIBUTING.md
+-- gen_version.sh
+-- package-lock.json
+-- README.md
+-- scripts
│   +-- .....
+-- server
│   +-- alembic.ini
│   +-- bolt
│   │   +-- .....
│   +-- Bolt.egg-info
│   │   +-- .....
│   +-- conf
│   │   +-- .....
│   +-- dev-requirements.txt
│   +-- Dockerfile
│   +-- Dockerfile-dev
│   +-- http.log
│   +-- MANIFEST.in
│   +-- pytest.ini
│   +-- requirements.txt
│   +-- scripts
│   │   +-- .....
│   +-- sdks
│   │   +-- ....
│   +-- server.log
│   +-- setup.py
│   +-- templates
│   │   +-- .....
│   +-- tests
│   │   +-- .....
│   \-- version.properties
\-- test.txt

知道我在这里做错了什么吗?

我也尝试在项目和文件系统的根文件夹中创建路径 /bolt-webserver/bolt 但仍然没有成功。

conv.xml 中提到的“base.py”文件和其他文件位于“/builds/core-tech/tools/nlu/mix-nlu-middleware/server/tests”下

【问题讨论】:

标签: python sonarqube pytest gitlab-ci gitlab-ci-runner


【解决方案1】:

您需要帮助 sonarqube 找到正确的文件,一个简单的方法是在运行测试并生成报告之后以及调用 souncarcloud scan 之前在您的 GH 操作中包含以下步骤

--> step that run tests

      - name: fix code coverage paths
        run: |
          sed -i 's/\/home\/runner\/work\/<your-repo>\/<your-repo>\//\/github\/workspace\//g' cover/coverage.xml

--> step that call sonar qube report

(我想你的仓库是bolt-webserver

【讨论】:

    【解决方案2】:

    好的,问题似乎来自 cov.xml 内容。似乎 sonarqube 无法仅根据名称找到测试文件:filename="base.py" 为了解决这个问题,我必须更新 cov.xml 以便文件名字段包含完整的文件路径。文件名="/base.py"

    【讨论】:

    • 您是手动执行此操作还是在 pytest 运行中执行此操作?
    • 我必须手动完成。好吧,我写了一个脚本来为我做这件事
    • 介意分享脚本吗? ;)
    • 撞!您如何仅从文件名中找到文件的路径?我也想看看那个脚本。
    • ...然后又撞了! @gheorghievgheniev 我们需要知道:) 目前有同样的问题
    猜你喜欢
    • 2015-08-08
    • 2021-09-17
    • 2020-03-04
    • 1970-01-01
    • 1970-01-01
    • 2014-03-27
    • 2020-04-29
    • 2017-12-25
    • 1970-01-01
    相关资源
    最近更新 更多