{"id":425,"date":"2024-01-29T11:09:37","date_gmt":"2024-01-29T11:09:37","guid":{"rendered":"https:\/\/learnpython.elegantwallp.com\/?p=425"},"modified":"2024-01-29T11:09:40","modified_gmt":"2024-01-29T11:09:40","slug":"python-asserttrue","status":"publish","type":"post","link":"https:\/\/learnpython.elegantwallp.com\/2024\/01\/29\/python-asserttrue\/","title":{"rendered":"Python assertTrue()"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you\u2019ll learn how to use Python&nbsp;<code>assertTrue()<\/code>&nbsp;to test if an expression is True and&nbsp;<code>assertFalse()<\/code>&nbsp;method to test if an expression is False.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to the Python assertTrue() method<\/h2>\n\n\n\n<p>The\u00a0<code>assertTrue()<\/code>\u00a0is a method of the\u00a0<code>TestCase<\/code>\u00a0class in the\u00a0unittest\u00a0module. The\u00a0<code>assertTrue()<\/code>\u00a0method tests if an expression is\u00a0<code>True<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>assertTrue(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>True<\/code>, the test passes. Otherwise, the test fails.<\/p>\n\n\n\n<p>The&nbsp;<code>msg<\/code>&nbsp;is optional. If you pass the&nbsp;<code>msg<\/code>&nbsp;parameter, it\u2019ll be displayed when the test fails.<\/p>\n\n\n\n<p>The\u00a0<code>assertTrue()<\/code>\u00a0method is equivalent to the following expression:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>bool(expr) is True<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>It is not equivalent to the following:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>expr is True<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>In practice, you should use the&nbsp;<code>assertTrue()<\/code>&nbsp;method to test the boolean of a variable. Also, you should not use the&nbsp;<code>assertTrue()<\/code>&nbsp;method if more specific methods are available.<\/p>\n\n\n\n<p>For example, instead of using the\u00a0<code>assertTrue()<\/code>\u00a0method for the following case:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>assertTrue(a == b)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>you should use the\u00a0<code>assertEqual()<\/code>\u00a0method instead:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>assertEqual(a,b)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Because the&nbsp;<code>assertEqual()<\/code>&nbsp;method provides a more clear error message in case the test fails.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Python assertTrue() method example<\/h2>\n\n\n\n<p>The following example uses the\u00a0<code>assertTrue()<\/code>\u00a0method to test if all characters in a string are digits:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>import unittest class TestBool(unittest.TestCase): def test_is_digit(self): str = '123' self.assertTrue(str.isdigit())<\/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_digit (test_bool.TestBool) ... ok ---------------------------------------------------------------------- Ran 1 test in 0.000s OK<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Python assertFalse() method<\/h2>\n\n\n\n<p>The\u00a0<code>assertFalse()<\/code>\u00a0method tests if an expression is\u00a0<code>False<\/code>. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>import unittest class TestBool(unittest.TestCase): def test_empty_string_is_digit(self): str = '' self.assertFalse(str.isdigit()) def test_alpha_is_digit(self): str = 'a00' self.assertFalse(str.isdigit())<\/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_alpha_is_digit (test_bool.TestBool) ... ok test_empty_string_is_digit (test_bool.TestBool) ... ok test_is_digit (test_bool.TestBool) ... ok ---------------------------------------------------------------------- Ran 3 tests in 0.001s OK<\/code><\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you\u2019ll learn how to use Python&nbsp;assertTrue()&nbsp;to test if an expression is True and&nbsp;assertFalse()&nbsp;method to test if an expression is False. Introduction to the Python assertTrue() method The\u00a0assertTrue()\u00a0is a method of the\u00a0TestCase\u00a0class in the\u00a0unittest\u00a0module. The\u00a0assertTrue()\u00a0method tests if an expression is\u00a0True: If the&nbsp;expr&nbsp;is&nbsp;True, the test passes. Otherwise, the test fails. The&nbsp;msg&nbsp;is optional. 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-425","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\/425","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=425"}],"version-history":[{"count":1,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/425\/revisions"}],"predecessor-version":[{"id":426,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/425\/revisions\/426"}],"wp:attachment":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/media?parent=425"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/categories?post=425"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/tags?post=425"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}