summaryrefslogtreecommitdiff
path: root/doc/excluding.rst
diff options
context:
space:
mode:
Diffstat (limited to 'doc/excluding.rst')
-rw-r--r--doc/excluding.rst55
1 files changed, 41 insertions, 14 deletions
diff --git a/doc/excluding.rst b/doc/excluding.rst
index 0dfee514..55acebeb 100644
--- a/doc/excluding.rst
+++ b/doc/excluding.rst
@@ -5,6 +5,8 @@ Excluding code from coverage
============================
:history: 20090613T090500, brand new docs.
+:history: 20100224T200900, updated for 3.3.
+
You may have code in your project that you know won't be executed, and you want
to tell coverage to ignore it. For example, you may have debugging-only code
@@ -30,7 +32,7 @@ function is not reported as missing::
def __init__(self):
blah1()
blah2()
-
+
def __repr__(self): # pragma: no cover
return "<MyObject>"
@@ -39,28 +41,53 @@ coverage data as usual. When producing reports though, coverage excludes it from
the list of missing code.
+Branch coverage
+---------------
+
+When measuring :ref:`branch coverage <branch>`, a condtional will not be
+counted as a branch if one of its choices is excluded::
+
+ def only_one_choice(x):
+ if x:
+ blah1()
+ blah2()
+ else: # pragma: no cover
+ # x is always true.
+ blah3()
+
+Because the ``else`` clause is excluded, the ``if`` only has one possible
+next line, so it isn't considered a branch at all.
+
+
Advanced exclusion
------------------
Coverage identifies exclusions by matching lines against a list of regular
-expressions. Using the coverage :ref:`API <api>`, you can add to that list.
-This is useful if you have 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.
+expressions. Using :ref:`configuration files <config>` or the coverage
+:ref:`API <api>`, you can add to that list. This is useful if you have
+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.
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::
- coverage.exclude("def __repr__")
-
+ [report]
+ exclude_lines = def __repr__
+
Here's a list of exclusions I've used::
- coverage.exclude('def __repr__')
- coverage.exclude('if self.debug:')
- coverage.exclude('if settings.DEBUG')
- coverage.exclude('raise AssertionError')
- coverage.exclude('raise NotImplementedError')
- coverage.exclude('if 0:')
- coverage.exclude('if __name__ == .__main__.:')
+ [report]
+ exclude_lines =
+ pragma: no cover
+ def __repr__
+ if self.debug:
+ if settings.DEBUG
+ raise AssertionError
+ raise NotImplementedError
+ if 0:
+ if __name__ == .__main__.:
+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.