{"id":3582,"date":"2022-07-01T04:28:10","date_gmt":"2022-07-01T04:28:10","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=3582"},"modified":"2022-07-09T07:36:39","modified_gmt":"2022-07-09T07:36:39","slug":"python-assertis","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/python-unit-testing\/python-assertis\/","title":{"rendered":"Python assertIs()"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn how to use the Python <code>assertIs()<\/code> to test if two objects are the same.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-python-assertis-method'>Introduction to Python assertIs() method <a href=\"#introduction-to-python-assertis-method\" class=\"anchor\" id=\"introduction-to-python-assertis-method\" title=\"Anchor for Introduction to Python assertIs() method\">#<\/a><\/h2>\n\n\n\n<p>The <code><code>assertIs()<\/code><\/code> allows you to test if two objects are the same. The following shows the syntax of the <code><code>assertIs()<\/code><\/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\">assertIs(first, second, msg=<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>If the <code>first<\/code> and <code>second<\/code> reference the same object, the test will pass. Otherwise, it&#8217;ll fail. <\/p>\n\n\n\n<p>The <code>msg<\/code> is optional. It&#8217;s displayed in the test result in case the test fails.<\/p>\n\n\n\n<p>Technically, the <code>assertIs()<\/code> method uses the <code>is<\/code> operator:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">first is second<\/code><\/span><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='python-assertis-method-example'>Python assertIs() method example <a href=\"#python-assertis-method-example\" class=\"anchor\" id=\"python-assertis-method-example\" title=\"Anchor for Python assertIs() method example\">#<\/a><\/h2>\n\n\n\n<p>First, create a <code>Logger<\/code> singleton class in the <code>logger.py<\/code> module:<\/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\"><span class=\"hljs-keyword\">from<\/span> datetime <span class=\"hljs-keyword\">import<\/span> datetime\n\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Logger<\/span>:<\/span>\n    _instance = <span class=\"hljs-literal\">None<\/span>\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__new__<\/span><span class=\"hljs-params\">(cls)<\/span>:<\/span>\n        <span class=\"hljs-keyword\">if<\/span> cls._instance <span class=\"hljs-keyword\">is<\/span> <span class=\"hljs-literal\">None<\/span>:\n            cls._instance = super(Logger, cls).__new__(cls)\n        <span class=\"hljs-keyword\">return<\/span> cls._instance\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">log<\/span><span class=\"hljs-params\">(self, message)<\/span>:<\/span>\n        print(<span class=\"hljs-string\">f'<span class=\"hljs-subst\">{datetime.now().strftime(<span class=\"hljs-string\">\"%Y-%m-%d %H:%M:%S\"<\/span>)}<\/span> <span class=\"hljs-subst\">{message}<\/span>'<\/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 singleton is a design pattern that limits the instantiation of a class to a single instance. In other words, you&#8217;ll have the same <code>Logger<\/code> object regardless of how many times you call the <code>Logger()<\/code> constructor.<\/p>\n\n\n\n<p>Second, create a <code>TestLogger<\/code> class that tests the <code>Logger<\/code> class:<\/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> unittest\n\n<span class=\"hljs-keyword\">from<\/span> logger <span class=\"hljs-keyword\">import<\/span> Logger\n\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">TestLogger<\/span><span class=\"hljs-params\">(unittest.TestCase)<\/span>:<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">setUp<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        self.logger = Logger()\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">test_singleton<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        new_logger = Logger()\n        self.assertIs(self.logger, new_logger)<\/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>In the <code>TestLogger<\/code> class:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>First, create a new instance of the <code>Logger<\/code> class in the <code>setUp()<\/code> method and assign it to the <code>self.logger<\/code> instance variable.<\/li><li>Second, create a new instance of the Logger class and use the <code>assertIs()<\/code> method to check if two instances are the same.<\/li><\/ul>\n\n\n\n<p>If you run the test:<\/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\">python -m unittest -v<\/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>you&#8217;ll get the following output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\">test_singleton (test_logger.TestLogger) ... ok\n\n----------------------------------------------------------------------\nRan 1 test in 0.000s\n\nOK<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><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 test passed.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='python-assertisnot-method'>Python assertIsNot() method <a href=\"#python-assertisnot-method\" class=\"anchor\" id=\"python-assertisnot-method\" title=\"Anchor for Python assertIsNot() method\">#<\/a><\/h2>\n\n\n\n<p>The <code>assertIsNot()<\/code> tests if the first object is not the same as the second one:<\/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\">assertIsNot(first, second, msg=<span class=\"hljs-literal\">None<\/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>For example:<\/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\"><span class=\"hljs-keyword\">import<\/span> unittest\n\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">TestInteger<\/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_integer_different_value<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        x = <span class=\"hljs-number\">10<\/span>\n        y = <span class=\"hljs-number\">20<\/span>\n        self.assertIsNot(x, y)\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">test_integer_same_value<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        x = <span class=\"hljs-number\">10<\/span>\n        y = <span class=\"hljs-number\">10<\/span>\n        self.assertIs(x, y)<\/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>Run the test:<\/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\">python -m unittest -v<\/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<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\">test_integer_different_value (test_integer.TestInteger) ... ok\ntest_integer_same_value (test_integer.TestInteger) ... ok\n\n----------------------------------------------------------------------\nRan 2 tests in 0.001s\n\nOK<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><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>In this example, we use the <code>assertIsNot()<\/code> 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 <code>assertIs()<\/code> method to test if two integer variables reference the same object. Because their values are the same, they reference the same object.<\/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 <code>assertIs()<\/code> method to test if two objects are the same.<\/li><li>Use the <code>assertIsNot()<\/code> method to test if two variables reference different objects.<\/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=\"3582\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-unit-testing\/python-assertis\/\"\n\t\t\t\tdata-post-title=\"Python assertIs()\"\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=\"3582\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-unit-testing\/python-assertis\/\"\n\t\t\t\tdata-post-title=\"Python assertIs()\"\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>Summary: in this tutorial, you&#8217;ll learn how to use the Python assertIs() to test if two objects are the same. Introduction to Python assertIs() method # The assertIs() allows you to test if two objects are the same. The following shows the syntax of the assertIs() method: If the first and second reference the same [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":3553,"menu_order":7,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-3582","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/3582","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=3582"}],"version-history":[{"count":0,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/3582\/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=3582"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}