Measuring code coverage of Python programs:
$ coverage help
- help Get help on using coverage.py.
- run Run a Python program and measure code execution.
- html Create an HTML report. –> creates htmlcov/index.html
Your program runs just as if it had been invoked with the Python command line.
$ coverage run my_program.py arg1 arg2
Rather than providing a file name, you can use the -m switch and specify an importable module name instead, just as you can with the Python -m switch:
$ coverage run -m packagename.modulename arg1 arg2
parseCommandLine.py
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
https://www.tutorialspoint.com/python/python_command_line_arguments.htm
"""
import sys, getopt
def main(argv):
inputfile = ''
outputfile = ''
try:
opts, args = getopt.getopt(argv,"hi:o:",["ifile=","ofile="])
except getopt.GetoptError:
print 'test.py -i <inputfile> -o <outputfile>'
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print 'test.py -i <inputfile> -o <outputfile>'
sys.exit()
elif opt in ("-i", "--ifile"):
inputfile = arg
elif opt in ("-o", "--ofile"):
outputfile = arg
print 'Input file is "', inputfile
print 'Output file is "', outputfile
if __name__ == "__main__":
print 'Number of arguments:', len(sys.argv), 'arguments.'
print 'Argument List:', str(sys.argv)
main(sys.argv[1:])
$ coverage run parseCommandLine.py
Number of arguments: 1 arguments.
Argument List: ['parseCommandLine.py']
Input file is "
Output file is "
$ coverage html
$ firefox htmlcov/index.html