Coverage Conditional Plugin

0.9.0 · active · verified Thu Apr 16

Coverage Conditional Plugin allows defining conditional coverage rules based on any Python expressions or environment variables. It helps to omit specific lines, functions, or entire modules from coverage reports based on custom logic. As of version 0.9.0, it officially supports Python 3.7+ and Coverage.py 7.x, enabling highly customizable control over what gets included in your coverage reports.

Common errors

Warnings

Install

Imports

Quickstart

To use the plugin, you must configure it within your `coverage.py` settings, typically in `pyproject.toml` or `.coveragerc`. Define `rules` as Python expressions that determine when lines or blocks of code should be omitted from coverage reports. These rules can reference `os.getenv`, `sys.version_info`, and other global Python context variables.

import os
import sys
from coverage_conditional_plugin.plugin import get_env_info

# --- Example of programmatic usage (less common, for debugging) ---
print("\n--- Plugin Environment Info ---")
print(get_env_info())
print("--------------------------------\n")

# --- Typical usage: configure via pyproject.toml or .coveragerc ---
# Create a `pyproject.toml` file in your project root with this content:
#
# [tool.coverage.run]
# plugins = ["coverage_conditional_plugin"]
#
# [tool.coverage.coverage_conditional_plugin]
# # Example rule 1: omit lines if Python version is below 3.8
# rules = [
#   "if sys.version_info < (3, 8): omit",
#   # Example rule 2: omit lines starting with 'def debug_feature' if env var is 'true'
#   "if os.getenv('SKIP_DEBUG_FEATURES', 'false') == 'true': exclude_lines_starting_with('def debug_feature')",
# ]
#
# Create a dummy Python file (e.g., `my_app.py`) to demonstrate:
# def standard_feature():
#     print("This is always covered.")
#
# def debug_feature():
#     print("This is a debug-only function.") # Will be excluded if SKIP_DEBUG_FEATURES is 'true'
#
# if sys.version_info >= (3, 8):
#     print("Running on Python 3.8 or newer.") # This line would be omitted if Python < 3.8
# else:
#     print("Running on Python older than 3.8.")
#
# standard_feature()
# debug_feature()

# --- To run this example from your terminal ---
# 1. Make sure you have `pyproject.toml` and `my_app.py` in your current directory.
# 2. Run coverage normally (all lines covered, assuming Python >= 3.8):
#    $ coverage run my_app.py
#    $ coverage report -m
#
# 3. Run coverage with an environment variable rule activated:
#    $ SKIP_DEBUG_FEATURES=true coverage run my_app.py
#    $ coverage report -m
#    (Expected: `debug_feature` lines are now marked as omitted/excluded)

# Note: The Python code in this block is for demonstration and `get_env_info()` execution.
# The primary interaction is via `coverage.py` configuration and shell commands.

view raw JSON →