Coverage

From Yocto Project
Revision as of 22:11, 20 November 2015 by Juan Humberto Ibarra (talk | contribs) (Created page with "Gathering code coverage is an important metric to ponder when analyzing the effectiveness of testcases. It consists not only in retrieving which lines were run and which ones ...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

Gathering code coverage is an important metric to ponder when analyzing the effectiveness of testcases. It consists not only in retrieving which lines were run and which ones weren't on a certain test, but it can also contain relevant information such as branch or function coverage.

For python code, there is an utility developed by Ned Batchelder which can give us these metrics, Coverage.py.[1] This article is going to explain how to use this tool to gather meaningful data from the Yocto Project.[2]

Version 4.0.2 of Coverage.py[1] was used to write the following instructions, changes might need to be made if using a different version.

Installation

There are a few ways of installing coverage, which are explained at the tool's site. [3] The simplest way is using pip:

 $ pip install coverage

Usage

Executing from the Command Line

Gathering the coverage data

To get the coverage information from a specific script, we need to use the run subcommand. This is done by adding coverage run to the command we want to get the data from, all of the command's arguments remain unaltered:

 $ coverage run command.py arg1 arg2 

This will generate a .coverage file in the current directory with all the coverage data from the script. For a complete explanation of the coverage run arguments, please check the Coverage.py documentation.[1]

Reporting the coverage data

Once we have the .coverage file we can analyze all the information present there. A full report of that data can be obtained by running the coverage report subcommand in the same folder where the data file is:

 $ coverage report

All this data can also be shown in other formats, such as html or xml, for better easier analysis or parsing.

 $ coverage html
 $ coverage xml

These commands also require to be run in the same folder where the data file is. They can be used with a few options, which are detailed in the documentation of the tool also.

Combining data files

Sometimes we have more than one data file, each one with coverage information from different scripts or testcases. If this is the case, we can combine them into a single data file with the coverage combine subcommand. This will merge the coverage information and will show us which code was run by at least one of the testcases.

 $ coverage combine

As well as the previous subcommands, this one also needs to be run in the same folder where the data files are. All data files to be combined need to be called .coverage.<testcase>, where <testcase> is any sufix given to the file; the resulting data file will be named .coverage and will be placed in the same folder.

IMPORTANT. When combining data files, the original files are lost in the process. If these are needed, they should be copied to a different folder before the combine.

Using a configuration file

Instead of giving all options to coverage through the command line, a configuration file can be built. Coverage.py looks for this file in the same directory where it runs, the default name of this configuration file is .coveragerc. This allows a great customization alternative, since it can be stored with all the desired parameters and coverage will use those paramteres each time that runs.

There is a whole section in the coverage documentation given to the construction of a configuration file. Here is the sample file from the docs:

 # .coveragerc to control coverage.py
 [run]
 branch = True
 
 [report]
 # Regexes for lines to exclude from consideration
 exclude_lines =
     # Have to re-enable the standard pragma
     pragma: no cover
     
     # Don't complain about missing debug-only code:
     def __repr__
     if self\.debug
     
     # Don't complain if tests don't hit defensive assertion code:
     raise AssertionError
     raise NotImplementedError
     
     # Don't complain if non-runnable code isn't run:
     if 0:
     if __name__ == .__main__.:
 
 ignore_errors = True
 
 [html]
 directory = coverage_html_report

A configuration file is also of great help when using subprocessing, which is explained in the next section.

Subprocessing

Coverage.py has a feature to track subprocesses, this means that it can gather coverage data from which were not directly triggered from the command line. When this option is enabled, it will track every python process in the system, so it is important to specify the directories that are not required for the measurement.

To set the subprocess tracking there are a couple of things that need to be done. First, we need to export the COVERAGE_PROCESS_START variable, which will point to our configuration file. Also, the sitecustomize.py file located in out python folder needs to be modified to contain the following:

 import coverage
 
 coverage.process_startup()

Coverage in the Yocto Project

In the Yocto Project there are a large variety of python scripts that can be tracked with this tool. However, one of the best parts to work is in the scripts/oe-selftest script. The oe-selftest script groups testcases from the system in modules, allowing us to check the status of different parts of the Yocto Project.

Another important advantage of working with oe-selftest script is that it isolates the changes away from the system's code, keeping the code to be measured unaltered.

There is currently a branch in poky-contrib that has implemented Coverage.py inside oe-selftest. This branch can be checked out with the following command:

 git -b lsandov1/coverage git://git.yoctoproject.org/poky-contrib

In this branch, the oe-selftest file has been modified to include coverage in the testcases that it runs. To use this version of oe-selftest the following is required:

  1. Install Coverage.py
  2. Modify sitecustomize.py file to include subprocessing. (As mentioned in the previous section)
  3. Issue oe-selftest script with coverage enabled

For the last step, a --coverage option was added to oe-selftest in this branch. Here is an example of how to implement this feature:

 $ oe-selftest --run-tests bblayers.BitbakeLayers.test_bitbakelayers_flatten --coverage

This command will run the given testcase with coverage tracking, generating a datafile in the current folder. For the previous example, the datafile generated will be named .coverage.bblayers.BitbakeLayers.test_bitbakelayers_flatten.


References