{"id":3634,"date":"2022-07-04T01:07:37","date_gmt":"2022-07-04T01:07:37","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=3634"},"modified":"2022-07-09T07:34:36","modified_gmt":"2022-07-09T07:34:36","slug":"python-assertalmostequal","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/python-unit-testing\/python-assertalmostequal\/","title":{"rendered":"Python assertAlmostEqual()"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you learn how to use the Python <code>assertAlmostEqual()<\/code> method to test if two values are approximately equal.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-the-python-assertalmostequal-method'>Introduction to the Python assertAlmostEqual() method <a href=\"#introduction-to-the-python-assertalmostequal-method\" class=\"anchor\" id=\"introduction-to-the-python-assertalmostequal-method\" title=\"Anchor for Introduction to the Python assertAlmostEqual() method\">#<\/a><\/h2>\n\n\n\n<p>The <code><code>assertAlmostEqual()<\/code><\/code> is a method of the <code>TestCase<\/code> class of the <code>unittest<\/code> module. The <code><code>assertAlmostEqual()<\/code><\/code> test if two values are approximately equal by doing the following: <\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>First, compute the difference.<\/li><li>Second, round to the given number to decimal places (default 7)<\/li><li>Third, compare the rounded value to zero.<\/li><\/ul>\n\n\n\n<p>The following shows the syntax of the <code>assertAlmostEqual()<\/code> method:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">assertAlmostEqual(first, second, places=<span class=\"hljs-number\">7<\/span>, msg=<span class=\"hljs-literal\">None<\/span>, delta=<span class=\"hljs-literal\">None<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><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>It uses the following check:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">round(first-second, <span class=\"hljs-number\">7<\/span>) == <span class=\"hljs-number\">0<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><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>The method uses places (or decimal places) to round the difference before comparing it to zero. Note that places are not significant digits.<\/p>\n\n\n\n<p>If you pass a <code>delta<\/code> instead of <code>places<\/code> then the difference between first and second must be less or equal to (or greater than) delta.<\/p>\n\n\n\n<p>The <code>assertAlmostEqual()<\/code> method allows you to use either places or delta. If you attempt to pass both arguments, you&#8217;ll get a <code>TypeError<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='python-assertalmostequal-method-example'>Python assertAlmostEqual method example <a href=\"#python-assertalmostequal-method-example\" class=\"anchor\" id=\"python-assertalmostequal-method-example\" title=\"Anchor for Python assertAlmostEqual method example\">#<\/a><\/h2>\n\n\n\n<p>First, define a function <code>area()<\/code> that calculates the area of a circle in the <code>circle.py<\/code> file:<\/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\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">area<\/span><span class=\"hljs-params\">(radius: float)<\/span> -&gt; float:<\/span>\n    <span class=\"hljs-keyword\">return<\/span> math.pi * math.pow(radius, <span class=\"hljs-number\">2<\/span>)<\/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>The <code>area()<\/code> function accepts a radius as a float and returns the area of the circle as a float. <\/p>\n\n\n\n<p>Since Python can only <a href=\"https:\/\/www.pythontutorial.net\/advanced-python\/python-float\/\">represent floats approximately<\/a>, you need to use the <code>assertAlmostEqual()<\/code> method to test the result of the <code>area()<\/code> with another float. <\/p>\n\n\n\n<p>For example, the following test that uses the <code>assertEqual()<\/code> method will fail:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">self.assertEqual(<span class=\"hljs-number\">0.1<\/span>+<span class=\"hljs-number\">0.1<\/span>+<span class=\"hljs-number\">0.1<\/span>, <span class=\"hljs-number\">0.3<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><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>However, the following test that uses the <code>assertAlmostEqual()<\/code> method will pass:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">self.assertAlmostEqual(<span class=\"hljs-number\">0.1<\/span>+<span class=\"hljs-number\">0.1<\/span>+<span class=\"hljs-number\">0.1<\/span>, <span class=\"hljs-number\">0.3<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><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>Second, define a test module <code>test_circle.py<\/code> and import the <code>circle.py<\/code> module:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> unittest\n<span class=\"hljs-keyword\">from<\/span> circle <span class=\"hljs-keyword\">import<\/span> area\n<span class=\"hljs-keyword\">from<\/span> math <span class=\"hljs-keyword\">import<\/span> pi\n\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">TestCircle<\/span><span class=\"hljs-params\">(unittest.TestCase)<\/span>:<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">test_area<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        self.assertAlmostEqual(area(<span class=\"hljs-number\">0<\/span>), <span class=\"hljs-number\">0<\/span>)\n        self.assertAlmostEqual(area(<span class=\"hljs-number\">1<\/span>), pi)\n        self.assertAlmostEqual(area(<span class=\"hljs-number\">0.1<\/span>), pi*<span class=\"hljs-number\">0.1<\/span>*<span class=\"hljs-number\">0.1<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><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>How it works:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>First, define a <code>TestCircle<\/code> class that inherits the <code>TestCase<\/code> class<\/li><li>Second, add the <code>test_area()<\/code> test method to the <code>TestCircle<\/code> class.<\/li><li>Third, use the <code>assertAlmostEqual()<\/code> method to test if the result of the <code>area()<\/code> function is almost equal to <code>0<\/code>, <code>pi<\/code>, and <code>pi * 0.1 * 0.1<\/code> .<\/li><\/ul>\n\n\n\n<p>Third, run the test:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">python -m unittest -v<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><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>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">test_area (test_circle.TestCircle) ... ok\n\n----------------------------------------------------------------------\nRan <span class=\"hljs-number\">1<\/span> test <span class=\"hljs-keyword\">in<\/span> <span class=\"hljs-number\">0.000<\/span>s\n\nOK<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><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<h2 class=\"wp-block-heading\" id='python-assertnotalmostequal-method'>Python assertNotAlmostEqual() method <a href=\"#python-assertnotalmostequal-method\" class=\"anchor\" id=\"python-assertnotalmostequal-method\" title=\"Anchor for Python assertNotAlmostEqual() method\">#<\/a><\/h2>\n\n\n\n<p>The <code>assertNotAlmostEqual()<\/code> method is the opposite of the <code>assertAlmostEqual()<\/code> method. It tests if two values are not approximately equal.<\/p>\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 Python <code>assertAlmostEqual()<\/code> method to test if two values are approximately equal.<\/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=\"3634\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-unit-testing\/python-assertalmostequal\/\"\n\t\t\t\tdata-post-title=\"Python assertAlmostEqual()\"\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=\"3634\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-unit-testing\/python-assertalmostequal\/\"\n\t\t\t\tdata-post-title=\"Python assertAlmostEqual()\"\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 learn how to use the Python assertAlmostEqual() method to test if two values are approximately equal.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":3553,"menu_order":6,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-3634","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/3634","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=3634"}],"version-history":[{"count":0,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/3634\/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=3634"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}