{"id":427,"date":"2024-01-29T11:10:50","date_gmt":"2024-01-29T11:10:50","guid":{"rendered":"https:\/\/learnpython.elegantwallp.com\/?p=427"},"modified":"2024-01-29T11:10:52","modified_gmt":"2024-01-29T11:10:52","slug":"python-assertin","status":"publish","type":"post","link":"https:\/\/learnpython.elegantwallp.com\/2024\/01\/29\/python-assertin\/","title":{"rendered":"Python assertIn()"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you\u2019ll learn how to use the Python&nbsp;<code>assertIn()<\/code>&nbsp;method to test if a member is in a container.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to the Python assertIn() method<\/h2>\n\n\n\n<p>The\u00a0<code>assertIn()<\/code>\u00a0is a method of the\u00a0<code>TestCase<\/code>\u00a0class of the\u00a0unittest\u00a0module. The\u00a0<code>assertIn()<\/code>\u00a0method tests if a member is in a container:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>assertIn(member, container, msg=None)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>If the member is in the container, the test will pass. Otherwise, it\u2019ll fail. The&nbsp;<code>msg<\/code>&nbsp;is optional. It\u2019ll be displayed in the test result when the test fails.<\/p>\n\n\n\n<p>Internally, the\u00a0<code>assertIn()<\/code>\u00a0method uses the\u00a0<code>in<\/code>\u00a0operator to check:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>member in container<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Python assertIn() method examples<\/h2>\n\n\n\n<p>The following example uses the\u00a0<code>assertIn()<\/code>\u00a0method to test if a number is in a\u00a0list\u00a0and a string is in another string:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>import unittest class TestIn(unittest.TestCase): def test_in_list(self): self.assertIn(1, &#91;1, 2, 3]) def test_in_string(self): self.assertIn('python', 'python tutorial')<\/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_in_list (test_in.TestIn) ... ok test_in_string (test_in.TestIn) ... ok ---------------------------------------------------------------------- Ran 2 tests in 0.001s OK<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Python assertNotIn() method<\/h2>\n\n\n\n<p>The\u00a0<code>assertNotIn()<\/code>\u00a0method is the opposite of the\u00a0<code>assertIn()<\/code>\u00a0method. The\u00a0<code>assertNotIn()<\/code>\u00a0method tests if a member is not in a container:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>assertNotIn(member, container, 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 TestNotIn(unittest.TestCase): def test_not_in_list(self): self.assertNotIn(0, &#91;1, 2, 3]) def test_not_in_string(self): self.assertNotIn('java', 'python tutorial')<\/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_not_in_list (test_not_in.TestNotIn) ... ok test_not_in_string (test_not_in.TestNotIn) ... ok ---------------------------------------------------------------------- Ran 2 tests in 0.000s OK<\/code><\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you\u2019ll learn how to use the Python&nbsp;assertIn()&nbsp;method to test if a member is in a container. Introduction to the Python assertIn() method The\u00a0assertIn()\u00a0is a method of the\u00a0TestCase\u00a0class of the\u00a0unittest\u00a0module. The\u00a0assertIn()\u00a0method tests if a member is in a container: If the member is in the container, the test will pass. Otherwise, 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-427","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\/427","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=427"}],"version-history":[{"count":1,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/427\/revisions"}],"predecessor-version":[{"id":428,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/427\/revisions\/428"}],"wp:attachment":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/media?parent=427"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/categories?post=427"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/tags?post=427"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}