Coverage

=Code Coverage=
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.Coverage.py This article is going to explain how to use this tool to gather meaningful data from the Yocto Project.Yocto Project
Version 4.0.2 of Coverage.py 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. Coverage.py Installation 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.
====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 ran 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 parameters 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
==Source code Coverage in the Yocto Project==
Source code coverage is done through the oe-selftest. To enable this feature, include the --coverage (this option must be present, even when filtering options are used) argument into the script:
 $ oe-selftest --run-tests bblayers.BitbakeLayers.test_bitbakelayers_flatten --coverage
This command will run the given testcase with coverage tracking, generating a datafile and a configuration file in the current folder. The data file name is generated with the current timestamp, while the configuration file is always named .coveragerc.
===Filtering===
By default, source code coverage looks into these Poky directories:
* bitbake
* scripts
* meta layers specified in the conf/bblayers.conf file
This is not always the required behavior so the following sections explain the filtering features that narrow the coverage. 
* Selecting source directories
The --coverage-source option lets the user specify source directories. This option overrides the default behavior mentioned above. For example, let's coverage just the directory bitbake/bin:
 $ oe-selftest --run-tests bblayers.BitbakeLayers.test_bitbakelayers_flatten --coverage --coverage-source bitbake/bin
* Including files using patterns
The --coverage-include option specifies a pattern to filter files inside the given sources. Unlike the previous option (which is a folder or a list of folders), this option works with patterns (see to check supported patterns). The following example only covers those files that contain the word bitbake:
 $ oe-selftest --run-tests bblayers.BitbakeLayers.test_bitbakelayers_flatten --coverage --coverage-include "*bitbake*"
* Omitting files using patterns
The --coverage-omit option behaves just like --coverage-include, but the behaviour is exactly the opposite: it omits files depending on the given pattern (see to check supported patterns). The following example omits coverage for wic related files:
 $ oe-selftest --run-tests bblayers.BitbakeLayers.test_bitbakelayers_flatten --coverage --coverage-omit "*wic*"
It is important to notice that multiple filtering options can be provided in the same command allowing more complex scenarios.
==References==