cmd.rst 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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. .. _cmd:
  4. ==============================
  5. Coverage.py command line usage
  6. ==============================
  7. .. :history: 20090524T134300, brand new docs.
  8. .. :history: 20090613T164000, final touches for 3.0
  9. .. :history: 20090913T084400, new command line syntax
  10. .. :history: 20091004T170700, changes for 3.1
  11. .. :history: 20091127T200700, changes for 3.2
  12. .. :history: 20100223T200600, changes for 3.3
  13. .. :history: 20100725T211700, updated for 3.4
  14. .. :history: 20110827T212500, updated for 3.5.1, combining aliases
  15. .. :history: 20120119T075600, Added some clarification from George Paci
  16. .. :history: 20120504T091800, Added info about execution warnings, and 3.5.2 stuff.
  17. .. :history: 20120807T211600, Clarified the combine rules.
  18. .. :history: 20121003T074600, Fixed an option reference, https://bitbucket.org/ned/coveragepy/issue/200/documentation-mentions-output-xml-instead
  19. .. :history: 20121117T091000, Added command aliases.
  20. .. :history: 20140924T193000, Added --concurrency
  21. .. :history: 20150802T174700, Updated for 4.0b1
  22. .. highlight:: console
  23. When you install coverage.py, a command-line script simply called ``coverage``
  24. is placed in your Python scripts directory. To help with multi-version
  25. installs, it will also create either a ``coverage2`` or ``coverage3`` alias,
  26. and a ``coverage-X.Y`` alias, depending on the version of Python you're using.
  27. For example, when installing on Python 2.7, you will be able to use
  28. ``coverage``, ``coverage2``, or ``coverage-2.7`` on the command line.
  29. Coverage.py has a number of commands which determine the action performed:
  30. * **run** -- Run a Python program and collect execution data.
  31. * **report** -- Report coverage results.
  32. * **html** -- Produce annotated HTML listings with coverage results.
  33. * **xml** -- Produce an XML report with coverage results.
  34. * **annotate** -- Annotate source files with coverage results.
  35. * **erase** -- Erase previously collected coverage data.
  36. * **combine** -- Combine together a number of data files.
  37. * **debug** -- Get diagnostic information.
  38. Help is available with the **help** command, or with the ``--help`` switch on
  39. any other command::
  40. $ coverage help
  41. $ coverage help run
  42. $ coverage run --help
  43. Version information for coverage.py can be displayed with
  44. ``coverage --version``.
  45. Any command can use a configuration file by specifying it with the
  46. ``--rcfile=FILE`` command-line switch. Any option you can set on the command
  47. line can also be set in the configuration file. This can be a better way to
  48. control coverage.py since the configuration file can be checked into source
  49. control, and can provide options that other invocation techniques (like test
  50. runner plugins) may not offer. See :ref:`config` for more details.
  51. .. _cmd_execution:
  52. Execution
  53. ---------
  54. You collect execution data by running your Python program with the **run**
  55. command::
  56. $ coverage run my_program.py arg1 arg2
  57. blah blah ..your program's output.. blah blah
  58. Your program runs just as if it had been invoked with the Python command line.
  59. Arguments after your file name are passed to your program as usual in
  60. ``sys.argv``. Rather than providing a file name, you can use the ``-m`` switch
  61. and specify an importable module name instead, just as you can with the
  62. Python ``-m`` switch::
  63. $ coverage run -m packagename.modulename arg1 arg2
  64. blah blah ..your program's output.. blah blah
  65. If you want :ref:`branch coverage <branch>` measurement, use the ``--branch``
  66. flag. Otherwise only statement coverage is measured.
  67. You can specify the code to measure with the ``--source``, ``--include``, and
  68. ``--omit`` switches. See :ref:`Specifying source files <source_execution>` for
  69. details of their interpretation. Remember to put options for run after "run",
  70. but before the program invocation::
  71. $ coverage run --source=dir1,dir2 my_program.py arg1 arg2
  72. $ coverage run --source=dir1,dir2 -m packagename.modulename arg1 arg2
  73. Coverage.py can measure multi-threaded programs by default. If you are using
  74. more exotic concurrency, with the `multiprocessing`_, `greenlet`_, `eventlet`_,
  75. or `gevent`_ libraries, then coverage.py will get very confused. Use the
  76. ``--concurrency`` switch to properly measure programs using these libraries.
  77. Give it a value of ``multiprocessing``, ``thread``, ``greenlet``, ``eventlet``,
  78. or ``gevent``. Values other than ``thread`` require the :ref:`C extension
  79. <install_extension>`.
  80. If you are using ``--concurrency=multiprocessing``, you must set other options
  81. in the configuration file. Options on the command line will not be passed to
  82. the processes that multiprocessing creates. Best practice is to use the
  83. configuration file for all options.
  84. .. _multiprocessing: https://docs.python.org/2/library/multiprocessing.html
  85. .. _greenlet: http://greenlet.readthedocs.org/en/latest/
  86. .. _gevent: http://www.gevent.org/
  87. .. _eventlet: http://eventlet.net/
  88. By default, coverage.py does not measure code installed with the Python
  89. interpreter, for example, the standard library. If you want to measure that
  90. code as well as your own, add the ``-L`` (or ``--pylib``) flag.
  91. If your coverage results seem to be overlooking code that you know has been
  92. executed, try running coverage.py again with the ``--timid`` flag. This uses a
  93. simpler but slower trace method. Projects that use DecoratorTools, including
  94. TurboGears, will need to use ``--timid`` to get correct results.
  95. If you are measuring coverage in a multi-process program, or across a number of
  96. machines, you'll want the ``--parallel-mode`` switch to keep the data separate
  97. during measurement. See :ref:`cmd_combining` below.
  98. During execution, coverage.py may warn you about conditions it detects that
  99. could affect the measurement process. The possible warnings include:
  100. * "Trace function changed, measurement is likely wrong: XXX"
  101. Coverage measurement depends on a Python setting called the trace function.
  102. Other Python code in your product might change that function, which will
  103. disrupt coverage.py's measurement. This warning indicate that has happened.
  104. The XXX in the message is the new trace function value, which might provide
  105. a clue to the cause.
  106. * "Module XXX has no Python source"
  107. You asked coverage.py to measure module XXX, but once it was imported, it
  108. turned out not to have a corresponding .py file. Without a .py file,
  109. coverage.py can't report on missing lines.
  110. * "Module XXX was never imported"
  111. You asked coverage.py to measure module XXX, but it was never imported by
  112. your program.
  113. * "No data was collected"
  114. Coverage.py ran your program, but didn't measure any lines as executed.
  115. This could be because you asked to measure only modules that never ran,
  116. or for other reasons.
  117. * "Module XXX was previously imported, but not measured."
  118. You asked coverage.py to measure module XXX, but it had already been imported
  119. when coverage started. This meant coverage.py couldn't monitor its
  120. execution.
  121. .. _cmd_datafile:
  122. Data file
  123. ---------
  124. Coverage.py collects execution data in a file called ".coverage". If need be,
  125. you can set a new file name with the COVERAGE_FILE environment variable. This
  126. can include a path to another directory.
  127. By default, each run of your program starts with an empty data set. If you need
  128. to run your program multiple times to get complete data (for example, because
  129. you need to supply disjoint options), you can accumulate data across runs with
  130. the ``-a`` flag on the **run** command.
  131. To erase the collected data, use the **erase** command::
  132. $ coverage erase
  133. .. _cmd_combining:
  134. Combining data files
  135. --------------------
  136. If you need to collect coverage data from different machines or processes,
  137. coverage.py can combine multiple files into one for reporting.
  138. Once you have created a number of these files, you can copy them all to a
  139. single directory, and use the **combine** command to combine them into one
  140. .coverage data file::
  141. $ coverage combine
  142. You can also name directories or files on the command line::
  143. $ coverage combine data1.dat windows_data_files/
  144. Coverage.py will collect the data from those places and combine them. The
  145. current directory isn't searched if you use command-line arguments. If you
  146. also want data from the current directory, name it explicitly on the command
  147. line.
  148. When coverage.py looks in directories for data files to combine, even the
  149. current directory, it only reads files with certain names. It looks for files
  150. named the same as the data file (defaulting to ".coverage"), with a dotted
  151. suffix. Here are some examples of data files that can be combined::
  152. .coverage.machine1
  153. .coverage.20120807T212300
  154. .coverage.last_good_run.ok
  155. An existing combined data file is ignored and re-written. If you want to use
  156. **combine** to accumulate results into the .coverage data file over a number of
  157. runs, use the ``--append`` switch on the **combine** command. This behavior
  158. was the default before version 4.2.
  159. The ``run --parallel-mode`` switch automatically creates separate data files
  160. for each run which can be combined later. The file names include the machine
  161. name, the process id, and a random number::
  162. .coverage.Neds-MacBook-Pro.local.88335.316857
  163. .coverage.Geometer.8044.799674
  164. If the different machines run your code from different places in their file
  165. systems, coverage.py won't know how to combine the data. You can tell
  166. coverage.py how the different locations correlate with a ``[paths]`` section in
  167. your configuration file. See :ref:`config_paths` for details.
  168. If any data files can't be read, coverage.py will print a warning indicating
  169. the file and the problem.
  170. .. _cmd_reporting:
  171. Reporting
  172. ---------
  173. Coverage.py provides a few styles of reporting, with the **report**, **html**,
  174. **annotate**, and **xml** commands. They share a number of common options.
  175. The command-line arguments are module or file names to report on, if you'd like
  176. to report on a subset of the data collected.
  177. The ``--include`` and ``--omit`` flags specify lists of file name patterns.
  178. They control which files to report on, and are described in more detail in
  179. :ref:`source`.
  180. The ``-i`` or ``--ignore-errors`` switch tells coverage.py to ignore problems
  181. encountered trying to find source files to report on. This can be useful if
  182. some files are missing, or if your Python execution is tricky enough that file
  183. names are synthesized without real source files.
  184. If you provide a ``--fail-under`` value, the total percentage covered will be
  185. compared to that value. If it is less, the command will exit with a status
  186. code of 2, indicating that the total coverage was less than your target. This
  187. can be used as part of a pass/fail condition, for example in a continuous
  188. integration server. This option isn't available for **annotate**.
  189. .. _cmd_summary:
  190. Coverage summary
  191. ----------------
  192. The simplest reporting is a textual summary produced with **report**::
  193. $ coverage report
  194. Name Stmts Miss Cover
  195. ---------------------------------------------
  196. my_program.py 20 4 80%
  197. my_module.py 15 2 86%
  198. my_other_module.py 56 6 89%
  199. ---------------------------------------------
  200. TOTAL 91 12 87%
  201. For each module executed, the report shows the count of executable statements,
  202. the number of those statements missed, and the resulting coverage, expressed
  203. as a percentage.
  204. The ``-m`` flag also shows the line numbers of missing statements::
  205. $ coverage report -m
  206. Name Stmts Miss Cover Missing
  207. -------------------------------------------------------
  208. my_program.py 20 4 80% 33-35, 39
  209. my_module.py 15 2 86% 8, 12
  210. my_other_module.py 56 6 89% 17-23
  211. -------------------------------------------------------
  212. TOTAL 91 12 87%
  213. If you are using branch coverage, then branch statistics will be reported in
  214. the Branch and BrPart (for Partial Branch) columns, the Missing column will
  215. detail the missed branches::
  216. $ coverage report -m
  217. Name Stmts Miss Branch BrPart Cover Missing
  218. ---------------------------------------------------------------------
  219. my_program.py 20 4 10 2 80% 33-35, 36->38, 39
  220. my_module.py 15 2 3 0 86% 8, 12
  221. my_other_module.py 56 6 5 1 89% 17-23, 40->45
  222. ---------------------------------------------------------------------
  223. TOTAL 91 12 18 3 87%
  224. You can restrict the report to only certain files by naming them on the
  225. command line::
  226. $ coverage report -m my_program.py my_other_module.py
  227. Name Stmts Miss Cover Missing
  228. -------------------------------------------------------
  229. my_program.py 20 4 80% 33-35, 39
  230. my_other_module.py 56 6 89% 17-23
  231. -------------------------------------------------------
  232. TOTAL 76 10 87%
  233. The ``--skip-covered`` switch will leave out any file with 100% coverage,
  234. letting you focus on the files that still need attention.
  235. Other common reporting options are described above in :ref:`cmd_reporting`.
  236. .. _cmd_html:
  237. HTML annotation
  238. ---------------
  239. Coverage.py can annotate your source code for which lines were executed
  240. and which were not. The **html** command creates an HTML report similar to the
  241. **report** summary, but as an HTML file. Each module name links to the source
  242. file decorated to show the status of each line.
  243. Here's a `sample report`__.
  244. __ http://nedbatchelder.com/files/sample_coverage_html/index.html
  245. Lines are highlighted green for executed, red for missing, and gray for
  246. excluded. The counts at the top of the file are buttons to turn on and off
  247. the highlighting.
  248. A number of keyboard shortcuts are available for navigating the report.
  249. Click the keyboard icon in the upper right to see the complete list.
  250. The title of the report can be set with the ``title`` setting in the
  251. ``[html]`` section of the configuration file, or the ``--title`` switch on
  252. the command line.
  253. If you prefer a different style for your HTML report, you can provide your
  254. own CSS file to apply, by specifying a CSS file in the ``[html]`` section of
  255. the configuration file. See :ref:`config_html` for details.
  256. The ``-d`` argument specifies an output directory, defaulting to "htmlcov"::
  257. $ coverage html -d coverage_html
  258. Other common reporting options are described above in :ref:`cmd_reporting`.
  259. Generating the HTML report can be time-consuming. Stored with the HTML report
  260. is a data file that is used to speed up reporting the next time. If you
  261. generate a new report into the same directory, coverage.py will skip
  262. generating unchanged pages, making the process faster.
  263. .. _cmd_annotation:
  264. Text annotation
  265. ---------------
  266. The **annotate** command produces a text annotation of your source code. With
  267. a ``-d`` argument specifying an output directory, each Python file becomes a
  268. text file in that directory. Without ``-d``, the files are written into the
  269. same directories as the original Python files.
  270. Coverage status for each line of source is indicated with a character prefix::
  271. > executed
  272. ! missing (not executed)
  273. - excluded
  274. For example::
  275. # A simple function, never called with x==1
  276. > def h(x):
  277. """Silly function."""
  278. - if 0: #pragma: no cover
  279. - pass
  280. > if x == 1:
  281. ! a = 1
  282. > else:
  283. > a = 2
  284. Other common reporting options are described above in :ref:`cmd_reporting`.
  285. .. _cmd_xml:
  286. XML reporting
  287. -------------
  288. The **xml** command writes coverage data to a "coverage.xml" file in a format
  289. compatible with `Cobertura`_.
  290. .. _Cobertura: http://cobertura.sourceforge.net
  291. You can specify the name of the output file with the ``-o`` switch.
  292. Other common reporting options are described above in :ref:`cmd_reporting`.
  293. .. _cmd_debug:
  294. Diagnostics
  295. -----------
  296. The **debug** command shows internal information to help diagnose problems.
  297. If you are reporting a bug about coverage.py, including the output of this
  298. command can often help::
  299. $ coverage debug sys > please_attach_to_bug_report.txt
  300. Three types of information are available:
  301. * ``config``: show coverage's configuration
  302. * ``sys``: show system configuration,
  303. * ``data``: show a summary of the collected coverage data
  304. .. _cmd_run_debug:
  305. The ``--debug`` option is available on all commands. It instructs coverage.py
  306. to log internal details of its operation, to help with diagnosing problems. It
  307. takes a comma-separated list of options, each indicating a facet of operation
  308. to log:
  309. * ``callers``: annotate each debug message with a stack trace of the callers
  310. to that point.
  311. * ``config``: before starting, dump all the :ref:`configuration <config>`
  312. values.
  313. * ``dataio``: log when reading or writing any data file.
  314. * ``dataop``: log when data is added to the CoverageData object.
  315. * ``pid``: annotate all debug output with the process id.
  316. * ``plugin``: print information about plugin operations.
  317. * ``sys``: before starting, dump all the system and environment information,
  318. as with :ref:`coverage debug sys <cmd_debug>`.
  319. * ``trace``: print every decision about whether to trace a file or not. For
  320. files not being traced, the reason is also given.
  321. Debug options can also be set with the ``COVERAGE_DEBUG`` environment variable,
  322. a comma-separated list of these options.
  323. The debug output goes to stderr, unless the ``COVERAGE_DEBUG_FILE`` environment
  324. variable names a different file, which will be appended to.