{"id":417,"date":"2024-01-29T11:00:36","date_gmt":"2024-01-29T11:00:36","guid":{"rendered":"https:\/\/learnpython.elegantwallp.com\/?p=417"},"modified":"2024-01-29T11:00:37","modified_gmt":"2024-01-29T11:00:37","slug":"python-assertalmostequal","status":"publish","type":"post","link":"https:\/\/learnpython.elegantwallp.com\/2024\/01\/29\/python-assertalmostequal\/","title":{"rendered":"Python assertAlmostEqual()"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you learn how to use the Python&nbsp;<code>assertAlmostEqual()<\/code>&nbsp;method to test if two values are approximately equal.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to the Python assertAlmostEqual() method<\/h2>\n\n\n\n<p>The&nbsp;<code>assertAlmostEqual()<\/code>&nbsp;is a method of the&nbsp;<code>TestCase<\/code>&nbsp;class of the&nbsp;<code>unittest<\/code>&nbsp;module. The&nbsp;<code>assertAlmostEqual()<\/code>&nbsp;test if two values are approximately equal by doing the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, compute the difference.<\/li>\n\n\n\n<li>Second, round to the given number to decimal places (default 7)<\/li>\n\n\n\n<li>Third, compare the rounded value to zero.<\/li>\n<\/ul>\n\n\n\n<p>The following shows the syntax of the\u00a0<code>assertAlmostEqual()<\/code>\u00a0method:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>assertAlmostEqual(first, second, places=7, msg=None, delta=None)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>It uses the following check:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>round(first-second, 7) == 0<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\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&nbsp;<code>delta<\/code>&nbsp;instead of&nbsp;<code>places<\/code>&nbsp;then the difference between first and second must be less or equal to (or greater than) delta.<\/p>\n\n\n\n<p>The&nbsp;<code>assertAlmostEqual()<\/code>&nbsp;method allows you to use either places or delta. If you attempt to pass both arguments, you\u2019ll get a&nbsp;<code>TypeError<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Python assertAlmostEqual method example<\/h2>\n\n\n\n<p>First, define a function\u00a0<code>area()<\/code>\u00a0that calculates the area of a circle in the\u00a0<code>circle.py<\/code>\u00a0file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>import math def area(radius: float) -> float: return math.pi * math.pow(radius, 2)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>The&nbsp;<code>area()<\/code>&nbsp;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\u00a0represent floats approximately, you need to use the\u00a0<code>assertAlmostEqual()<\/code>\u00a0method to test the result of the\u00a0<code>area()<\/code>\u00a0with another float.<\/p>\n\n\n\n<p>For example, the following test that uses the\u00a0<code>assertEqual()<\/code>\u00a0method will fail:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>self.assertEqual(0.1+0.1+0.1, 0.3)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>However, the following test that uses the\u00a0<code>assertAlmostEqual()<\/code>\u00a0method will pass:<\/p>\n\n\n\n<p><code>self.assertAlmostEqual(0.1+0.1+0.1, 0.3)<\/code><small>Code language: Python (python)<\/small><\/p>\n\n\n\n<p>Second, define a test module\u00a0<code>test_circle.py<\/code>\u00a0and import the\u00a0<code>circle.py<\/code>\u00a0module:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>import unittest from circle import area from math import pi class TestCircle(unittest.TestCase): def test_area(self): self.assertAlmostEqual(area(0), 0) self.assertAlmostEqual(area(1), pi) self.assertAlmostEqual(area(0.1), pi*0.1*0.1)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>How it works:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, define a&nbsp;<code>TestCircle<\/code>&nbsp;class that inherits the&nbsp;<code>TestCase<\/code>&nbsp;class<\/li>\n\n\n\n<li>Second, add the&nbsp;<code>test_area()<\/code>&nbsp;test method to the&nbsp;<code>TestCircle<\/code>&nbsp;class.<\/li>\n\n\n\n<li>Third, use the&nbsp;<code>assertAlmostEqual()<\/code>&nbsp;method to test if the result of the&nbsp;<code>area()<\/code>&nbsp;function is almost equal to&nbsp;<code>0<\/code>,&nbsp;<code>pi<\/code>, and&nbsp;<code>pi * 0.1 * 0.1<\/code>&nbsp;.<\/li>\n<\/ul>\n\n\n\n<p>Third, run the test:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>python -m unittest -v<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>test_area (test_circle.TestCircle) ... ok ---------------------------------------------------------------------- Ran 1 test in 0.000s OK<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Python assertNotAlmostEqual() method<\/h2>\n\n\n\n<p>The&nbsp;<code>assertNotAlmostEqual()<\/code>&nbsp;method is the opposite of the&nbsp;<code>assertAlmostEqual()<\/code>&nbsp;method. It tests if two values are not approximately equal.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you learn how to use the Python&nbsp;assertAlmostEqual()&nbsp;method to test if two values are approximately equal. Introduction to the Python assertAlmostEqual() method The&nbsp;assertAlmostEqual()&nbsp;is a method of the&nbsp;TestCase&nbsp;class of the&nbsp;unittest&nbsp;module. The&nbsp;assertAlmostEqual()&nbsp;test if two values are approximately equal by doing the following: The following shows the syntax of the\u00a0assertAlmostEqual()\u00a0method: It uses the following check: [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43],"tags":[],"class_list":["post-417","post","type-post","status-publish","format-standard","hentry","category-3-python-unit-testing"],"_links":{"self":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/417","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/comments?post=417"}],"version-history":[{"count":1,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/417\/revisions"}],"predecessor-version":[{"id":418,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/417\/revisions\/418"}],"wp:attachment":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/media?parent=417"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/categories?post=417"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/tags?post=417"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}