excluding.rst 3.5 KB

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