<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.yoctoproject.org/wiki/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Humberto+Ibarra</id>
	<title>Yocto Project - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.yoctoproject.org/wiki/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Humberto+Ibarra"/>
	<link rel="alternate" type="text/html" href="https://wiki.yoctoproject.org/wiki/Special:Contributions/Humberto_Ibarra"/>
	<updated>2026-04-07T03:40:46Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.39.5</generator>
	<entry>
		<id>https://wiki.yoctoproject.org/wiki/index.php?title=Coverage&amp;diff=18067</id>
		<title>Coverage</title>
		<link rel="alternate" type="text/html" href="https://wiki.yoctoproject.org/wiki/index.php?title=Coverage&amp;diff=18067"/>
		<updated>2016-04-07T19:57:49Z</updated>

		<summary type="html">&lt;p&gt;Humberto Ibarra: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Code Coverage=&lt;br /&gt;
&lt;br /&gt;
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&#039;t on a certain test, but it can also contain relevant information such as branch or function coverage.&lt;br /&gt;
&lt;br /&gt;
For python code, there is an utility developed by Ned Batchelder which can give us these metrics, Coverage.py.&amp;lt;ref name=&amp;quot;coverage&amp;quot; &amp;gt;[http://coverage.readthedocs.org/en/latest/ Coverage.py]&amp;lt;/ref&amp;gt; This article is going to explain how to use this tool to gather meaningful data from the Yocto Project.&amp;lt;ref name=&amp;quot;yocto&amp;quot; &amp;gt;[http://www.yoctoproject.org/ Yocto Project]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Version 4.0.2 of Coverage.py&amp;lt;ref name=&amp;quot;coverage&amp;quot;&amp;gt;&amp;lt;/ref&amp;gt; was used to write the following instructions, changes might need to be made if using a different version.&lt;br /&gt;
&lt;br /&gt;
==Installation==&lt;br /&gt;
&lt;br /&gt;
There are a few ways of installing coverage, which are explained at the tool&#039;s site. &amp;lt;ref name=&amp;quot;install&amp;quot;&amp;gt;[http://coverage.readthedocs.org/en/latest/install.html Coverage.py Installation]&amp;lt;/ref&amp;gt; The simplest way is using pip:&lt;br /&gt;
&lt;br /&gt;
  $ pip install coverage&lt;br /&gt;
&lt;br /&gt;
==Usage==&lt;br /&gt;
&lt;br /&gt;
===Executing from the Command Line===&lt;br /&gt;
&lt;br /&gt;
====Gathering the coverage data====&lt;br /&gt;
&lt;br /&gt;
To get the coverage information from a specific script, we need to use the &#039;&#039;&#039;run&#039;&#039;&#039; subcommand. This is done by adding &#039;&#039;&#039;coverage run&#039;&#039;&#039; to the command we want to get the data from, all of the command&#039;s arguments remain unaltered:&lt;br /&gt;
&lt;br /&gt;
  $ &#039;&#039;&#039;coverage run&#039;&#039;&#039; &#039;&#039;command.py arg1 arg2&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
This will generate a &amp;lt;code&amp;gt;.coverage&amp;lt;/code&amp;gt; file in the current directory with all the coverage data from the script. For a complete explanation of the &#039;&#039;&#039;coverage run&#039;&#039;&#039; arguments, please check the Coverage.py documentation.&amp;lt;ref name=&amp;quot;coverage&amp;quot;&amp;gt;&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Reporting the coverage data====&lt;br /&gt;
&lt;br /&gt;
Once we have the &amp;lt;code&amp;gt;.coverage&amp;lt;/code&amp;gt; file we can analyze all the information present there. A full report of that data can be obtained by running the &#039;&#039;&#039;coverage report&#039;&#039;&#039; subcommand in the same folder where the data file is:&lt;br /&gt;
&lt;br /&gt;
  $ &#039;&#039;&#039;coverage report&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
All this data can also be shown in other formats, such as html or xml, for better easier analysis or parsing.&lt;br /&gt;
&lt;br /&gt;
  $ &#039;&#039;&#039;coverage html&#039;&#039;&#039;&lt;br /&gt;
  $ &#039;&#039;&#039;coverage xml&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
====Combining data files====&lt;br /&gt;
&lt;br /&gt;
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 &#039;&#039;&#039;coverage combine&#039;&#039;&#039; subcommand. This will merge the coverage information and will show us which code was ran by at least one of the testcases.&lt;br /&gt;
&lt;br /&gt;
  $ &#039;&#039;&#039;coverage combine&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
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 &amp;lt;code&amp;gt;.coverage.&#039;&#039;&amp;lt;testcase&amp;gt;&#039;&#039;&amp;lt;/code&amp;gt;, where &#039;&#039;&amp;lt;testcase&amp;gt;&#039;&#039; is any sufix given to the file; the resulting data file will be named &amp;lt;code&amp;gt;.coverage&amp;lt;/code&amp;gt; and will be placed in the same folder.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;IMPORTANT.&#039;&#039;&#039; 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 &#039;&#039;&#039;combine&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
===Using a configuration file===&lt;br /&gt;
&lt;br /&gt;
Instead of giving all options to coverage through the command line, a configuration file can be built. &#039;&#039;&#039;Coverage.py&#039;&#039;&#039; looks for this file in the same directory where it runs, the default name of this configuration file is &amp;lt;code&amp;gt;.coveragerc&amp;lt;/code&amp;gt;. 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.&lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
&lt;br /&gt;
  # .coveragerc to control coverage.py&lt;br /&gt;
  [run]&lt;br /&gt;
  branch = True&lt;br /&gt;
  &lt;br /&gt;
  [report]&lt;br /&gt;
  # Regexes for lines to exclude from consideration&lt;br /&gt;
  exclude_lines =&lt;br /&gt;
      # Have to re-enable the standard pragma&lt;br /&gt;
      pragma: no cover&lt;br /&gt;
      &lt;br /&gt;
      # Don&#039;t complain about missing debug-only code:&lt;br /&gt;
      def __repr__&lt;br /&gt;
      if self\.debug&lt;br /&gt;
      &lt;br /&gt;
      # Don&#039;t complain if tests don&#039;t hit defensive assertion code:&lt;br /&gt;
      raise AssertionError&lt;br /&gt;
      raise NotImplementedError&lt;br /&gt;
      &lt;br /&gt;
      # Don&#039;t complain if non-runnable code isn&#039;t run:&lt;br /&gt;
      if 0:&lt;br /&gt;
      if __name__ == .__main__.:&lt;br /&gt;
  &lt;br /&gt;
  ignore_errors = True&lt;br /&gt;
  &lt;br /&gt;
  [html]&lt;br /&gt;
  directory = coverage_html_report&lt;br /&gt;
&lt;br /&gt;
==Coverage in the Yocto Project==&lt;br /&gt;
&lt;br /&gt;
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 &amp;lt;code&amp;gt;scripts/oe-selftest&amp;lt;/code&amp;gt; script. The &amp;lt;code&amp;gt;oe-selftest&amp;lt;/code&amp;gt; script groups testcases from the system in modules, allowing us to check the status of different parts of the Yocto Project.&lt;br /&gt;
&lt;br /&gt;
Another important advantage of working with &amp;lt;code&amp;gt;oe-selftest&amp;lt;/code&amp;gt; script is that it isolates the changes away from the system&#039;s code, keeping the code to be measured unaltered.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;oe-selftest&amp;lt;/code&amp;gt; file has been modified to include coverage in the testcases that it runs, it just requires to have Coverage.py installed.&lt;br /&gt;
&lt;br /&gt;
To issue &amp;lt;code&amp;gt;oe-selftest&amp;lt;/code&amp;gt; with coverage measurement the &#039;&#039;--coverage&#039;&#039; option is used.. Here is an example of how to execute this feature:&lt;br /&gt;
&lt;br /&gt;
  $ &#039;&#039;&#039;oe-selftest --run-tests&#039;&#039;&#039; &#039;&#039;bblayers.BitbakeLayers.test_bitbakelayers_flatten&#039;&#039; &#039;&#039;&#039;--coverage&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
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 &#039;&#039;&#039;.coveragerc&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
===Filtering===&lt;br /&gt;
&lt;br /&gt;
The coverage measurement returns only the information from the files under the following directories:&lt;br /&gt;
&lt;br /&gt;
* {poky}/bitbake&lt;br /&gt;
* {poky}/scripts&lt;br /&gt;
* Layer folders specified in the &amp;lt;code&amp;gt;bblayers.conf&amp;lt;/code&amp;gt; file&lt;br /&gt;
&lt;br /&gt;
This is not always the required behavior so three options have been added to &amp;lt;code&amp;gt;oe-selftest&amp;lt;/code&amp;gt; to improve the filtering of the files reported.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;--coverage-source&amp;lt;/code&amp;gt; option specifies from which directories to gather coverage data. Giving this option will override the default behavior mentioned above. The following example only tracks coverage from the bitbake/bin folder:&lt;br /&gt;
&lt;br /&gt;
  $ &#039;&#039;&#039;oe-selftest --run-tests&#039;&#039;&#039; &#039;&#039;bblayers.BitbakeLayers.test_bitbakelayers_flatten&#039;&#039; &#039;&#039;&#039;--coverage&#039;&#039;&#039; &#039;&#039;&#039;--coverage-source&#039;&#039;&#039; &#039;&#039;bitbake/bin&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;--coverage-include&amp;lt;/code&amp;gt; option specifies a pattern to filter files inside the sources. Unlike the previous option (which is a folder or a list of folders), this option works with selecting files based on patterns, accepting wildcards. Only shows the files that match the pattern and that are inside one of the folders specified in the source directories. The following example only shows the files that contain the sequence &#039;&#039;bitbake&#039;&#039; and that are contained in the bitbake directory:&lt;br /&gt;
&lt;br /&gt;
  $ &#039;&#039;&#039;oe-selftest --run-tests&#039;&#039;&#039; &#039;&#039;bblayers.BitbakeLayers.test_bitbakelayers_flatten&#039;&#039; &#039;&#039;&#039;--coverage&#039;&#039;&#039; &#039;&#039;&#039;--coverage-source&#039;&#039;&#039; &#039;&#039;bitbake&#039;&#039; &#039;&#039;&#039;--coverage-include&#039;&#039;&#039; &#039;&#039;&amp;quot;*bitbake*&amp;quot;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;--coverage-omit&amp;lt;/code&amp;gt; behaves just like &amp;lt;code&amp;gt;--coverage-include&amp;lt;/code&amp;gt;, except that it specifies files not to be included in the report. The following example works similar to the last one, but it omits the files in &#039;&#039;bitbake/lib/bb&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
  $ &#039;&#039;&#039;oe-selftest --run-tests&#039;&#039;&#039; &#039;&#039;bblayers.BitbakeLayers.test_bitbakelayers_flatten&#039;&#039; &#039;&#039;&#039;--coverage&#039;&#039;&#039; &#039;&#039;&#039;--coverage-source&#039;&#039;&#039; &#039;&#039;bitbake&#039;&#039; &#039;&#039;&#039;--coverage-include&#039;&#039;&#039; &#039;&#039;&amp;quot;*bitbake*&amp;quot;&#039;&#039; &#039;&#039;&#039;--coverage-omit&#039;&#039;&#039; &#039;&#039;&amp;quot;*/lib/bb/*&amp;quot;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Humberto Ibarra</name></author>
	</entry>
</feed>