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

如何为CI与本地构建配置不同的pytest代码覆盖率规则?

Custom Coverage Exclusion for Local vs CI Builds

Great question! You're on the right track with using custom markers for local vs CI coverage exclusion—here's how to make this work with coverage.py's configuration and environment variables, so you can use a single .coveragerc file that adapts to your build environment.

Step 1: Keep Your Custom Markers in Code

First, leave your existing markers as-is in the code—they're correctly targeted for each environment:

  • Lines 4-5 (the no-device check) have # pragma: no cover for local builds (where this branch won't run)
  • Line 7 (the else block start) has # ci: no cover for CI builds (where this branch won't run)

Your code remains unchanged:

import pyaudio
def play_audio(audio):
    """Play audio array to the default audio device"""
    if pyaudio.PyAudio().get_host_api_info_by_index(0).get('deviceCount') == 0 : # pragma: no cover
        logging.error(NO_DEVICE_ERROR) 
    else : # ci: no cover        
        stream = pyaudio.PyAudio().open(
            format=pyaudio.paFloat32, channels=2, rate=44100, output=True
        )
        stream.write(audio).tobytes()

Step 2: Configure .coveragerc with Environment Variables

Update your .coveragerc to use a dynamic environment variable that specifies which marker to exclude. This lets you switch between local and CI behavior without modifying the config file itself:

[run]
# Replace with your project's source directory
source = .
# Optional: Omit test files or other non-source code
omit =
    */tests/*
    */__pycache__/*

[report]
show_missing = True
exclude_lines =
    # Add universal exclusion rules here (optional)
    def __repr__
    if __name__ == "__main__":
    # Dynamic exclusion marker controlled by environment variable
    ${COVERAGE_EXCLUDE_MARKER}

Step 3: Run Coverage with Environment-Specific Variables

Now you just need to set the COVERAGE_EXCLUDE_MARKER environment variable before running coverage, depending on your build environment:

Local Build (with audio device)

Run this command to exclude lines marked with pragma: no cover:

# Linux/macOS
export COVERAGE_EXCLUDE_MARKER="pragma: no cover"
coverage run -m pytest  # Replace with your test runner command
coverage report

# Windows (PowerShell)
$env:COVERAGE_EXCLUDE_MARKER = "pragma: no cover"
coverage run -m pytest
coverage report

CI Build (no audio device)

In your CI pipeline (GitHub Actions, GitLab CI, etc.), set the variable to exclude lines marked with ci: no cover:

export COVERAGE_EXCLUDE_MARKER="ci: no cover"
coverage run -m pytest
coverage report

Alternative: Command-Line Override

If you prefer not to modify .coveragerc, you can pass the exclusion rule directly via the command line instead:

  • Local: coverage run --exclude-lines="pragma: no cover" -m pytest
  • CI: coverage run --exclude-lines="ci: no cover" -m pytest

This works because coverage.py's --exclude-lines parameter adds to (or overrides) the rules in your config file.


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.04 17:50:22