{"id":415,"date":"2024-01-29T10:59:00","date_gmt":"2024-01-29T10:59:00","guid":{"rendered":"https:\/\/learnpython.elegantwallp.com\/?p=415"},"modified":"2024-01-29T10:59:00","modified_gmt":"2024-01-29T10:59:00","slug":"python-assertequal","status":"publish","type":"post","link":"https:\/\/learnpython.elegantwallp.com\/2024\/01\/29\/python-assertequal\/","title":{"rendered":"Python assertEqual"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you\u2019ll learn how to use the Python&nbsp;<code>assertEqual()<\/code>&nbsp;method to test if two values are equal.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to the Python assertEqual() method<\/h2>\n\n\n\n<p>The\u00a0<code>assertEqual()<\/code>\u00a0is a method of the TestCase class of the\u00a0unittest\u00a0module. The\u00a0<code>assertEqual()<\/code>\u00a0tests if two values are equal:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>assertEqual(first, second, msg=None)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>If the&nbsp;<code>first<\/code>&nbsp;value does not equal the&nbsp;<code>second<\/code>&nbsp;value, the test will fail.<\/p>\n\n\n\n<p>The&nbsp;<code>msg<\/code>&nbsp;is optional. If the&nbsp;<code>msg<\/code>&nbsp;is provided, it\u2019ll be shown on the test result if the test fails.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Python assertEqual() method example<\/h2>\n\n\n\n<p>First, create a new module called\u00a0<code>main.py<\/code>\u00a0and define the\u00a0<code>add()<\/code>\u00a0function:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>def add(a, b): return a + b<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Second, create a test module\u00a0<code>test_main.py<\/code>\u00a0to test the\u00a0<code>add()<\/code>\u00a0function:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>import unittest from main import add class TestMain(unittest.TestCase): def test_add(self): self.assertEqual(add(1, 2), 3)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Third, run the test:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>python -m unittest test_main.py -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_add (test_main.TestMain) ... 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 assertNotEqual() method<\/h2>\n\n\n\n<p>The\u00a0<code>assertNotEqual()<\/code>\u00a0method tests if two values are not equal:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>assertNotEqual(first, second, msg=None)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>If the first is equal to the second, the test will fail. Otherwise, it\u2019ll pass. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>import unittest from main import add class TestMain(unittest.TestCase): def test_add(self): self.assertEqual(add(1, 2), 3) def test_add_floats(self): self.assertNotEqual(add(0.2, 0.1), 0.3)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Run the test:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>python -m unittest test_main.py -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_add (test_main.TestMain) ... ok test_add_floats (test_main.TestMain) ... ok ---------------------------------------------------------------------- Ran 2 tests in 0.001s OK<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Since 0.2 + 0.1 returns 0.30000000000000004, it\u2019s not equal to 0.3. Therefore, the following test passes:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>self.assertNotEqual(add(0.2, 0.1), 0.3)<\/code><\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you\u2019ll learn how to use the Python&nbsp;assertEqual()&nbsp;method to test if two values are equal. Introduction to the Python assertEqual() method The\u00a0assertEqual()\u00a0is a method of the TestCase class of the\u00a0unittest\u00a0module. The\u00a0assertEqual()\u00a0tests if two values are equal: If the&nbsp;first&nbsp;value does not equal the&nbsp;second&nbsp;value, the test will fail. The&nbsp;msg&nbsp;is optional. If the&nbsp;msg&nbsp;is provided, it\u2019ll [&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-415","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\/415","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=415"}],"version-history":[{"count":1,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/415\/revisions"}],"predecessor-version":[{"id":416,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/415\/revisions\/416"}],"wp:attachment":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/media?parent=415"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/categories?post=415"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/tags?post=415"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}