{"id":3690,"date":"2022-07-06T01:59:35","date_gmt":"2022-07-06T01:59:35","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=3690"},"modified":"2022-07-09T07:47:20","modified_gmt":"2022-07-09T07:47:20","slug":"python-unittest-coverage","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/python-unit-testing\/python-unittest-coverage\/","title":{"rendered":"Python unittest Coverage"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn how to use the Python unittest coverage command to generate a test coverage report.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='what-is-a-test-coverage'>What is a test coverage <a href=\"#what-is-a-test-coverage\" class=\"anchor\" id=\"what-is-a-test-coverage\" title=\"Anchor for What is a test coverage\">#<\/a><\/h2>\n\n\n\n<p>Test coverage is a ratio between the number of lines executed by at least one test case and the total number of lines of the code base:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">test coverage = lines of code executed \/ total number of lines<\/code><\/span><\/pre>\n\n\n<p>The test coverage is also known as code coverage. <\/p>\n\n\n\n<p>The test coverage is often used to assess the quality of a test suite. If the test coverage is low e.g., 5%, it is an indicator that you&#8217;re not testing enough. <\/p>\n\n\n\n<p>However, the reverse may not be true. For example, 100% test coverage is not a guarantee that you have a good test suite. In other words, a test suite with high coverage can still be of poor quality.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='unittest-coverage-example'>Unittest coverage example <a href=\"#unittest-coverage-example\" class=\"anchor\" id=\"unittest-coverage-example\" title=\"Anchor for Unittest coverage example\">#<\/a><\/h2>\n\n\n\n<p>We&#8217;ll use the following project structure to demo the <code>unittest<\/code> coverage. Note that you can get the source code from <a href=\"https:\/\/www.pythontutorial.net\/python-unit-testing\/python-run-unittest\/\" target=\"_blank\" rel=\"noreferrer noopener\">this tutorial<\/a>.<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">D:\\python-unit-testing\n\u251c\u2500\u2500 shapes\n|  \u251c\u2500\u2500 circle.py\n|  \u251c\u2500\u2500 shape.py\n|  \u2514\u2500\u2500 square.py\n\u2514\u2500\u2500 test\n   \u251c\u2500\u2500 test_circle.py\n   \u251c\u2500\u2500 test_square.py\n   \u2514\u2500\u2500 __init__.py<\/code><\/span><\/pre>\n\n\n<p>To generate a coverage report, you need to carry out two steps:<\/p>\n\n\n\n<p>First, run the coverage module to generate the coverage data:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">python -m coverage run -m unittest<\/code><\/span><\/pre>\n\n\n<p>Second, turn the coverage data into a report:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">python -m coverage report<\/code><\/span><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\">Name                  Stmts   Miss  Cover\n-----------------------------------------\nshapes\\circle.py          9      0   100%\nshapes\\shape.py           4      0   100%\nshapes\\square.py          9      0   100%\ntest\\__init__.py          0      0   100%\ntest\\test_circle.py      14      0   100%\ntest\\test_square.py      14      0   100%\n-----------------------------------------\nTOTAL                    50      0   100%<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">plaintext<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">plaintext<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>To generate the coverage report in HTML format, you change the option of the coverage module to HTML like this:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">python -m coverage html <\/code><\/span><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-selector-tag\">Wrote<\/span> <span class=\"hljs-selector-tag\">HTML<\/span> <span class=\"hljs-selector-tag\">report<\/span> <span class=\"hljs-selector-tag\">to<\/span> <span class=\"hljs-selector-tag\">htmlcov<\/span>\\<span class=\"hljs-selector-tag\">index<\/span><span class=\"hljs-selector-class\">.html<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The output indicates the location of the HTML coverage report <code>htmlcov\\index.html<\/code> under the project folder. <\/p>\n\n\n\n<p>If you open the index.html file of the <code>htmlcov<\/code> folder, it&#8217;ll look like the following:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"773\" height=\"435\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/07\/python-unittest-coverage.png\" alt=\"python unittest coverage\" class=\"wp-image-3693\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/07\/python-unittest-coverage.png 773w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/07\/python-unittest-coverage-300x169.png 300w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/07\/python-unittest-coverage-768x432.png 768w\" sizes=\"auto, (max-width: 773px) 100vw, 773px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id='examining-unittest-coverage-detailed-report'>Examining unittest coverage detailed report <a href=\"#examining-unittest-coverage-detailed-report\" class=\"anchor\" id=\"examining-unittest-coverage-detailed-report\" title=\"Anchor for Examining unittest coverage detailed report\">#<\/a><\/h2>\n\n\n\n<p>First, add the <code>perimeter()<\/code> method to the <code>Circle<\/code> class as follows:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> math\n<span class=\"hljs-keyword\">from<\/span> .shape <span class=\"hljs-keyword\">import<\/span> Shape\n\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Circle<\/span><span class=\"hljs-params\">(Shape)<\/span>:<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self, radius: float)<\/span> -&gt; <span class=\"hljs-keyword\">None<\/span>:<\/span>\n        <span class=\"hljs-keyword\">if<\/span> radius &lt; <span class=\"hljs-number\">0<\/span>:\n            <span class=\"hljs-keyword\">raise<\/span> ValueError(<span class=\"hljs-string\">'The radius cannot be negative'<\/span>)\n\n        self._radius = radius\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">area<\/span><span class=\"hljs-params\">(self)<\/span> -&gt; float:<\/span>\n        <span class=\"hljs-keyword\">return<\/span> math.pi * math.pow(self._radius, <span class=\"hljs-number\">2<\/span>)\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">perimeter<\/span><span class=\"hljs-params\">(self)<\/span> -&gt; float:<\/span>\n        <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-number\">2<\/span> * math.pi * self._radius\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Next, gather the coverage data by running the following command:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">python -m coverage run -m unittest<\/code><\/span><\/pre>\n\n\n<p>Then, generate the coverage report by executing the following command:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">python -m coverage report<\/code><\/span><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\">Name                  Stmts   Miss  Cover\n-----------------------------------------\nshapes\\circle.py         11      1    91%\nshapes\\shape.py           4      0   100%\nshapes\\square.py          9      0   100%\ntest\\__init__.py          0      0   100%\ntest\\test_circle.py      14      0   100%\ntest\\test_square.py      14      0   100%\n-----------------------------------------\nTOTAL                    52      1    98%<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">plaintext<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">plaintext<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The coverage now is 98% in total and 91% in the <code>shape\\circle.py<\/code> module. This is because the <code>perimeter()<\/code> method is not tested.<\/p>\n\n\n\n<p>After that, generate the coverage report in HTML format:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">python -m coverage html  <\/code><\/span><\/pre>\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"765\" height=\"427\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/07\/python-unittest-coverage-example.png\" alt=\"\" class=\"wp-image-3697\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/07\/python-unittest-coverage-example.png 765w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/07\/python-unittest-coverage-example-300x167.png 300w\" sizes=\"auto, (max-width: 765px) 100vw, 765px\" \/><\/figure>\n\n\n\n<p>The <code>circle.py<\/code> has 11 statements. The test executes 10 of them and misses one statement. Therefore, the test coverage is 10\/11 ~ 91%.<\/p>\n\n\n\n<p>Finally, click the <code>circle.py<\/code> module for the detailed report:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"783\" height=\"631\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/07\/python-unittest-coverage-detail.png\" alt=\"\" class=\"wp-image-3698\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/07\/python-unittest-coverage-detail.png 783w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/07\/python-unittest-coverage-detail-300x242.png 300w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/07\/python-unittest-coverage-detail-768x619.png 768w\" sizes=\"auto, (max-width: 783px) 100vw, 783px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>Use the <code>python -m coverage run -m unittest<\/code> command to gather coverage data and the <code>python -m coverage report<\/code> command to generate a coverage report.<\/li><li>Use the <code>python -m coverage html<\/code> to generate the test coverage report in HTML format.<\/li><\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"3690\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-unit-testing\/python-unittest-coverage\/\"\n\t\t\t\tdata-post-title=\"Python unittest Coverage\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"3690\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-unit-testing\/python-unittest-coverage\/\"\n\t\t\t\tdata-post-title=\"Python unittest Coverage\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\t\t\t<input type=\"button\" name=\"wth-submit\" class=\"wth-btn wth-btn-submit\" id=\"wth-submit\" \/>\n\t\t\t<input type=\"button\" class=\"wth-btn wth-btn-cancel\" value=\"Cancel\" \/>\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you&#8217;ll learn how to use the Python unittest coverage command to generate a test coverage report.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":3553,"menu_order":17,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-3690","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/3690","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/comments?post=3690"}],"version-history":[{"count":0,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/3690\/revisions"}],"up":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/3553"}],"wp:attachment":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/media?parent=3690"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}