{"id":421,"date":"2024-01-29T11:06:25","date_gmt":"2024-01-29T11:06:25","guid":{"rendered":"https:\/\/learnpython.elegantwallp.com\/?p=421"},"modified":"2024-01-29T11:06:26","modified_gmt":"2024-01-29T11:06:26","slug":"python-assertisinstance","status":"publish","type":"post","link":"https:\/\/learnpython.elegantwallp.com\/2024\/01\/29\/python-assertisinstance\/","title":{"rendered":"Python assertIsInstance()"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you\u2019ll learn how to use Python&nbsp;<code>assertIsInstance()<\/code>&nbsp;method to test if an object is an instance of a class.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to the Python assertIsInstance() method<\/h2>\n\n\n\n<p>The\u00a0<code>assertIsInstance()<\/code>\u00a0is a method of the\u00a0<code>TestCase<\/code>\u00a0class of the\u00a0unittest\u00a0module. The\u00a0<code>assertIsInstance()<\/code>\u00a0method tests if an object is an instance of a\u00a0class.<\/p>\n\n\n\n<p>The following shows the syntax of the\u00a0<code>assertIsInstance()<\/code>\u00a0method:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>assertIsInstance(obj, cls, msg=None)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>In this syntax:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>obj<\/code>&nbsp;is the object to test.<\/li>\n\n\n\n<li><code>cls<\/code>\u00a0is a class or a\u00a0tuple\u00a0of classes.<\/li>\n\n\n\n<li><code>msg<\/code>&nbsp;is an optional string that will be displayed if the&nbsp;<code>obj<\/code>&nbsp;is not an instance of the&nbsp;<code>cls<\/code>&nbsp;class.<\/li>\n<\/ul>\n\n\n\n<p>Internally, the&nbsp;<code>assertIsInstance()<\/code>&nbsp;uses the&nbsp;<code>isinstance()<\/code>&nbsp;function to check if the object is an instance of the&nbsp;<code>cls<\/code>&nbsp;class.<\/p>\n\n\n\n<p>If the&nbsp;<code>cls<\/code>&nbsp;is not the class of the obj but the base class of the class of the obj, the test will also pass.<\/p>\n\n\n\n<p>Since the object class is the base class of all classes, the&nbsp;<code>assertIsInstance(obj, object)<\/code>&nbsp;will always pass.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Python assertIsInstance() method examples<\/h2>\n\n\n\n<p>Let\u2019s create a\u00a0<code>shape.py<\/code>\u00a0module with two classes\u00a0<code>Shape<\/code>\u00a0and\u00a0<code>Square<\/code>. The\u00a0<code>Shape<\/code>\u00a0class is the base class of the\u00a0<code>Square<\/code>\u00a0class:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>class Shape: pass class Square(Shape): pass<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>To make it simple, the&nbsp;<code>Shape<\/code>&nbsp;and&nbsp;<code>Square<\/code>&nbsp;classes have no implementation.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1) Using the Python assertIsInstance() method example<\/h3>\n\n\n\n<p>The following example uses the\u00a0<code>assertIsInstance()<\/code>\u00a0method to test if the\u00a0<code>square<\/code>\u00a0object is an instance of the\u00a0<code>Square<\/code>\u00a0class:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>import unittest from shape import Shape, Square class TestShape(unittest.TestCase): def setUp(self): self.square = Square() def test_is_instance(self): self.assertIsInstance(self.square, Square)<\/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_is_instance (test_shape.TestShape) ... ok ---------------------------------------------------------------------- Ran 1 test in 0.001s OK<\/code><small>Code language: plaintext (plaintext)<\/small><\/code><\/pre>\n\n\n\n<p>Because the&nbsp;<code>square<\/code>&nbsp;instance variable is an object of the&nbsp;<code>Square<\/code>&nbsp;class, the test passes.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2) Using the Python assertIsInstance() method with a base class example<\/h3>\n\n\n\n<p>The following example uses the\u00a0<code>assertIsInstance()<\/code>\u00a0method to test if the\u00a0<code>square<\/code>\u00a0is an instance of the\u00a0<code>Shape<\/code>\u00a0class:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>import unittest from shape import Shape, Square class TestShape(unittest.TestCase): def setUp(self): self.square = Square() def test_is_instance(self): self.assertIsInstance(self.square, Square) def test_is_instance_of_parent_class(self): self.assertIsInstance(self.square, Shape)<\/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_is_instance (test_shape.TestShape) ... ok test_is_instance_of_parent_class (test_shape.TestShape) ... ok ---------------------------------------------------------------------- Ran 2 tests in 0.001s OK<\/code><small>Code language: plaintext (plaintext)<\/small><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3) Using the Python assertIsInstance() method to test if an object is an instance of the object class<\/h3>\n\n\n\n<p>The following example uses\u00a0<code>assertIsInstance()<\/code>\u00a0method to test if the\u00a0<code>square<\/code>\u00a0instance variable is an instance of the\u00a0<code>object<\/code>\u00a0class:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>import unittest from shape import Shape, Square class TestShape(unittest.TestCase): def setUp(self): self.square = Square() def test_is_instance(self): self.assertIsInstance(self.square, Square) def test_is_instance_of_parent_class(self): self.assertIsInstance(self.square, Shape) def test_is_instance_of_object(self): self.assertIsInstance(self.square, object)<\/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_is_instance (test_shape.TestShape) ... ok test_is_instance_of_object (test_shape.TestShape) ... ok test_is_instance_of_parent_class (test_shape.TestShape) ... ok ---------------------------------------------------------------------- Ran 3 tests in 0.001s OK<\/code><small>Code language: plaintext (plaintext)<\/small><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Python assertIsNotInstance() method<\/h2>\n\n\n\n<p>The\u00a0<code>assertIsNotInstance()<\/code>\u00a0is the opposite of the\u00a0<code>assertIsInstance()<\/code>\u00a0method. It tests if an object is not an instance of a class:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>assertNotIsInstance(obj, cls, 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 from shape import Shape, Square class TestShape(unittest.TestCase): def setUp(self): self.square = Square() def test_is_instance(self): self.assertIsInstance(self.square, Square) def test_is_instance_of_parent_class(self): self.assertIsInstance(self.square, Shape) def test_is_instance_of_object(self): self.assertIsInstance(self.square, object) def test_is_not_instance(self): shape = Shape() self.assertNotIsInstance(shape, Square)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>In this example, the&nbsp;<code>test_is_not_instance()<\/code>&nbsp;method uses the&nbsp;<code>assertIsNotInstance()<\/code>&nbsp;method to test if a&nbsp;<code>Shape<\/code>&nbsp;object is an instance of the&nbsp;<code>Square<\/code>&nbsp;class.<\/p>\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_is_instance (test_shape.TestShape) ... ok test_is_instance_of_object (test_shape.TestShape) ... ok test_is_instance_of_parent_class (test_shape.TestShape) ... ok test_is_not_instance (test_shape.TestShape) ... ok ---------------------------------------------------------------------- Ran 4 tests in 0.002s OK<\/code><\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you\u2019ll learn how to use Python&nbsp;assertIsInstance()&nbsp;method to test if an object is an instance of a class. Introduction to the Python assertIsInstance() method The\u00a0assertIsInstance()\u00a0is a method of the\u00a0TestCase\u00a0class of the\u00a0unittest\u00a0module. The\u00a0assertIsInstance()\u00a0method tests if an object is an instance of a\u00a0class. The following shows the syntax of the\u00a0assertIsInstance()\u00a0method: In this syntax: Internally, [&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-421","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\/421","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=421"}],"version-history":[{"count":1,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/421\/revisions"}],"predecessor-version":[{"id":422,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/421\/revisions\/422"}],"wp:attachment":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/media?parent=421"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/categories?post=421"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/tags?post=421"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}