{"id":423,"date":"2024-01-29T11:08:00","date_gmt":"2024-01-29T11:08:00","guid":{"rendered":"https:\/\/learnpython.elegantwallp.com\/?p=423"},"modified":"2024-01-29T11:08:02","modified_gmt":"2024-01-29T11:08:02","slug":"python-assertisnone","status":"publish","type":"post","link":"https:\/\/learnpython.elegantwallp.com\/2024\/01\/29\/python-assertisnone\/","title":{"rendered":"Python assertIsNone()"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you\u2019ll learn how to use the Python&nbsp;<code>assertIsNone()<\/code>&nbsp;method to test if an expression is&nbsp;<code>None<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to the Python assertIsNone() method<\/h2>\n\n\n\n<p>The\u00a0<code>assertIsNone()<\/code>\u00a0is a method of the\u00a0<code>TestCase<\/code>\u00a0class of the\u00a0unittest\u00a0module. The\u00a0<code>assertIsNone()<\/code>\u00a0test if an expression is\u00a0None:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>assertIsNone(expr, msg=None)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>If the&nbsp;<code>expr<\/code>&nbsp;is&nbsp;<code>None<\/code>, the test passes. Otherwise, the test will fail.<\/p>\n\n\n\n<p>The&nbsp;<code>msg<\/code>&nbsp;is optional. It\u2019ll be displayed in the test result if the test fails.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Python assertIsNone() method examples<\/h2>\n\n\n\n<p>Let\u2019s take some examples of using the&nbsp;<code>assertIsNone()<\/code>&nbsp;method.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1) Using assertIsNone() with a success case<\/h3>\n\n\n\n<p>The following example uses the\u00a0<code>assertIsNone()<\/code>\u00a0to test if the message variable is None:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>import unittest class TestNone(unittest.TestCase): def test_variable_none(self): message = None self.assertIsNone(message)<\/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_variable_none (test_none.TestNone) ... ok ---------------------------------------------------------------------- Ran 1 test in 0.000s<\/code><small>Code language: plaintext (plaintext)<\/small><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">2) Using assertIsNone() with a failed case<\/h3>\n\n\n\n<p>The following example uses the\u00a0<code>assertIsNone()<\/code>\u00a0method to test if the\u00a0<code>message<\/code>\u00a0variable is None:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>import unittest class TestNone(unittest.TestCase): def test_variable_not_none(self): message = 'Hello' self.assertIsNone(message)<\/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_variable_not_none (test_none.TestNone) ... FAIL ====================================================================== FAIL: test_variable_not_none (test_none.TestNone) ---------------------------------------------------------------------- Traceback (most recent call last): File \"D:\\python-unit-testing\\test_none.py\", line 7, in test_variable_not_none self.assertIsNone(message) AssertionError: 'Hello' is not None ---------------------------------------------------------------------- Ran 1 test in 0.001s FAILED (failures=1)<\/code><small>Code language: plaintext (plaintext)<\/small><\/code><\/pre>\n\n\n\n<p>Since the message is&nbsp;<code>'Hello'<\/code>, it is not None. Therefore, the test failed.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3) Using assertIsNone() with a failed case with a message<\/h3>\n\n\n\n<p>The following example uses the\u00a0<code>assertIsNone()<\/code>\u00a0to test if the message variable is None. Also, we show a message when the test fails:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>import unittest class TestNone(unittest.TestCase): def test_variable_not_none(self): message = 'Hello' self.assertIsNone( message, f'The message is \"{message}\" so it is not None.' )<\/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_variable_not_none (test_none.TestNone) ... FAIL ====================================================================== FAIL: test_variable_not_none (test_none.TestNone) ---------------------------------------------------------------------- Traceback (most recent call last): File \"D:\\python-unit-testing\\test_none.py\", line 7, in test_variable_not_none self.assertIsNone( AssertionError: 'Hello' is not None : The message is \"Hello\" so it is not None. ---------------------------------------------------------------------- Ran 1 test in 0.001s FAILED (failures=1)<\/code><small>Code language: plaintext (plaintext)<\/small><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Python assertIsNotNone() method<\/h2>\n\n\n\n<p>The\u00a0<code>assertIsNotNone()<\/code>\u00a0is opposite of the\u00a0<code>assertIsNone()<\/code>\u00a0method. The\u00a0<code>assertIsNotNone()<\/code>\u00a0method tests if a variable is not None.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>assertIsNotNone(expr, msg=None)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>The test passes if the\u00a0<code>expr<\/code>\u00a0is not\u00a0<code>None<\/code>\u00a0or fails otherwise. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>import unittest class TestNone(unittest.TestCase): def test_variable_is_not_none(self): message = 'Bye' self.assertIsNotNone(message)<\/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_variable_is_not_none (test_not_none.TestNone) ... ok ---------------------------------------------------------------------- Ran 1 test in 0.001s OK<\/code><\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you\u2019ll learn how to use the Python&nbsp;assertIsNone()&nbsp;method to test if an expression is&nbsp;None. Introduction to the Python assertIsNone() method The\u00a0assertIsNone()\u00a0is a method of the\u00a0TestCase\u00a0class of the\u00a0unittest\u00a0module. The\u00a0assertIsNone()\u00a0test if an expression is\u00a0None: If the&nbsp;expr&nbsp;is&nbsp;None, the test passes. Otherwise, the test will fail. The&nbsp;msg&nbsp;is optional. It\u2019ll be displayed in the test result if [&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-423","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\/423","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=423"}],"version-history":[{"count":1,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/423\/revisions"}],"predecessor-version":[{"id":424,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/423\/revisions\/424"}],"wp:attachment":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/media?parent=423"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/categories?post=423"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/tags?post=423"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}