OLD | NEW |
(Empty) | |
| 1 .. _cmd: |
| 2 |
| 3 =========================== |
| 4 Coverage command line usage |
| 5 =========================== |
| 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 |
| 21 .. highlight:: console |
| 22 |
| 23 |
| 24 When you install coverage.py, a command-line script simply called ``coverage`` |
| 25 is placed in your Python scripts directory. To help with multi-version |
| 26 installs, it will also create either a ``coverage2`` or ``coverage3`` alias, |
| 27 and a ``coverage-X.Y`` alias, depending on the version of Python you're using. |
| 28 For example, when installing on Python 2.7, you will be able to use |
| 29 ``coverage``, ``coverage2``, or ``coverage-2.7`` on the command line. |
| 30 |
| 31 Coverage has a number of commands which determine the action performed: |
| 32 |
| 33 * **run** -- Run a Python program and collect execution data. |
| 34 |
| 35 * **report** -- Report coverage results. |
| 36 |
| 37 * **html** -- Produce annotated HTML listings with coverage results. |
| 38 |
| 39 * **xml** -- Produce an XML report with coverage results. |
| 40 |
| 41 * **annotate** -- Annotate source files with coverage results. |
| 42 |
| 43 * **erase** -- Erase previously collected coverage data. |
| 44 |
| 45 * **combine** -- Combine together a number of data files. |
| 46 |
| 47 * **debug** -- Get diagnostic information. |
| 48 |
| 49 Help is available with the **help** command, or with the ``--help`` switch on |
| 50 any other command:: |
| 51 |
| 52 $ coverage help |
| 53 $ coverage help run |
| 54 $ coverage run --help |
| 55 |
| 56 Version information for coverage.py can be displayed with |
| 57 ``coverage --version``. |
| 58 |
| 59 Any command can use a configuration file by specifying it with the |
| 60 ``--rcfile=FILE`` command-line switch. Any option you can set on the command |
| 61 line can also be set in the configuration file. This can be a better way to |
| 62 control coverage.py since the configuration file can be checked into source |
| 63 control, and can provide options that other invocation techniques (like test |
| 64 runner plugins) may not offer. See :ref:`config` for more details. |
| 65 |
| 66 |
| 67 .. _cmd_execution: |
| 68 |
| 69 Execution |
| 70 --------- |
| 71 |
| 72 You collect execution data by running your Python program with the **run** |
| 73 command:: |
| 74 |
| 75 $ coverage run my_program.py arg1 arg2 |
| 76 blah blah ..your program's output.. blah blah |
| 77 |
| 78 Your program runs just as if it had been invoked with the Python command line. |
| 79 Arguments after your file name are passed to your program as usual in |
| 80 ``sys.argv``. Rather than providing a filename, you can use the ``-m`` switch |
| 81 and specify an importable module name instead, just as you can with the |
| 82 Python ``-m`` switch:: |
| 83 |
| 84 $ coverage run -m packagename.modulename arg1 arg2 |
| 85 blah blah ..your program's output.. blah blah |
| 86 |
| 87 If you want :ref:`branch coverage <branch>` measurement, use the ``--branch`` |
| 88 flag. Otherwise only statement coverage is measured. |
| 89 |
| 90 You can specify the code to measure with the ``--source``, ``--include``, and |
| 91 ``--omit`` switches. See :ref:`Specifying source files <source_execution>` for |
| 92 details of their interpretation. Remember to put options for run after "run", |
| 93 but before the program invocation:: |
| 94 |
| 95 $ coverage run --source=dir1,dir2 my_program.py arg1 arg2 |
| 96 $ coverage run --source=dir1,dir2 -m packagename.modulename arg1 arg2 |
| 97 |
| 98 By default, coverage does not measure code installed with the Python |
| 99 interpreter, for example, the standard library. If you want to measure that |
| 100 code as well as your own, add the ``-L`` flag. |
| 101 |
| 102 If your coverage results seem to be overlooking code that you know has been |
| 103 executed, try running coverage again with the ``--timid`` flag. This uses a |
| 104 simpler but slower trace method. Projects that use DecoratorTools, including |
| 105 TurboGears, will need to use ``--timid`` to get correct results. This option |
| 106 can also be enabled by setting the environment variable COVERAGE_OPTIONS to |
| 107 ``--timid``. |
| 108 |
| 109 If you are measuring coverage in a multi-process program, or across a number of |
| 110 machines, you'll want the ``--parallel-mode`` switch to keep the data separate |
| 111 during measurement. See :ref:`cmd_combining` below. |
| 112 |
| 113 During execution, coverage.py may warn you about conditions it detects that |
| 114 could affect the measurement process. The possible warnings include: |
| 115 |
| 116 * "Trace function changed, measurement is likely wrong: XXX" |
| 117 |
| 118 Coverage measurement depends on a Python setting called the trace function. |
| 119 Other Python code in your product might change that function, which will |
| 120 disrupt coverage.py's measurement. This warning indicate that has happened. |
| 121 The XXX in the message is the new trace function value, which might provide |
| 122 a clue to the cause. |
| 123 |
| 124 * "Module XXX has no Python source" |
| 125 |
| 126 You asked coverage.py to measure module XXX, but once it was imported, it |
| 127 turned out not to have a corresponding .py file. Without a .py file, |
| 128 coverage.py can't report on missing lines. |
| 129 |
| 130 * "Module XXX was never imported" |
| 131 |
| 132 You asked coverage.py to measure module XXX, but it was never imported by |
| 133 your program. |
| 134 |
| 135 * "No data was collected" |
| 136 |
| 137 Coverage.py ran your program, but didn't measure any lines as executed. |
| 138 This could be because you asked to measure only modules that never ran, |
| 139 or for other reasons. |
| 140 |
| 141 |
| 142 |
| 143 .. _cmd_datafile: |
| 144 |
| 145 Data file |
| 146 --------- |
| 147 |
| 148 Coverage collects execution data in a file called ".coverage". If need be, you |
| 149 can set a new file name with the COVERAGE_FILE environment variable. |
| 150 |
| 151 By default,each run of your program starts with an empty data set. If you need |
| 152 to run your program multiple times to get complete data (for example, because |
| 153 you need to supply disjoint options), you can accumulate data across runs with |
| 154 the ``-a`` flag on the **run** command. |
| 155 |
| 156 To erase the collected data, use the **erase** command:: |
| 157 |
| 158 $ coverage erase |
| 159 |
| 160 |
| 161 .. _cmd_combining: |
| 162 |
| 163 Combining data files |
| 164 -------------------- |
| 165 |
| 166 If you need to collect coverage data from different machines or processes, |
| 167 coverage can combine multiple files into one for reporting. Use the ``-p`` flag |
| 168 during execution to append distinguishing information to the .coverage data |
| 169 file name. |
| 170 |
| 171 Once you have created a number of these files, you can copy them all to a single |
| 172 directory, and use the **combine** command to combine them into one .coverage |
| 173 data file:: |
| 174 |
| 175 $ coverage combine |
| 176 |
| 177 If the different machines run your code from different places in their file |
| 178 systems, coverage won't know how to combine the data. You can tell coverage |
| 179 how the different locations correlate with a ``[paths]`` section in your |
| 180 configuration file. See :ref:`config_paths` for details. |
| 181 |
| 182 If you are collecting and renaming your own data files, you'll need to name |
| 183 them properly for **combine** to find them. It looks for files named after |
| 184 the data file (defaulting to ".coverage", overridable with COVERAGE_FILE), with |
| 185 a dotted suffix. All such files in the current directory will be combined. |
| 186 Here are some examples of combinable data files:: |
| 187 |
| 188 .coverage.machine1 |
| 189 .coverage.20120807T212300 |
| 190 .coverage.last_good_run.ok |
| 191 |
| 192 |
| 193 .. _cmd_reporting: |
| 194 |
| 195 Reporting |
| 196 --------- |
| 197 |
| 198 Coverage provides a few styles of reporting, with the **report**, **html**, |
| 199 **annotate**, and **xml** commands. They share a number of common options. |
| 200 |
| 201 The command-line arguments are module or file names to report on, if you'd like |
| 202 to report on a subset of the data collected. |
| 203 |
| 204 The ``--include`` and ``--omit`` flags specify lists of filename patterns. They |
| 205 control which files to report on, and are described in more detail |
| 206 in :ref:`source`. |
| 207 |
| 208 The ``-i`` or ``--ignore-errors`` switch tells coverage.py to ignore problems |
| 209 encountered trying to find source files to report on. This can be useful if |
| 210 some files are missing, or if your Python execution is tricky enough that file |
| 211 names are synthesized without real source files. |
| 212 |
| 213 If you provide a ``--fail-under`` value, the total percentage covered will be |
| 214 compared to that value. If it is less, the command will exit with a status |
| 215 code of 2, indicating that the total coverage was less than your target. This |
| 216 can be used as part of a pass/fail condition, for example in a continuous |
| 217 integration server. This option isn't available for **annotate**. |
| 218 |
| 219 |
| 220 .. _cmd_summary: |
| 221 |
| 222 Coverage summary |
| 223 ---------------- |
| 224 |
| 225 The simplest reporting is a textual summary produced with **report**:: |
| 226 |
| 227 $ coverage report |
| 228 Name Stmts Miss Cover |
| 229 --------------------------------------------- |
| 230 my_program 20 4 80% |
| 231 my_module 15 2 86% |
| 232 my_other_module 56 6 89% |
| 233 --------------------------------------------- |
| 234 TOTAL 91 12 87% |
| 235 |
| 236 For each module executed, the report shows the count of executable statements, |
| 237 the number of those statements missed, and the resulting coverage, expressed |
| 238 as a percentage. |
| 239 |
| 240 The ``-m`` flag also shows the line numbers of missing statements:: |
| 241 |
| 242 $ coverage report -m |
| 243 Name Stmts Miss Cover Missing |
| 244 ------------------------------------------------------- |
| 245 my_program 20 4 80% 33-35, 39 |
| 246 my_module 15 2 86% 8, 12 |
| 247 my_other_module 56 6 89% 17-23 |
| 248 ------------------------------------------------------- |
| 249 TOTAL 91 12 87% |
| 250 |
| 251 You can restrict the report to only certain files by naming them on the |
| 252 command line:: |
| 253 |
| 254 $ coverage report -m my_program.py my_other_module.py |
| 255 Name Stmts Miss Cover Missing |
| 256 ------------------------------------------------------- |
| 257 my_program 20 4 80% 33-35, 39 |
| 258 my_other_module 56 6 89% 17-23 |
| 259 ------------------------------------------------------- |
| 260 TOTAL 76 10 87% |
| 261 |
| 262 Other common reporting options are described above in :ref:`cmd_reporting`. |
| 263 |
| 264 |
| 265 .. _cmd_html: |
| 266 |
| 267 HTML annotation |
| 268 --------------- |
| 269 |
| 270 Coverage can annotate your source code for which lines were executed |
| 271 and which were not. The **html** command creates an HTML report similar to the |
| 272 **report** summary, but as an HTML file. Each module name links to the source |
| 273 file decorated to show the status of each line. |
| 274 |
| 275 Here's a `sample report`__. |
| 276 |
| 277 __ /code/coverage/sample_html/index.html |
| 278 |
| 279 Lines are highlighted green for executed, red for missing, and gray for |
| 280 excluded. The counts at the top of the file are buttons to turn on and off |
| 281 the highlighting. |
| 282 |
| 283 A number of keyboard shortcuts are available for navigating the report. |
| 284 Click the keyboard icon in the upper right to see the complete list. |
| 285 |
| 286 The title of the report can be set with the ``title`` setting in the |
| 287 ``[html]`` section of the configuration file, or the ``--title`` switch on |
| 288 the command line. |
| 289 |
| 290 If you prefer a different style for your HTML report, you can provide your |
| 291 own CSS file to apply, by specifying a CSS file in the ``[html]`` section of |
| 292 the configuration file. See :ref:`config_html` for details. |
| 293 |
| 294 The ``-d`` argument specifies an output directory, defaulting to "htmlcov":: |
| 295 |
| 296 $ coverage html -d coverage_html |
| 297 |
| 298 Other common reporting options are described above in :ref:`cmd_reporting`. |
| 299 |
| 300 Generating the HTML report can be time-consuming. Stored with the HTML report |
| 301 is a data file that is used to speed up reporting the next time. If you |
| 302 generate a new report into the same directory, coverage.py will skip |
| 303 generating unchanged pages, making the process faster. |
| 304 |
| 305 |
| 306 .. _cmd_annotation: |
| 307 |
| 308 Text annotation |
| 309 --------------- |
| 310 |
| 311 The **annotate** command produces a text annotation of your source code. With a |
| 312 ``-d`` argument specifying an output directory, each Python file becomes a text |
| 313 file in that directory. Without ``-d``, the files are written into the same |
| 314 directories as the original Python files. |
| 315 |
| 316 Coverage status for each line of source is indicated with a character prefix:: |
| 317 |
| 318 > executed |
| 319 ! missing (not executed) |
| 320 - excluded |
| 321 |
| 322 For example:: |
| 323 |
| 324 # A simple function, never called with x==1 |
| 325 |
| 326 > def h(x): |
| 327 """Silly function.""" |
| 328 - if 0: #pragma: no cover |
| 329 - pass |
| 330 > if x == 1: |
| 331 ! a = 1 |
| 332 > else: |
| 333 > a = 2 |
| 334 |
| 335 Other common reporting options are described above in :ref:`cmd_reporting`. |
| 336 |
| 337 |
| 338 .. _cmd_xml: |
| 339 |
| 340 XML reporting |
| 341 ------------- |
| 342 |
| 343 The **xml** command writes coverage data to a "coverage.xml" file in a format |
| 344 compatible with `Cobertura`_. |
| 345 |
| 346 .. _Cobertura: http://cobertura.sourceforge.net |
| 347 |
| 348 You can specify the name of the output file with the ``-o`` switch. |
| 349 |
| 350 Other common reporting options are described above in :ref:`cmd_reporting`. |
| 351 |
| 352 |
| 353 .. _cmd_debug: |
| 354 |
| 355 Diagnostics |
| 356 ----------- |
| 357 |
| 358 The **debug** command shows internal information to help diagnose problems. |
| 359 If you are reporting a bug about coverage.py, including the output of this |
| 360 command can often help:: |
| 361 |
| 362 $ coverage debug sys > please_attach_to_bug_report.txt |
| 363 |
| 364 Two types of information are available: ``sys`` to show system configuration, |
| 365 and ``data`` to show a summary of the collected coverage data. |
OLD | NEW |