{"id":413,"date":"2024-01-29T10:57:31","date_gmt":"2024-01-29T10:57:31","guid":{"rendered":"https:\/\/learnpython.elegantwallp.com\/?p=413"},"modified":"2024-01-29T10:57:32","modified_gmt":"2024-01-29T10:57:32","slug":"python-unittest-assert-methods","status":"publish","type":"post","link":"https:\/\/learnpython.elegantwallp.com\/2024\/01\/29\/python-unittest-assert-methods\/","title":{"rendered":"Python Unittest Assert Methods"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you\u2019ll learn the overview of the Python&nbsp;<code>unittest<\/code>&nbsp;assert methods to perform unit testing.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to Python unittest assert methods<\/h2>\n\n\n\n<p>The\u00a0<code>TestCase<\/code>\u00a0class of the\u00a0unittest\u00a0module provides you with a large number of assert methods to test. The following table shows the most commonly used assert methods:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Method<\/th><th>Checks that<\/th><\/tr><\/thead><tbody><tr><td><code>assertEqual(x,\u00a0y, msg=None)<\/code><\/td><td><code>x&nbsp;==&nbsp;y<\/code><\/td><\/tr><tr><td><code>assertNotEqual(x,y,msg=None)<\/code><\/td><td><code>x&nbsp;!=&nbsp;y<\/code><\/td><\/tr><tr><td><code>assertTrue(x, msg=None)<\/code><\/td><td><code>bool(x)&nbsp;is&nbsp;True<\/code><\/td><\/tr><tr><td><code>assertFalse(x, msg=None)<\/code><\/td><td><code>bool(x)&nbsp;is&nbsp;False<\/code><\/td><\/tr><tr><td><code>assertIs(x,\u00a0y , msg=None)<\/code><\/td><td><code>x&nbsp;is&nbsp;y<\/code><\/td><\/tr><tr><td><code>assertIsNot(x,\u00a0y, msg=None)<\/code><\/td><td>x<code>&nbsp;is&nbsp;not&nbsp;<\/code>y<\/td><\/tr><tr><td><code>assertIsNone(x, msg=None)<\/code><\/td><td><code>x&nbsp;is&nbsp;None<\/code><\/td><\/tr><tr><td><code>assertIsNotNone(x , msg=None)<\/code><\/td><td><code>x&nbsp;is&nbsp;not&nbsp;None<\/code><\/td><\/tr><tr><td><code>assertIn(x,\u00a0y, msg=None)<\/code><\/td><td>x<code>&nbsp;in&nbsp;<\/code>y<\/td><\/tr><tr><td><code>assertNotIn(x,\u00a0y, msg=None)<\/code><\/td><td>x<code>&nbsp;not&nbsp;in&nbsp;<\/code>y<\/td><\/tr><tr><td><code>assertIsInstance(x,\u00a0y, msg=None)<\/code><\/td><td><code>isinstance(x,&nbsp;y)<\/code><\/td><\/tr><tr><td><code>assertNotIsInstance(x,\u00a0y, msg=None)<\/code><\/td><td><code>not&nbsp;isinstance(x,&nbsp;y)<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>All of these methods have an optional&nbsp;<code>msg<\/code>&nbsp;parameter whose type is a string. The&nbsp;<code>msg<\/code>&nbsp;will be displayed in the test result if the test fails.<\/p>\n\n\n\n<p>The following assert methods check the exceptions, warnings, and log messages:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Method<\/th><th>Checks that<\/th><\/tr><\/thead><tbody><tr><td><code>assertRaises(exc,&nbsp;fun,&nbsp;*args,&nbsp;**kwds)<\/code><\/td><td><code>fun(*args,&nbsp;**kwds)<\/code>&nbsp;raises&nbsp;<em>exc<\/em><\/td><\/tr><tr><td><code>assertRaisesRegex(exc,&nbsp;r,&nbsp;fun,&nbsp;*args,&nbsp;**kwds)<\/code><\/td><td><code>fun(*args,&nbsp;**kwds)<\/code>&nbsp;raises&nbsp;<em>exc<\/em>&nbsp;and the message matches regex&nbsp;<em>r<\/em><\/td><\/tr><tr><td><code>assertWarns(warn,&nbsp;fun,&nbsp;*args,&nbsp;**kwds)<\/code><\/td><td><code>fun(*args,&nbsp;**kwds)<\/code>&nbsp;raises&nbsp;<em>warn<\/em><\/td><\/tr><tr><td><code>assertWarnsRegex(warn,&nbsp;r,&nbsp;fun,&nbsp;*args,&nbsp;**kwds)<\/code><\/td><td><code>fun(*args,&nbsp;**kwds)<\/code>&nbsp;raises&nbsp;<em>warn<\/em>&nbsp;and the message matches regex&nbsp;<em>r<\/em><\/td><\/tr><tr><td><code>assertLogs(logger,&nbsp;level)<\/code><\/td><td>The&nbsp;<code>with<\/code>&nbsp;block logs on&nbsp;<em>logger<\/em>&nbsp;with a minimum&nbsp;<em>level<\/em><\/td><\/tr><tr><td><code>assertNoLogs(logger,&nbsp;level)<\/code><\/td><td>The&nbsp;<code>with<\/code>&nbsp;block does not log on<em>&nbsp;logger<\/em>&nbsp;with a minimum&nbsp;<em>level<\/em><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>The following table shows the assert methods that perform more specific checks:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Method<\/th><th>Checks that<\/th><\/tr><\/thead><tbody><tr><td><code>assertAlmostEqual(x,\u00a0y)<\/code><\/td><td><code>round(x-y,&nbsp;7)&nbsp;==&nbsp;0<\/code><\/td><\/tr><tr><td><code>assertNotAlmostEqual(x,\u00a0y)<\/code><\/td><td><code>round(x-y,&nbsp;7)&nbsp;!=&nbsp;0<\/code><\/td><\/tr><tr><td><code>assertGreater(x,&nbsp;y)<\/code><\/td><td><code>x&nbsp;&gt;&nbsp;y<\/code><\/td><\/tr><tr><td><code>assertGreaterEqual(x,&nbsp;y)<\/code><\/td><td><code>x&nbsp;&gt;=&nbsp;y<\/code><\/td><\/tr><tr><td><code>assertLess(x,&nbsp;y)<\/code><\/td><td>x<code>&nbsp;&lt;&nbsp;<\/code>y<\/td><\/tr><tr><td><code>assertLessEqual(x,&nbsp;y)<\/code><\/td><td>x<code>&nbsp;&lt;=&nbsp;<\/code>y<\/td><\/tr><tr><td><code>assertRegex(s,&nbsp;r)<\/code><\/td><td><code>r.search(s)<\/code><\/td><\/tr><tr><td><code>assertNotRegex(s,&nbsp;r)<\/code><\/td><td><code>not&nbsp;r.search(s)<\/code><\/td><\/tr><tr><td><code>assertCountEqual(x,&nbsp;y)<\/code><\/td><td><em>x<\/em>&nbsp;and&nbsp;<em>y<\/em>&nbsp;have the same number of elements in the same number.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>In the next tutorials, you\u2019ll learn about the&nbsp;<code>unittest<\/code>&nbsp;assert methods in more detail and how to use them effectively.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you\u2019ll learn the overview of the Python&nbsp;unittest&nbsp;assert methods to perform unit testing. Introduction to Python unittest assert methods The\u00a0TestCase\u00a0class of the\u00a0unittest\u00a0module provides you with a large number of assert methods to test. The following table shows the most commonly used assert methods: Method Checks that assertEqual(x,\u00a0y, msg=None) x&nbsp;==&nbsp;y assertNotEqual(x,y,msg=None) x&nbsp;!=&nbsp;y assertTrue(x, [&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-413","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\/413","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=413"}],"version-history":[{"count":1,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/413\/revisions"}],"predecessor-version":[{"id":414,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/413\/revisions\/414"}],"wp:attachment":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/media?parent=413"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/categories?post=413"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/tags?post=413"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}