| OLD | NEW |
| (Empty) |
| 1 .. _excluding: | |
| 2 | |
| 3 ============================ | |
| 4 Excluding code from coverage | |
| 5 ============================ | |
| 6 | |
| 7 :history: 20090613T090500, brand new docs. | |
| 8 :history: 20100224T200900, updated for 3.3. | |
| 9 :history: 20100725T211700, updated for 3.4. | |
| 10 :history: 20110604T184400, updated for 3.5. | |
| 11 | |
| 12 | |
| 13 You may have code in your project that you know won't be executed, and you want | |
| 14 to tell coverage to ignore it. For example, you may have debugging-only code | |
| 15 that won't be executed during your unit tests. You can tell coverage to exclude | |
| 16 this code during reporting so that it doesn't clutter your reports with noise | |
| 17 about code that you don't need to hear about. | |
| 18 | |
| 19 Coverage will look for comments marking clauses for exclusion. In this code, | |
| 20 the "if debug" clause is excluded from reporting:: | |
| 21 | |
| 22 a = my_function1() | |
| 23 if debug: # pragma: no cover | |
| 24 msg = "blah blah" | |
| 25 log_message(msg, a) | |
| 26 b = my_function2() | |
| 27 | |
| 28 Any line with a comment of "pragma: no cover" is excluded. If that line | |
| 29 introduces a clause, for example, an if clause, or a function or class | |
| 30 definition, then the entire clause is also excluded. Here the __repr__ | |
| 31 function is not reported as missing:: | |
| 32 | |
| 33 class MyObject(object): | |
| 34 def __init__(self): | |
| 35 blah1() | |
| 36 blah2() | |
| 37 | |
| 38 def __repr__(self): # pragma: no cover | |
| 39 return "<MyObject>" | |
| 40 | |
| 41 Excluded code is executed as usual, and its execution is recorded in the | |
| 42 coverage data as usual. When producing reports though, coverage excludes it from | |
| 43 the list of missing code. | |
| 44 | |
| 45 | |
| 46 Branch coverage | |
| 47 --------------- | |
| 48 | |
| 49 When measuring :ref:`branch coverage <branch>`, a condtional will not be | |
| 50 counted as a branch if one of its choices is excluded:: | |
| 51 | |
| 52 def only_one_choice(x): | |
| 53 if x: | |
| 54 blah1() | |
| 55 blah2() | |
| 56 else: # pragma: no cover | |
| 57 # x is always true. | |
| 58 blah3() | |
| 59 | |
| 60 Because the ``else`` clause is excluded, the ``if`` only has one possible | |
| 61 next line, so it isn't considered a branch at all. | |
| 62 | |
| 63 | |
| 64 Advanced exclusion | |
| 65 ------------------ | |
| 66 | |
| 67 Coverage identifies exclusions by matching lines against a list of regular | |
| 68 expressions. Using :ref:`configuration files <config>` or the coverage | |
| 69 :ref:`API <api>`, you can add to that list. This is useful if you have | |
| 70 often-used constructs to exclude that can be matched with a regex. You can | |
| 71 exclude them all at once without littering your code with exclusion pragmas. | |
| 72 | |
| 73 For example, you might decide that __repr__ functions are usually only used | |
| 74 in debugging code, and are uninteresting to test themselves. You could exclude | |
| 75 all of them by adding a regex to the exclusion list:: | |
| 76 | |
| 77 [report] | |
| 78 exclude_lines = def __repr__ | |
| 79 | |
| 80 For example, here's a list of exclusions I've used:: | |
| 81 | |
| 82 [report] | |
| 83 exclude_lines = | |
| 84 pragma: no cover | |
| 85 def __repr__ | |
| 86 if self.debug: | |
| 87 if settings.DEBUG | |
| 88 raise AssertionError | |
| 89 raise NotImplementedError | |
| 90 if 0: | |
| 91 if __name__ == .__main__.: | |
| 92 | |
| 93 Note that when using the ``exclude_lines`` option in a configuration file, you | |
| 94 are taking control of the entire list of regexes, so you need to re-specify the | |
| 95 default "pragma: no cover" match if you still want it to apply. | |
| 96 | |
| 97 A similar pragma, "no branch", can be used to tailor branch coverage | |
| 98 measurement. See :ref:`branch` for details. | |
| 99 | |
| 100 | |
| 101 Excluding source files | |
| 102 ---------------------- | |
| 103 | |
| 104 See :ref:`source` for ways to limit what files coverage.py measures or reports | |
| 105 on. | |
| OLD | NEW |