Sonarqube 客户端无法解析 pytest 覆盖率结果

问题描述 投票:0回答:5

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

我收到的错误如下:

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 但仍然没有成功。

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

python sonarqube pytest gitlab-ci gitlab-ci-runner
5个回答
3
投票

您需要帮助 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


3
投票

我在 GitHub Actions 中遇到了同样的问题,可以通过在

relative_files = True
中指定
.coveragerc
来解决。

[coverage:run]
relative_files = True
source = my_project/
branch = True

这也记录在 SonarCloud 的 docs

请注意,我们在 tox.ini 文件中指定relative_files = True,以确保 GitHub Actions 能够正确解析您的覆盖率结果。

希望这有帮助!


0
投票

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


0
投票

我按照 Adrian 的说明解决了这个问题 https://community.sonarsource.com/t/python-coverage-analysis-warning/62629/7

好像使用

source
有问题,使用
include
时有效 相反。


0
投票

我在使用 unittestcoverage 的 python 项目中遇到了同样的问题,并从

GitLab CI
GitHub Actions
执行与 SonarQube 的集成。

花了几个小时尝试了这里分享的之前的每一个答案后,我的案例的解决方案非常简单:

  • 将执行SonarQube扫描仪的工作目录设置为测试覆盖率报告中计算相对路径的子目录。

示例:

就我而言,我的存储库布局类似于:

a-file
another-file
project-1/
    app/
    tests/
some-other-dir/
some-other-file
...

鉴于我希望扫描器收集

project-1
中代码的测试覆盖率结果,那么应将
project-1
设置为工作目录。

示例解决方案,使用 GitHub actions + sonarqube-scan-action 并设置

projectBaseDir
:

  test:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v3
        with:
          # Disable shallow clones, as recommended by SonarQube for improved analysis and reports
          fetch-depth: 0

      - name: Run tests
        run: # command to run tests and generate coverage report

      - name: SonarQube Scan
        uses: sonarsource/sonarqube-scan-action@master
        with:
          # projectBaseDir: path/to/my/sub_dir/with/app_and_tests_folders
          projectBaseDir: project-1
        env:
          SONAR_TOKEN: ${{ secrets.SONARQUBE_TOKEN }}
          SONAR_HOST_URL: ${{ secrets.SONARQUBE_URL }}

在 GitLab CI 中,我使用 Docker 镜像 sonar-scanner-cli-docker 实现了相同的效果,并使用我的项目子文件夹的路径设置镜像环境变量

SRC_PATH

© www.soinside.com 2019 - 2024. All rights reserved.