| OLD | NEW |
| 1 .. _branch: | 1 .. _branch: |
| 2 | 2 |
| 3 =========================== | 3 =========================== |
| 4 Branch coverage measurement | 4 Branch coverage measurement |
| 5 =========================== | 5 =========================== |
| 6 | 6 |
| 7 :history: 20091127T201300, new for version 3.2 | 7 :history: 20091127T201300, new for version 3.2 |
| 8 :history: 20100725T211700, updated for 3.4. | 8 :history: 20100725T211700, updated for 3.4. |
| 9 :history: 20110604T181700, updated for 3.5. | 9 :history: 20110604T181700, updated for 3.5. |
| 10 :history: 20111214T181800, Fix a bug that Guido pointed out. | 10 :history: 20111214T181800, Fix a bug that Guido pointed out. |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 66 | 66 |
| 67 The idea of tracking how lines follow each other was from `Titus Brown`__. | 67 The idea of tracking how lines follow each other was from `Titus Brown`__. |
| 68 Thanks, Titus! | 68 Thanks, Titus! |
| 69 | 69 |
| 70 __ http://ivory.idyll.org/blog | 70 __ http://ivory.idyll.org/blog |
| 71 | 71 |
| 72 | 72 |
| 73 Excluding code | 73 Excluding code |
| 74 -------------- | 74 -------------- |
| 75 | 75 |
| 76 If you have :ref:`excluded code <excluding>`, a condtional will not be | 76 If you have :ref:`excluded code <excluding>`, a condtional will not be counted |
| 77 counted as a branch if one of its choices is excluded:: | 77 as a branch if one of its choices is excluded:: |
| 78 | 78 |
| 79 def only_one_choice(x): | 79 def only_one_choice(x): |
| 80 if x: | 80 if x: |
| 81 blah1() | 81 blah1() |
| 82 blah2() | 82 blah2() |
| 83 else: # pragma: no cover | 83 else: # pragma: no cover |
| 84 # x is always true. | 84 # x is always true. |
| 85 blah3() | 85 blah3() |
| 86 | 86 |
| 87 Because the ``else`` clause is excluded, the ``if`` only has one possible | 87 Because the ``else`` clause is excluded, the ``if`` only has one possible next |
| 88 next line, so it isn't considered a branch at all. | 88 line, so it isn't considered a branch at all. |
| 89 | 89 |
| 90 | 90 |
| 91 Structurally partial branches | 91 Structurally partial branches |
| 92 ----------------------------- | 92 ----------------------------- |
| 93 | 93 |
| 94 Sometimes branching constructs are used in unusual ways that don't actually | 94 Sometimes branching constructs are used in unusual ways that don't actually |
| 95 branch. For example:: | 95 branch. For example:: |
| 96 | 96 |
| 97 while True: | 97 while True: |
| 98 if cond: | 98 if cond: |
| 99 break | 99 break |
| 100 do_something() | 100 do_something() |
| 101 | 101 |
| 102 Here the while loop will never exit normally, so it doesn't take both of its | 102 Here the while loop will never exit normally, so it doesn't take both of its |
| 103 "possible" branches. For some of these constructs, such as "while True:" and | 103 "possible" branches. For some of these constructs, such as "while True:" and |
| 104 "if 0:", coverage.py understands what is going on. In these cases, the line | 104 "if 0:", coverage.py understands what is going on. In these cases, the line |
| 105 will not be marked as a partial branch. | 105 will not be marked as a partial branch. |
| 106 | 106 |
| 107 But there are many ways in your own code to write intentionally partial | 107 But there are many ways in your own code to write intentionally partial |
| 108 branches, and you don't want coverage.py pestering you about them. You can | 108 branches, and you don't want coverage.py pestering you about them. You can |
| 109 tell coverage.py that you don't want them flagged by marking them with a | 109 tell coverage.py that you don't want them flagged by marking them with a |
| 110 pragma:: | 110 pragma:: |
| 111 | 111 |
| 112 i = 0 | 112 i = 0 |
| 113 while i < 999999999: # pragma: no branch | 113 while i < 999999999: # pragma: no branch |
| 114 if eventually(): | 114 if eventually(): |
| 115 break | 115 break |
| 116 | 116 |
| 117 Here the while loop will never complete because the break will always be taken | 117 Here the while loop will never complete because the break will always be taken |
| 118 at some point. Coverage.py can't work that out on its own, but the | 118 at some point. Coverage.py can't work that out on its own, but the "no branch" |
| 119 "no branch" pragma indicates that the branch is known to be partial, and | 119 pragma indicates that the branch is known to be partial, and the line is not |
| 120 the line is not flagged. | 120 flagged. |
| OLD | NEW |