| Mar | APR | May |
| 20 | ||
| 2020 | 2021 | 2022 |
COLLECTED BY
Collection: Common Crawl
●Python »
Python Developer's Guide »
stat
module). In these instances the module will appear to not have any coverage of
global statements but will have proper coverage of local statements (e.g.,
function definitions will not be traced, but the function bodies will).
Calculating the coverage of modules in this situation will simply require
manually looking at what local statements were not executed.
./python -m venv ../cpython-venv source ../cpython-venv/bin/activate pip install coverageOnmost Mac OS X systems run:
./python.exe -m venv ../cpython-venv source ../cpython-venv/bin/activate pip install coverageOn Windows run:
python.bat -m venv ..\\cpython-venv ..\\cpython-venv\\Scripts\\activate.bat pip install coverageYou can now use python without the ./ for the rest of these instructions, as long as your venv is activated. For more info on venv see Virtual Environment documentation. If this does not work for you for some reason, you should try using the in-development version of coverage.py to see if it has been updated as needed. To do this you should clone/check out the development version of coverage.py: git clone https://github.com/nedbat/coveragepy.git You will need to use the full path to the installation. Another option is to use an installed copy of coverage.py, if you already have it. For this, you will again need to use the full path to that installation.
COVERAGEDIR with the directory where your clone exists, e.g.
../coveragepy):
./python COVERAGEDIRCoverage.py will print out a little bit of helper text verifying that everything is working. If you are using an installed copy, you can do the following instead (note this must be installed using the built copy of Python, such as by venv):
./python -m coverageThe rest of the examples on how to use coverage.py will assume you are using a cloned copy, but you can substitute the above and all instructions should still be valid. To run the test suite under coverage.py, do the following:
./python COVERAGEDIR run --pylib Lib/test/regrtest.pyTo run only a single test, specify the module/package being tested in the
--source flag (so as to prune the coverage reporting to only the
module/package you are interested in) and then append the name of the test you
wish to run to the command:
./python COVERAGEDIR run --pylib --source=abc Lib/test/regrtest.py test_abcTo see the results of the coverage run, you can view a text-based report with:
./python COVERAGEDIR reportYou can use the
--show-missing flag to get a list of lines that were not
executed:
./python COVERAGEDIR report --show-missingBut one of the strengths of coverage.py is its HTML-based reports which let you visually see what lines of code were not tested:
./python COVERAGEDIR html -i --include=`pwd`/Lib/* --omit="Lib/test/*,Lib/*/tests/*"This will generate an HTML report in a directory named
htmlcov which
ignores any errors that may arise and ignores modules for which test coverage is
unimportant (e.g. tests, temp files, etc.). You can then open the
htmlcov/index.html file in a web browser to view the coverage results along
with pages that visibly show what lines of code were or were not executed.
--branch flag to your coverage run:
./python COVERAGEDIR run --pylib --branch <arguments to run test(s)>This will lead to the report stating not only what lines were not covered, but also what branch paths were not executed.
./python COVERAGEDIR --versionIf it says ‘without C extension’, then you will need to build the C extension. Assuming that coverage.py’s clone is at
COVERAGEDIR and your clone of CPython
is at CPYTHONDIR, you can do this by executing the following in your coverage.py
clone:
CPPFLAGS="-I CPYTHONDIR -I CPYTHONDIR/Include" CPYTHONDIR/python setup.py build_ext --inplaceThis will build coverage.py’s C extension code in-place, allowing the previous instructions on how to gather coverage to continue to work. To get coverage.py to be able to gather the most accurate coverage data on as many modules as possible with a HORRIBLE HACK that you should NEVER use in your own code, run the following from your CPython clone:
PYTHONPATH=COVERAGEDIR/coverage/fullcoverage ./python COVERAGEDIR run --pylib Lib/test/regrtest.pyThis will give you the most complete coverage possible for CPython’s standard library.
test (along with
any other flags you want to):
./python -m test --coverage -D `pwd`/coverage_data <test arguments>Do note the argument to
-D; if you do not specify an absolute path to where
you want the coverage data to end up it will go somewhere you don’t expect.
Note
If you are running coverage over the entire test suite, make sure to
add -x test_importlib test_runpy test_trace to exclude those tests as
they trigger exceptions during coverage; see
https://bugs.python.org/issue10541 and https://bugs.python.org/issue10991.
Once the tests are done you will find the directory you specified contains
files for each executed module along with which lines were executed how many
times.
make coverageThen run some code and gather coverage data with the
gcov command. In
order to create a HTML report you can install lcov. The command:
make coverage-lcovassembles coverage data, removes 3rd party and system libraries and finally creates a report. You can skip both steps and just run:
make coverage-reportif you like to generate a coverage report for Python’s stdlib tests. It takes about 20 to 30 minutes on a modern computer. Note Multiple test jobs may not work properly. C coverage reporting has only been tested with a single test process.
●Python »
Python Developer's Guide »