source.rst 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. .. _source:
  4. =======================
  5. Specifying source files
  6. =======================
  7. .. :history: 20100725T172000, new in 3.4
  8. When coverage.py is running your program and measuring its execution, it needs
  9. to know what code to measure and what code not to. Measurement imposes a speed
  10. penalty, and the collected data must be stored in memory and then on disk.
  11. More importantly, when reviewing your coverage reports, you don't want to be
  12. distracted with modules that aren't your concern.
  13. Coverage.py has a number of ways you can focus it in on the code you care
  14. about.
  15. .. _source_execution:
  16. Execution
  17. ---------
  18. When running your code, the ``coverage run`` command will by default measure
  19. all code, unless it is part of the Python standard library.
  20. You can specify source to measure with the ``--source`` command-line switch, or
  21. the ``[run] source`` configuration value. The value is a comma- or
  22. newline-separated list of directories or package names. If specified, only
  23. source inside these directories or packages will be measured. Specifying the
  24. source option also enables coverage.py to report on unexecuted files, since it
  25. can search the source tree for files that haven't been measured at all. Only
  26. importable files (ones at the root of the tree, or in directories with a
  27. ``__init__.py`` file) will be considered, and files with unusual punctuation in
  28. their names will be skipped (they are assumed to be scratch files written by
  29. text editors).
  30. You can further fine-tune coverage.py's attention with the ``--include`` and
  31. ``--omit`` switches (or ``[run] include`` and ``[run] omit`` configuration
  32. values). ``--include`` is a list of file name patterns. If specified, only
  33. files matching those patterns will be measured. ``--omit`` is also a list of
  34. file name patterns, specifying files not to measure. If both ``include`` and
  35. ``omit`` are specified, first the set of files is reduced to only those that
  36. match the include patterns, then any files that match the omit pattern are
  37. removed from the set.
  38. The ``include`` and ``omit`` file name patterns follow typical shell syntax:
  39. ``*`` matches any number of characters and ``?`` matches a single character.
  40. Patterns that start with a wildcard character are used as-is, other patterns
  41. are interpreted relative to the current directory::
  42. [run]
  43. omit =
  44. # omit anything in a .local directory anywhere
  45. */.local/*
  46. # omit everything in /usr
  47. /usr/*
  48. # omit this single file
  49. utils/tirefire.py
  50. The ``source``, ``include``, and ``omit`` values all work together to determine
  51. the source that will be measured.
  52. .. _source_reporting:
  53. Reporting
  54. ---------
  55. Once your program is measured, you can specify the source files you want
  56. reported. Usually you want to see all the code that was measured, but if you
  57. are measuring a large project, you may want to get reports for just certain
  58. parts.
  59. The report commands (``report``, ``html``, ``annotate``, and ``xml``) all take
  60. optional ``modules`` arguments, and ``--include`` and ``--omit`` switches. The
  61. ``modules`` arguments specify particular modules to report on. The ``include``
  62. and ``omit`` values are lists of file name patterns, just as with the ``run``
  63. command.
  64. Remember that the reporting commands can only report on the data that has been
  65. collected, so the data you're looking for may not be in the data available for
  66. reporting.
  67. Note that these are ways of specifying files to measure. You can also exclude
  68. individual source lines. See :ref:`excluding` for details.