You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Pytest+GitHub Actions时Coveralls覆盖率徽章显示0%及工具报错求助

问题排查与解决方案

一、解决coveragepy-lcov导入错误

问题原因

你安装的coveragepy-lcov版本(0.1.2)是2021年的旧版本,仅兼容coverage 5.x~6.x;而CI环境中自动安装了最新的coverage 7.1.0,该版本已移除FnmatchMatcher类,导致导入失败。

修复方案(二选一,推荐方案2)

  1. 降级coverage到兼容版本
    在安装依赖的步骤中,指定coverage版本上限:

    pip install flake8 pytest pytest-cov coverage<7 coveragepy-lcov
    
  2. 使用coverage官方命令生成lcov文件(更可靠)
    放弃第三方工具coveragepy-lcov,直接用coverage自带的lcov命令生成报告,无需额外安装工具:

    • 移除pip install coveragepy-lcov
    • 将「Convert Coverage Results」步骤替换为:
      coverage lcov -o lcov.info
      

二、解决Coveralls显示0%覆盖率问题

问题原因

你运行pytest时仅用--cov参数,未指定要覆盖的源文件目录,导致生成的.coverage文件可能未正确关联项目源代码路径,Coveralls无法匹配到对应文件,最终显示0%覆盖率。

修复方案

运行pytest时明确指定源文件目录(假设你的业务代码在Source目录下):

pytest Source/tests -v --cov=Source

修改后的完整main.yml示例

on: [push, pull_request]

jobs:
  build:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        python-version: ['3.10']
    timeout-minutes: 5

    steps:
      - uses: actions/checkout@v2

      - name: Set up Python ${{ matrix.python-version }}
        uses: actions/setup-python@v2
        with:
          python-version: ${{ matrix.python-version }}

      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install -r requirements.txt
          pip install flake8 pytest pytest-cov coverage

      - name: Lint with flake8
        run: flake8 --statistics

      - name: Run unit tests with pytest and code coverage
        run: pytest Source/tests -v --cov=Source

      - name: Generate LCOV coverage report
        run: coverage lcov -o lcov.info

      - name: Upload Coverage Results
        uses: coverallsapp/github-action@master
        with:
          github-token: ${{ secrets.github_token }}
          path-to-lcov: lcov.info

内容的提问来源于stack exchange,提问作者Nick_2440

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 07:15:26