OLD | NEW |
1 .. _excluding: | 1 .. _excluding: |
2 | 2 |
3 ============================ | 3 ============================ |
4 Excluding code from coverage | 4 Excluding code from coverage |
5 ============================ | 5 ============================ |
6 | 6 |
7 :history: 20090613T090500, brand new docs. | 7 :history: 20090613T090500, brand new docs. |
8 :history: 20100224T200900, updated for 3.3. | 8 :history: 20100224T200900, updated for 3.3. |
9 :history: 20100725T211700, updated for 3.4. | 9 :history: 20100725T211700, updated for 3.4. |
10 :history: 20110604T184400, updated for 3.5. | 10 :history: 20110604T184400, updated for 3.5. |
(...skipping 21 matching lines...) Expand all Loading... |
32 | 32 |
33 class MyObject(object): | 33 class MyObject(object): |
34 def __init__(self): | 34 def __init__(self): |
35 blah1() | 35 blah1() |
36 blah2() | 36 blah2() |
37 | 37 |
38 def __repr__(self): # pragma: no cover | 38 def __repr__(self): # pragma: no cover |
39 return "<MyObject>" | 39 return "<MyObject>" |
40 | 40 |
41 Excluded code is executed as usual, and its execution is recorded in the | 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 | 42 coverage data as usual. When producing reports though, coverage excludes it |
43 the list of missing code. | 43 from the list of missing code. |
44 | 44 |
45 | 45 |
46 Branch coverage | 46 Branch coverage |
47 --------------- | 47 --------------- |
48 | 48 |
49 When measuring :ref:`branch coverage <branch>`, a condtional will not be | 49 When measuring :ref:`branch coverage <branch>`, a condtional will not be |
50 counted as a branch if one of its choices is excluded:: | 50 counted as a branch if one of its choices is excluded:: |
51 | 51 |
52 def only_one_choice(x): | 52 def only_one_choice(x): |
53 if x: | 53 if x: |
54 blah1() | 54 blah1() |
55 blah2() | 55 blah2() |
56 else: # pragma: no cover | 56 else: # pragma: no cover |
57 # x is always true. | 57 # x is always true. |
58 blah3() | 58 blah3() |
59 | 59 |
60 Because the ``else`` clause is excluded, the ``if`` only has one possible | 60 Because the ``else`` clause is excluded, the ``if`` only has one possible next |
61 next line, so it isn't considered a branch at all. | 61 line, so it isn't considered a branch at all. |
62 | 62 |
63 | 63 |
64 Advanced exclusion | 64 Advanced exclusion |
65 ------------------ | 65 ------------------ |
66 | 66 |
67 Coverage identifies exclusions by matching lines against a list of regular | 67 Coverage identifies exclusions by matching lines against a list of regular |
68 expressions. Using :ref:`configuration files <config>` or the coverage | 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 | 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 | 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. | 71 exclude them all at once without littering your code with exclusion pragmas. |
72 | 72 |
73 For example, you might decide that __repr__ functions are usually only used | 73 For example, you might decide that __repr__ functions are usually only used in |
74 in debugging code, and are uninteresting to test themselves. You could exclude | 74 debugging code, and are uninteresting to test themselves. You could exclude |
75 all of them by adding a regex to the exclusion list:: | 75 all of them by adding a regex to the exclusion list:: |
76 | 76 |
77 [report] | 77 [report] |
78 exclude_lines = def __repr__ | 78 exclude_lines = def __repr__ |
79 | 79 |
80 For example, here's a list of exclusions I've used:: | 80 For example, here's a list of exclusions I've used:: |
81 | 81 |
82 [report] | 82 [report] |
83 exclude_lines = | 83 exclude_lines = |
84 pragma: no cover | 84 pragma: no cover |
(...skipping 11 matching lines...) Expand all Loading... |
96 | 96 |
97 A similar pragma, "no branch", can be used to tailor branch coverage | 97 A similar pragma, "no branch", can be used to tailor branch coverage |
98 measurement. See :ref:`branch` for details. | 98 measurement. See :ref:`branch` for details. |
99 | 99 |
100 | 100 |
101 Excluding source files | 101 Excluding source files |
102 ---------------------- | 102 ---------------------- |
103 | 103 |
104 See :ref:`source` for ways to limit what files coverage.py measures or reports | 104 See :ref:`source` for ways to limit what files coverage.py measures or reports |
105 on. | 105 on. |
OLD | NEW |