Skip to content

Commit

Permalink
docs: clarify the behavior of exclude_lines
Browse files Browse the repository at this point in the history
  • Loading branch information
nedbat committed Jul 25, 2021
1 parent 3232c60 commit c6d9eb9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
14 changes: 13 additions & 1 deletion doc/config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ Here's a sample configuration file::
if 0:
if __name__ == .__main__.:

# Don't complain about abstract methods, they aren't run:
@(abc\.)?abstractmethod

ignore_errors = True

[html]
Expand Down Expand Up @@ -267,11 +270,20 @@ Values common to many kinds of reporting.
.. _config_report_exclude_lines:

``exclude_lines`` (multi-string): a list of regular expressions. Any line of
your source code that matches one of these regexes is excluded from being
your source code containing a match for one of these regexes is excluded from
being
reported as missing. More details are in :ref:`excluding`. If you use this
option, you are replacing all the exclude regexes, so you'll need to also
supply the "pragma: no cover" regex if you still want to use it.

You can exclude lines introducing blocks, and the entire block is excluded. If
you exclude a ``def`` line or decorator line, the entire function is excluded.

Be careful when writing this setting: the values are regular expressions that
only have to match a portion of the line. For example, if you write ``...``,
you'll exclude any line with three or more of any character. If you write
``pass``, you'll also exclude the line ``my_pass="foo"``, and so on.

.. _config_report_fail_under:

``fail_under`` (float): a target coverage percentage. If the total coverage
Expand Down
11 changes: 10 additions & 1 deletion doc/excluding.rst
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,17 @@ expressions. Using :ref:`configuration files <config>` or the coverage
often-used constructs to exclude that can be matched with a regex. You can
exclude them all at once without littering your code with exclusion pragmas.

If the matched line introduces a block, the entire block is excluded from
reporting. Matching a ``def`` line or decorator line will exclude an entire
function.

For example, you might decide that __repr__ functions are usually only used in
debugging code, and are uninteresting to test themselves. You could exclude
all of them by adding a regex to the exclusion list::

[report]
exclude_lines = def __repr__
exclude_lines =
def __repr__

For example, here's a list of exclusions I've used::

Expand All @@ -87,11 +92,15 @@ For example, here's a list of exclusions I've used::
if 0:
if __name__ == .__main__.:
class .*\bProtocol\):
@(abc\.)?abstractmethod

Note that when using the ``exclude_lines`` option in a configuration file, you
are taking control of the entire list of regexes, so you need to re-specify the
default "pragma: no cover" match if you still want it to apply.

The regexes only have to match part of a line. Be careful not to over-match. A
value of ``...`` will match any line with more than three characters in it.

A similar pragma, "no branch", can be used to tailor branch coverage
measurement. See :ref:`branch` for details.

Expand Down

0 comments on commit c6d9eb9

Please sign in to comment.