OLD | NEW |
(Empty) | |
| 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 |
| 4 .. _cmd: |
| 5 |
| 6 ============================== |
| 7 Coverage.py command line usage |
| 8 ============================== |
| 9 |
| 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 stu
ff. |
| 20 .. :history: 20120807T211600, Clarified the combine rules. |
| 21 .. :history: 20121003T074600, Fixed an option reference, https://bitbucket.org/n
ed/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 |
| 25 |
| 26 .. highlight:: console |
| 27 |
| 28 |
| 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. |
| 35 |
| 36 Coverage.py has a number of commands which determine the action performed: |
| 37 |
| 38 * **run** -- Run a Python program and collect execution data. |
| 39 |
| 40 * **report** -- Report coverage results. |
| 41 |
| 42 * **html** -- Produce annotated HTML listings with coverage results. |
| 43 |
| 44 * **xml** -- Produce an XML report with coverage results. |
| 45 |
| 46 * **annotate** -- Annotate source files with coverage results. |
| 47 |
| 48 * **erase** -- Erase previously collected coverage data. |
| 49 |
| 50 * **combine** -- Combine together a number of data files. |
| 51 |
| 52 * **debug** -- Get diagnostic information. |
| 53 |
| 54 Help is available with the **help** command, or with the ``--help`` switch on |
| 55 any other command:: |
| 56 |
| 57 $ coverage help |
| 58 $ coverage help run |
| 59 $ coverage run --help |
| 60 |
| 61 Version information for coverage.py can be displayed with |
| 62 ``coverage --version``. |
| 63 |
| 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. |
| 70 |
| 71 |
| 72 .. _cmd_execution: |
| 73 |
| 74 Execution |
| 75 --------- |
| 76 |
| 77 You collect execution data by running your Python program with the **run** |
| 78 command:: |
| 79 |
| 80 $ coverage run my_program.py arg1 arg2 |
| 81 blah blah ..your program's output.. blah blah |
| 82 |
| 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:: |
| 88 |
| 89 $ coverage run -m packagename.modulename arg1 arg2 |
| 90 blah blah ..your program's output.. blah blah |
| 91 |
| 92 If you want :ref:`branch coverage <branch>` measurement, use the ``--branch`` |
| 93 flag. Otherwise only statement coverage is measured. |
| 94 |
| 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:: |
| 99 |
| 100 $ coverage run --source=dir1,dir2 my_program.py arg1 arg2 |
| 101 $ coverage run --source=dir1,dir2 -m packagename.modulename arg1 arg2 |
| 102 |
| 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 ``greenlet``, ``eventlet``, or ``gevent``. |
| 108 |
| 109 .. _multiprocessing: https://docs.python.org/2/library/multiprocessing.html |
| 110 .. _greenlet: http://greenlet.readthedocs.org/en/latest/ |
| 111 .. _gevent: http://www.gevent.org/ |
| 112 .. _eventlet: http://eventlet.net/ |
| 113 |
| 114 By default, coverage.py does not measure code installed with the Python |
| 115 interpreter, for example, the standard library. If you want to measure that |
| 116 code as well as your own, add the ``-L`` flag. |
| 117 |
| 118 If your coverage results seem to be overlooking code that you know has been |
| 119 executed, try running coverage.py again with the ``--timid`` flag. This uses a |
| 120 simpler but slower trace method. Projects that use DecoratorTools, including |
| 121 TurboGears, will need to use ``--timid`` to get correct results. |
| 122 |
| 123 If you are measuring coverage in a multi-process program, or across a number of |
| 124 machines, you'll want the ``--parallel-mode`` switch to keep the data separate |
| 125 during measurement. See :ref:`cmd_combining` below. |
| 126 |
| 127 During execution, coverage.py may warn you about conditions it detects that |
| 128 could affect the measurement process. The possible warnings include: |
| 129 |
| 130 * "Trace function changed, measurement is likely wrong: XXX" |
| 131 |
| 132 Coverage measurement depends on a Python setting called the trace function. |
| 133 Other Python code in your product might change that function, which will |
| 134 disrupt coverage.py's measurement. This warning indicate that has happened. |
| 135 The XXX in the message is the new trace function value, which might provide |
| 136 a clue to the cause. |
| 137 |
| 138 * "Module XXX has no Python source" |
| 139 |
| 140 You asked coverage.py to measure module XXX, but once it was imported, it |
| 141 turned out not to have a corresponding .py file. Without a .py file, |
| 142 coverage.py can't report on missing lines. |
| 143 |
| 144 * "Module XXX was never imported" |
| 145 |
| 146 You asked coverage.py to measure module XXX, but it was never imported by |
| 147 your program. |
| 148 |
| 149 * "No data was collected" |
| 150 |
| 151 Coverage.py ran your program, but didn't measure any lines as executed. |
| 152 This could be because you asked to measure only modules that never ran, |
| 153 or for other reasons. |
| 154 |
| 155 * "Module XXX was previously imported, but not measured." |
| 156 |
| 157 You asked coverage.py to measure module XXX, but it had already been imported |
| 158 when coverage started. This meant coverage.py couldn't monitor its |
| 159 execution. |
| 160 |
| 161 |
| 162 .. _cmd_datafile: |
| 163 |
| 164 Data file |
| 165 --------- |
| 166 |
| 167 Coverage.py collects execution data in a file called ".coverage". If need be, |
| 168 you can set a new file name with the COVERAGE_FILE environment variable. |
| 169 |
| 170 By default,each run of your program starts with an empty data set. If you need |
| 171 to run your program multiple times to get complete data (for example, because |
| 172 you need to supply disjoint options), you can accumulate data across runs with |
| 173 the ``-a`` flag on the **run** command. |
| 174 |
| 175 To erase the collected data, use the **erase** command:: |
| 176 |
| 177 $ coverage erase |
| 178 |
| 179 |
| 180 .. _cmd_combining: |
| 181 |
| 182 Combining data files |
| 183 -------------------- |
| 184 |
| 185 If you need to collect coverage data from different machines or processes, |
| 186 coverage.py can combine multiple files into one for reporting. |
| 187 |
| 188 Once you have created a number of these files, you can copy them all to a |
| 189 single directory, and use the **combine** command to combine them into one |
| 190 .coverage data file:: |
| 191 |
| 192 $ coverage combine |
| 193 |
| 194 You can also name directories or files on the command line:: |
| 195 |
| 196 $ combine combine data1.dat windows_data_files/ |
| 197 |
| 198 Coverage.py will collect the data from those places and combine them. The |
| 199 current directory isn't searched if you use command-line arguments. If you |
| 200 also want data from the current directory, name it explicitly on the command |
| 201 line. |
| 202 |
| 203 When coverage.py looks in directories for data files to combine, even the |
| 204 current directory, it only reads files with certain names. It looks for files |
| 205 named the same as the data file (defaulting to ".coverage"), with a dotted |
| 206 suffix. Here are some examples of data files that can be combined:: |
| 207 |
| 208 .coverage.machine1 |
| 209 .coverage.20120807T212300 |
| 210 .coverage.last_good_run.ok |
| 211 |
| 212 The ``run --parallel-mode`` switch automatically creates separate data files |
| 213 for each run which can be combined later. The file names include the machine |
| 214 name, the process id, and a random number:: |
| 215 |
| 216 .coverage.Neds-MacBook-Pro.local.88335.316857 |
| 217 .coverage.Geometer.8044.799674 |
| 218 |
| 219 If the different machines run your code from different places in their file |
| 220 systems, coverage.py won't know how to combine the data. You can tell |
| 221 coverage.py how the different locations correlate with a ``[paths]`` section in |
| 222 your configuration file. See :ref:`config_paths` for details. |
| 223 |
| 224 .. _cmd_reporting: |
| 225 |
| 226 Reporting |
| 227 --------- |
| 228 |
| 229 Coverage.py provides a few styles of reporting, with the **report**, **html**, |
| 230 **annotate**, and **xml** commands. They share a number of common options. |
| 231 |
| 232 The command-line arguments are module or file names to report on, if you'd like |
| 233 to report on a subset of the data collected. |
| 234 |
| 235 The ``--include`` and ``--omit`` flags specify lists of file name patterns. |
| 236 They control which files to report on, and are described in more detail in |
| 237 :ref:`source`. |
| 238 |
| 239 The ``-i`` or ``--ignore-errors`` switch tells coverage.py to ignore problems |
| 240 encountered trying to find source files to report on. This can be useful if |
| 241 some files are missing, or if your Python execution is tricky enough that file |
| 242 names are synthesized without real source files. |
| 243 |
| 244 If you provide a ``--fail-under`` value, the total percentage covered will be |
| 245 compared to that value. If it is less, the command will exit with a status |
| 246 code of 2, indicating that the total coverage was less than your target. This |
| 247 can be used as part of a pass/fail condition, for example in a continuous |
| 248 integration server. This option isn't available for **annotate**. |
| 249 |
| 250 |
| 251 .. _cmd_summary: |
| 252 |
| 253 Coverage summary |
| 254 ---------------- |
| 255 |
| 256 The simplest reporting is a textual summary produced with **report**:: |
| 257 |
| 258 $ coverage report |
| 259 Name Stmts Miss Cover |
| 260 --------------------------------------------- |
| 261 my_program.py 20 4 80% |
| 262 my_module.py 15 2 86% |
| 263 my_other_module.py 56 6 89% |
| 264 --------------------------------------------- |
| 265 TOTAL 91 12 87% |
| 266 |
| 267 For each module executed, the report shows the count of executable statements, |
| 268 the number of those statements missed, and the resulting coverage, expressed |
| 269 as a percentage. |
| 270 |
| 271 The ``-m`` flag also shows the line numbers of missing statements:: |
| 272 |
| 273 $ coverage report -m |
| 274 Name Stmts Miss Cover Missing |
| 275 ------------------------------------------------------- |
| 276 my_program.py 20 4 80% 33-35, 39 |
| 277 my_module.py 15 2 86% 8, 12 |
| 278 my_other_module.py 56 6 89% 17-23 |
| 279 ------------------------------------------------------- |
| 280 TOTAL 91 12 87% |
| 281 |
| 282 If you are using branch coverage, then branch statistics will be reported in |
| 283 the Branch and BrPart (for Partial Branch) columns, the Missing column will |
| 284 detail the missed branches:: |
| 285 |
| 286 $ coverage report -m |
| 287 Name Stmts Miss Branch BrPart Cover Missing |
| 288 --------------------------------------------------------------------- |
| 289 my_program.py 20 4 10 2 80% 33-35, 36->38,
39 |
| 290 my_module.py 15 2 3 0 86% 8, 12 |
| 291 my_other_module.py 56 6 5 1 89% 17-23, 40->45 |
| 292 --------------------------------------------------------------------- |
| 293 TOTAL 91 12 18 3 87% |
| 294 |
| 295 You can restrict the report to only certain files by naming them on the |
| 296 command line:: |
| 297 |
| 298 $ coverage report -m my_program.py my_other_module.py |
| 299 Name Stmts Miss Cover Missing |
| 300 ------------------------------------------------------- |
| 301 my_program.py 20 4 80% 33-35, 39 |
| 302 my_other_module.py 56 6 89% 17-23 |
| 303 ------------------------------------------------------- |
| 304 TOTAL 76 10 87% |
| 305 |
| 306 The ``--skip-covered`` switch will leave out any file with 100% coverage, |
| 307 letting you focus on the files that still need attention. |
| 308 |
| 309 Other common reporting options are described above in :ref:`cmd_reporting`. |
| 310 |
| 311 |
| 312 .. _cmd_html: |
| 313 |
| 314 HTML annotation |
| 315 --------------- |
| 316 |
| 317 Coverage.py can annotate your source code for which lines were executed |
| 318 and which were not. The **html** command creates an HTML report similar to the |
| 319 **report** summary, but as an HTML file. Each module name links to the source |
| 320 file decorated to show the status of each line. |
| 321 |
| 322 Here's a `sample report`__. |
| 323 |
| 324 __ http://nedbatchelder.com/files/sample_coverage_html/index.html |
| 325 |
| 326 Lines are highlighted green for executed, red for missing, and gray for |
| 327 excluded. The counts at the top of the file are buttons to turn on and off |
| 328 the highlighting. |
| 329 |
| 330 A number of keyboard shortcuts are available for navigating the report. |
| 331 Click the keyboard icon in the upper right to see the complete list. |
| 332 |
| 333 The title of the report can be set with the ``title`` setting in the |
| 334 ``[html]`` section of the configuration file, or the ``--title`` switch on |
| 335 the command line. |
| 336 |
| 337 If you prefer a different style for your HTML report, you can provide your |
| 338 own CSS file to apply, by specifying a CSS file in the ``[html]`` section of |
| 339 the configuration file. See :ref:`config_html` for details. |
| 340 |
| 341 The ``-d`` argument specifies an output directory, defaulting to "htmlcov":: |
| 342 |
| 343 $ coverage html -d coverage_html |
| 344 |
| 345 Other common reporting options are described above in :ref:`cmd_reporting`. |
| 346 |
| 347 Generating the HTML report can be time-consuming. Stored with the HTML report |
| 348 is a data file that is used to speed up reporting the next time. If you |
| 349 generate a new report into the same directory, coverage.py will skip |
| 350 generating unchanged pages, making the process faster. |
| 351 |
| 352 |
| 353 .. _cmd_annotation: |
| 354 |
| 355 Text annotation |
| 356 --------------- |
| 357 |
| 358 The **annotate** command produces a text annotation of your source code. With |
| 359 a ``-d`` argument specifying an output directory, each Python file becomes a |
| 360 text file in that directory. Without ``-d``, the files are written into the |
| 361 same directories as the original Python files. |
| 362 |
| 363 Coverage status for each line of source is indicated with a character prefix:: |
| 364 |
| 365 > executed |
| 366 ! missing (not executed) |
| 367 - excluded |
| 368 |
| 369 For example:: |
| 370 |
| 371 # A simple function, never called with x==1 |
| 372 |
| 373 > def h(x): |
| 374 """Silly function.""" |
| 375 - if 0: #pragma: no cover |
| 376 - pass |
| 377 > if x == 1: |
| 378 ! a = 1 |
| 379 > else: |
| 380 > a = 2 |
| 381 |
| 382 Other common reporting options are described above in :ref:`cmd_reporting`. |
| 383 |
| 384 |
| 385 .. _cmd_xml: |
| 386 |
| 387 XML reporting |
| 388 ------------- |
| 389 |
| 390 The **xml** command writes coverage data to a "coverage.xml" file in a format |
| 391 compatible with `Cobertura`_. |
| 392 |
| 393 .. _Cobertura: http://cobertura.sourceforge.net |
| 394 |
| 395 You can specify the name of the output file with the ``-o`` switch. |
| 396 |
| 397 Other common reporting options are described above in :ref:`cmd_reporting`. |
| 398 |
| 399 |
| 400 .. _cmd_debug: |
| 401 |
| 402 Diagnostics |
| 403 ----------- |
| 404 |
| 405 The **debug** command shows internal information to help diagnose problems. |
| 406 If you are reporting a bug about coverage.py, including the output of this |
| 407 command can often help:: |
| 408 |
| 409 $ coverage debug sys > please_attach_to_bug_report.txt |
| 410 |
| 411 Two types of information are available: ``sys`` to show system configuration, |
| 412 and ``data`` to show a summary of the collected coverage data. |
| 413 |
| 414 |
| 415 .. _cmd_run_debug: |
| 416 |
| 417 The ``--debug`` option is available on all commands. It instructs coverage.py |
| 418 to log internal details of its operation, to help with diagnosing problems. It |
| 419 takes a comma-separated list of options, each indicating a facet of operation |
| 420 to log: |
| 421 |
| 422 * ``callers``: annotate each debug message with a stack trace of the callers |
| 423 to that point. |
| 424 |
| 425 * ``config``: before starting, dump all the :ref:`configuration <config>` |
| 426 values. |
| 427 |
| 428 * ``dataio``: log when reading or writing any data file. |
| 429 |
| 430 * ``dataop``: log when data is added to the CoverageData object. |
| 431 |
| 432 * ``pid``: annotate all debug output with the process id. |
| 433 |
| 434 * ``plugin``: print information about plugin operations. |
| 435 |
| 436 * ``sys``: before starting, dump all the system and environment information, |
| 437 as with :ref:`coverage debug sys <cmd_debug>`. |
| 438 |
| 439 * ``trace``: print every decision about whether to trace a file or not. For |
| 440 files not being traced, the reason is also given. |
| 441 |
| 442 Debug options can also be set with the ``COVERAGE_DEBUG`` environment variable, |
| 443 a comma-separated list of these options. |
| 444 |
| 445 The debug output goes to stderr, unless the ``COVERAGE_DEBUG_FILE`` environment |
| 446 variable names a different file, which will be appended to. |
OLD | NEW |