Sunday, March 6, 2011

Price Of Vodaphone Simcards In India

Python code coverage analysis

A powerful tool that has perl module Devel:: Cover. A module for python equivalent Coverage. Such programs, which makes is a code coverage analysis, it is mainly to test. This is very useful to know if our test of the application covering the entire code (ideally) and if not, how we need to get there, highlighting the parts of code executed and which not.

To install, we get off the sources and compile or use the repository.

$ sudo apt-get install python-coverage

It's very easy to use. The program has a little help:

$ python-coverage help
Coverage.py, version 3.2
Measure, collect, and report on Code Coverage in Python programs.

usage: coverage [options] [args]

Commands:
annotate Annotate source files with execution information.
    combine     Combine a number of data files.
    erase       Erase previously collected coverage data.
    help        Get help on using coverage.py.
    html        Create an HTML report.
    report      Report coverage stats on modules.
    run         Run a Python program and measure code execution.
    xml         Create an XML report of coverage results.

Use "coverage help " for detailed help on any command.
Use "coverage help classic" for help on older command syntax.
For more information, see http://nedbatchelder.com/code/coverage

Reporting

The first thing to do is run the program. This it will do is create a file. Coverage with coverage information.

$ python-coverage run example02.py

To see the results, we use the option to report:

$ python-coverage report - m example02.py Name Stmts Exec

----------------------- Missing Cover example02
------------------ 14 14 100%

Then we can generate the html file:

$ python-coverage-d html coverhtml
coverhtml $ firefox / index.html


Use the API

This does not end here. If you want you can make a program to run the analysis in the program you want and then generate a report.

The API is well explained, and the truth is quite sencillo.Un example: __________________________________________________


   # -*- coding: utf-8 -*-   
import
Coverage cov = coverage.coverage ()
cov.start () def
square (List):
"" "Find the picture of the numbers listed
>>> l = [ 0 , 1 , 2 , 3 ]
>>> square (l)
[0, 1, 4, 9]
"" "
return [n ** 2 for n into list]

def _test ():
imports doctest
doctest.testmod ()

_test ()

cov.stop ()
cov.html_report (directory = 'covhtml' ) _______________________________________________



$ python mycov.py

The program creates a directory covhtml with the results.



References
  • Python-coverage
  • API python-coverage

0 comments:

Post a Comment