Linux command
coverage 命令
网络
复制后可按需替换文件名、目录或参数。
常用示例
Run a Python script with coverage
coverage run [script.py]
Run pytest with coverage
coverage run -m pytest
Show coverage report in terminal
coverage report
Generate HTML coverage report
coverage html
Fail if total coverage is below 80%
coverage report --fail-under=80
Combine coverage from multiple runs
coverage combine
Erase collected coverage data
coverage erase
Show coverage for specific file
coverage report --include=[path/to/file.py]
Run with branch coverage
coverage run --branch -m pytest
Generate XML report
coverage xml
说明
Coverage.py measures code coverage for Python programs, showing which lines and branches are executed during testing. It helps identify untested code paths and ensures comprehensive test suites. The tool instruments Python bytecode to track execution. After running tests with coverage run, reports show percentage of lines covered per file. HTML reports provide visual highlighting of covered and uncovered lines. Branch coverage (--branch) additionally tracks which conditional branches are taken, catching cases where both sides of an if statement aren't tested.
参数
- run _options_ _program_
- Run a program and collect coverage data.
- report
- Display coverage report in terminal.
- html
- Generate HTML report in htmlcov/.
- xml
- Generate Cobertura XML report.
- json
- Generate JSON report.
- combine
- Combine data from multiple coverage files.
- erase
- Delete collected coverage data.
- annotate
- Annotate source files with coverage markers.
- lcov
- Generate LCOV report.
- -m _module_
- Run library module as script (like python -m).
- --source _paths_
- Limit coverage to specified packages/directories.
- --include _patterns_
- Include only files matching patterns.
- --omit _patterns_
- Omit files matching patterns.
- --branch
- Enable branch coverage measurement.
- --fail-under _n_
- Exit with failure if coverage is below n%.
FAQ
What is the coverage command used for?
Coverage.py measures code coverage for Python programs, showing which lines and branches are executed during testing. It helps identify untested code paths and ensures comprehensive test suites. The tool instruments Python bytecode to track execution. After running tests with coverage run, reports show percentage of lines covered per file. HTML reports provide visual highlighting of covered and uncovered lines. Branch coverage (--branch) additionally tracks which conditional branches are taken, catching cases where both sides of an if statement aren't tested.
How do I run a basic coverage example?
Run `coverage run [script.py]` in a terminal, then adjust file names, paths, flags, or remote targets for your system.
What does run _options_ _program_ do in coverage?
Run a program and collect coverage data.