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

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

我正在尝试为 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 的调用方式如下:

- 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”下

最佳答案

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

关于python - Sonarqube 客户端无法解析 pytest 覆盖率结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57220171/

相关文章:

javascript - 有哪些选项可用于定义具有 node.js 依赖项的 Python 包?

python循环导入错误

java - 使用 Jenkins 构建和 maven 项目进行 Sonar 分析时,无法通过 ClassLoader 警告访问类 'XXX/XXX/XXX '

python - 如何使用pytest模拟完全重启

python - 回滚事务不适用于 py.test 和 Flask

python Mechanize 没有正确解析表单

python - 我正在使用 py2neo 将 python 连接到 neo4j 数据库

java - HTTP 安全配置authorizeRequests() 出现 Sonar 严重缺陷

java - SonarQube 运行 main : java. lang.NoSuchMethodError : ch. qos.logback.classic.LoggerContext.initCollisionMaps()V 时出错

flask - 如何使用 pytest 测试 Flask View 上下文和模板?