Search code examples
pythonwindowscode-coveragecoverage.py

CoveragePy command for measuring Code Coverage


Introduction

A directory containing a test and main python file has been created. Executing a test works:

C:\...>py test/test.py
..
----------------------------------------------------------------------
Ran 2 tests in 0.001s

OK

According to this documentation it is possible to measure code coverage using the command line.

A number of commands has been issued:

C:\...>coverage run --source="C:\path\to\test"
Nothing to do.
Use 'coverage help' for help.

C:\...>coverage run --source "C:\path\to\test\main.py" -m "C:\path\to\test\test.py"
No module named 'C:\path\to\test\test.py'

without success.

Question

Which command needs to be issued in order to measure the code coverage of a Python file using CoveragePy?


Solution

  • For coverage to capture data, you run the tests with the coverage tool. Something like this:

    coverage run py test/test.py
    

    Coverage can be awkward to get set up, but it's incredibly useful -- have fun!

    You collect execution data by running your Python program with the run command:

    $ coverage run my_program.py arg1 arg2 
    

    (from http://nedbatchelder.com/code/coverage/cmd.html#cmd)