{"id":419,"date":"2024-01-29T11:04:10","date_gmt":"2024-01-29T11:04:10","guid":{"rendered":"https:\/\/learnpython.elegantwallp.com\/?p=419"},"modified":"2024-01-29T11:04:11","modified_gmt":"2024-01-29T11:04:11","slug":"python-assertis","status":"publish","type":"post","link":"https:\/\/learnpython.elegantwallp.com\/2024\/01\/29\/python-assertis\/","title":{"rendered":"Python assertIs()"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you\u2019ll learn how to use the Python&nbsp;<code>assertIs()<\/code>&nbsp;to test if two objects are the same.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to Python assertIs() method<\/h2>\n\n\n\n<p>The\u00a0<code>assertIs()<\/code>\u00a0allows you to test if two objects are the same. The following shows the syntax of the\u00a0<code>assertIs()<\/code>\u00a0method:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>assertIs(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;and&nbsp;<code>second<\/code>&nbsp;reference the same object, the test will pass. Otherwise, it\u2019ll fail.<\/p>\n\n\n\n<p>The&nbsp;<code>msg<\/code>&nbsp;is optional. It\u2019s displayed in the test result in case the test fails.<\/p>\n\n\n\n<p>Technically, the&nbsp;<code>assertIs()<\/code>&nbsp;method uses the&nbsp;<code>is<\/code>&nbsp;operator:<code>first is second<\/code><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Python assertIs() method example<\/h2>\n\n\n\n<p>First, create a\u00a0<code>Logger<\/code>\u00a0singleton class in the\u00a0<code>logger.py<\/code>\u00a0module:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>from datetime import datetime class Logger: _instance = None def __new__(cls): if cls._instance is None: cls._instance = super(Logger, cls).__new__(cls) return cls._instance def log(self, message): print(f'{datetime.now().strftime(\"%Y-%m-%d %H:%M:%S\")} {message}')<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>The singleton is a design pattern that limits the instantiation of a class to a single instance. In other words, you\u2019ll have the same&nbsp;<code>Logger<\/code>&nbsp;object regardless of how many times you call the&nbsp;<code>Logger()<\/code>&nbsp;constructor.<\/p>\n\n\n\n<p>Second, create a\u00a0<code>TestLogger<\/code>\u00a0class that tests the\u00a0<code>Logger<\/code>\u00a0class:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>import unittest from logger import Logger class TestLogger(unittest.TestCase): def setUp(self): self.logger = Logger() def test_singleton(self): new_logger = Logger() self.assertIs(self.logger, new_logger)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>In the&nbsp;<code>TestLogger<\/code>&nbsp;class:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, create a new instance of the&nbsp;<code>Logger<\/code>&nbsp;class in the&nbsp;<code>setUp()<\/code>&nbsp;method and assign it to the&nbsp;<code>self.logger<\/code>&nbsp;instance variable.<\/li>\n\n\n\n<li>Second, create a new instance of the Logger class and use the&nbsp;<code>assertIs()<\/code>&nbsp;method to check if two instances are the same.<\/li>\n<\/ul>\n\n\n\n<p>If you 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>you\u2019ll get the following output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>test_singleton (test_logger.TestLogger) ... ok ---------------------------------------------------------------------- Ran 1 test in 0.000s OK<\/code><small>Code language: plaintext (plaintext)<\/small><\/code><\/pre>\n\n\n\n<p>The test passed.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Python assertIsNot() method<\/h2>\n\n\n\n<p>The\u00a0<code>assertIsNot()<\/code>\u00a0tests if the first object is not the same as the second one:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>assertIsNot(first, second, msg=None)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>import unittest class TestInteger(unittest.TestCase): def test_integer_different_value(self): x = 10 y = 20 self.assertIsNot(x, y) def test_integer_same_value(self): x = 10 y = 10 self.assertIs(x, y)<\/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 -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_integer_different_value (test_integer.TestInteger) ... ok test_integer_same_value (test_integer.TestInteger) ... ok ---------------------------------------------------------------------- Ran 2 tests in 0.001s OK<\/code><small>Code language: plaintext (plaintext)<\/small><\/code><\/pre>\n\n\n\n<p>In this example, we use the&nbsp;<code>assertIsNot()<\/code>&nbsp;method to test if two integer variables reference different objects. Since their values are different, they reference different objects.<\/p>\n\n\n\n<p>In the second test case, we use the&nbsp;<code>assertIs()<\/code>&nbsp;method to test if two integer variables reference the same object. Because their values are the same, they reference the same object.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you\u2019ll learn how to use the Python&nbsp;assertIs()&nbsp;to test if two objects are the same. Introduction to Python assertIs() method The\u00a0assertIs()\u00a0allows you to test if two objects are the same. The following shows the syntax of the\u00a0assertIs()\u00a0method: If the&nbsp;first&nbsp;and&nbsp;second&nbsp;reference the same object, the test will pass. Otherwise, it\u2019ll fail. The&nbsp;msg&nbsp;is optional. It\u2019s [&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-419","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\/419","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=419"}],"version-history":[{"count":1,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/419\/revisions"}],"predecessor-version":[{"id":420,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/419\/revisions\/420"}],"wp:attachment":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/media?parent=419"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/categories?post=419"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/tags?post=419"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}