<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.brandon-rodriguez.com/index.php?action=history&amp;feed=atom&amp;title=Python%2FTesting%2FCodeCoverage</id>
	<title>Python/Testing/CodeCoverage - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.brandon-rodriguez.com/index.php?action=history&amp;feed=atom&amp;title=Python%2FTesting%2FCodeCoverage"/>
	<link rel="alternate" type="text/html" href="https://wiki.brandon-rodriguez.com/index.php?title=Python/Testing/CodeCoverage&amp;action=history"/>
	<updated>2026-05-07T13:38:36Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.39.3</generator>
	<entry>
		<id>https://wiki.brandon-rodriguez.com/index.php?title=Python/Testing/CodeCoverage&amp;diff=656&amp;oldid=prev</id>
		<title>Brodriguez: Create page</title>
		<link rel="alternate" type="text/html" href="https://wiki.brandon-rodriguez.com/index.php?title=Python/Testing/CodeCoverage&amp;diff=656&amp;oldid=prev"/>
		<updated>2022-11-12T02:31:31Z</updated>

		<summary type="html">&lt;p&gt;Create page&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;&lt;br /&gt;
[https://coverage.readthedocs.io/ Python CodeCoverage] is a tool to measure UnitTest coverage for Python code.&lt;br /&gt;
&lt;br /&gt;
It can be very useful for making sure your tests cover all relevant cases.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{ note | Coverage (alone) is not necessarily all-encompassing.&amp;lt;br&amp;gt;&lt;br /&gt;
For example, it&amp;#039;s possible to technically have &amp;quot;100% code coverage&amp;quot; (in that all lines are touched at least once), yet not actually be testing for important edge cases, etc.&amp;lt;br&amp;gt;&lt;br /&gt;
Having said that, high code coverage is one indicator (of many) that you can use to gauge effectiveness of your UnitTests. }}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Installation ===&lt;br /&gt;
Using the Python environment of choice, run:&lt;br /&gt;
&lt;br /&gt;
 pip install coverage&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Running Code Coverage ==&lt;br /&gt;
The base command is:&lt;br /&gt;
 coverage run --source=&amp;#039;&amp;lt;relative_path&amp;gt;&amp;#039; --omit=&amp;#039;&amp;lt;relative_path&amp;gt;&amp;#039; -m &amp;lt;entity_to_run&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Where:&lt;br /&gt;
* {{ ic |entity_to_run}} - Indicates the testing method to execute with.&lt;br /&gt;
** Ex: {{ ic |manage.py test}} or {{ ic |pytest}}.&lt;br /&gt;
* {{ ic |source}} - The path to the directory to recursively check coverage within.&lt;br /&gt;
** Usually, this is the path to project root, such as {{ ic |--source&amp;lt;nowiki&amp;gt;=&amp;lt;/nowiki&amp;gt;&amp;#039;.&amp;#039;}}.&lt;br /&gt;
* {{ ic |omit}} - The path to sub-directories of {{ ic |source}} to exclude.&lt;br /&gt;
** Ex: There&amp;#039;s no point in checkout our environment so we can add {{ ic |--omit&amp;lt;nowiki&amp;gt;=&amp;lt;/nowiki&amp;gt;&amp;#039;.venv/*&amp;#039;}}.&lt;br /&gt;
&lt;br /&gt;
For example, a full example using [[Python/Testing | Pytest]] (to check coverage at project root, and exclude only the {{ ic |venv}} folder):&lt;br /&gt;
&lt;br /&gt;
 coverage run --source=&amp;#039;.&amp;#039; --omit=&amp;#039;.venv/*&amp;#039; -m pytest&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Viewing Coverage Results ==&lt;br /&gt;
&lt;br /&gt;
Running Code Coverage will only generate results to files, not necessarily show them.&lt;br /&gt;
&lt;br /&gt;
View basic results with:&lt;br /&gt;
&lt;br /&gt;
 coverage report&lt;br /&gt;
&lt;br /&gt;
Optional useful args:&lt;br /&gt;
&lt;br /&gt;
* {{ ic |-m}} - Display line numbers of missing coverage for each file.&lt;br /&gt;
* {{ ic |--skip-covered}} Skip output for any files at 100% coverage. Particularly useful for large projects with good coverage already.&lt;br /&gt;
&lt;br /&gt;
Ex:&lt;br /&gt;
&lt;br /&gt;
 coverage report -m --skip-covered&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Generate result as html with:&lt;br /&gt;
&lt;br /&gt;
 coverage html&lt;br /&gt;
&lt;br /&gt;
Optional useful args:&lt;br /&gt;
* {{ ic |-d}} - Allows specifying a directory to generate html files to.&lt;/div&gt;</summary>
		<author><name>Brodriguez</name></author>
	</entry>
</feed>