Python coverage: Difference between revisions

From wikinotes
 
(3 intermediate revisions by the same user not shown)
Line 5: Line 5:
{| class="wikitable"
{| class="wikitable"
|-
|-
| official docs || https://coverage.readthedocs.io/en/6.2/
| official docs || https://coverage.readthedocs.io
|-
|-
|}
|}
Line 12: Line 12:
= Usage =
= Usage =
<blockquote>
<blockquote>
<source lang="bash">
 
write report to <code>.coverage</code>
<syntaxhighlight lang="bash">
# run coverage
# run coverage
coverage run -m pytest
coverage run \
coverage run \
     --source path/A \
     --source path/A \
     --source path/B \
     --source path/B \
     --omit path/C
     --omit path/C
</syntaxhighlight>


coverage reports
<syntaxhighlight lang="bash">
# combine coverage multiple reports into a single .coverage file
# combine coverage multiple reports into a single .coverage file
coverage combine .coverage1 .coverage2 ...
coverage combine .coverage1 .coverage2 ...
Line 26: Line 33:


# produce interactive HTML report (htmlcov/index.html)
# produce interactive HTML report (htmlcov/index.html)
# qutebrowser htmlcov/index.html
coverage html
coverage html
</source>
</syntaxhighlight>
 
</blockquote><!-- usage-->
</blockquote><!-- usage-->



Latest revision as of 01:44, 25 January 2022

Produce reports of which lines of code are not run by tests.

Documentation

official docs https://coverage.readthedocs.io

Usage

write report to .coverage

# run coverage
coverage run -m pytest

coverage run \
    --source path/A \
    --source path/B \
    --omit path/C

coverage reports

# combine coverage multiple reports into a single .coverage file
coverage combine .coverage1 .coverage2 ...

# produce XML report from a single .coverage file
coverage xml

# produce interactive HTML report (htmlcov/index.html)
# qutebrowser htmlcov/index.html
coverage html

Excluding Code

Append a comment to a line to exclude it. If that statement is a block of code, the entire block will be excluded.

if __name__ == '__main__':   # pragma: no cover
    # not included in coverage report...