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