

{"id":25681,"date":"2018-08-23T05:36:38","date_gmt":"2018-08-23T00:06:38","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=25681"},"modified":"2026-04-24T17:43:26","modified_gmt":"2026-04-24T12:13:26","slug":"python-unittest","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/python-unittest\/","title":{"rendered":"Unit Testing With Python Unittest &#8211; Example &amp; Working"},"content":{"rendered":"<p><span style=\"font-weight: 400\">In this Python Unittest tutorial, we will learn how to set up unit tests for our Python code. For this, we will use the module Unittest\u00a0in Unit Testing with Python. <\/span><\/p>\n<p><span style=\"font-weight: 400\">Right before leaving, we will also introduce you to pytest, another module for the same thing. <\/span><\/p>\n<p><span style=\"font-weight: 400\">Moreover, we will discuss the Python Unittest example and its working. Also, we will see Python Unit Testing Framework and assert.<\/span><\/p>\n<p>So, let&#8217;s start Unit Testing with Python Unittest Tutorial.<\/p>\n<div id=\"attachment_25715\" style=\"width: 1210px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/Unit-Testing-With-Python-Unittest-01.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-25715\" class=\"wp-image-25715 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/Unit-Testing-With-Python-Unittest-01.jpg\" alt=\"Unit Testing With Python Unittest - Example &amp; Working\" width=\"1200\" height=\"628\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/Unit-Testing-With-Python-Unittest-01.jpg 1200w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/Unit-Testing-With-Python-Unittest-01-150x79.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/Unit-Testing-With-Python-Unittest-01-300x157.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/Unit-Testing-With-Python-Unittest-01-768x402.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/Unit-Testing-With-Python-Unittest-01-1024x536.jpg 1024w\" sizes=\"auto, (max-width: 1200px) 100vw, 1200px\" \/><\/a><p id=\"caption-attachment-25715\" class=\"wp-caption-text\">Unit Testing With Python Unittest &#8211; Example &amp; Working<\/p><\/div>\n<h3>What is Python Unittest?<\/h3>\n<p><span style=\"font-weight: 400\">Python Unittest is a Python Unit-Testing framework. Inspired by JUnit, it is much like the unit testing frameworks we have with other languages. <\/span><\/p>\n<p><span style=\"font-weight: 400\">Here are some features it supports-<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Test automation<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Sharing setup and shutdown code for tests<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Aggregating tests into collections<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Independence of tests from the framework<\/span><\/li>\n<\/ul>\n<h4>1. Concepts in an object-oriented way for Python Unittest<\/h4>\n<ul>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\"><strong>Test fixture-<\/strong> the preparation necessary to carry out test(s) and related cleanup actions.<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\"><strong>Test case-<\/strong> the individual unit of testing.<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\"><strong>A Test suite-<\/strong> a collection of test cases, test suites, or both.<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\"><strong>Test runner-<\/strong> a component for organising the execution of tests and for delivering the outcome to the user.<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400\">In this Python Unittest tutorial, we will use the unittest module to demonstrate our examples to you.<\/span><\/p>\n<h3>Python Unittest Example<\/h3>\n<p><span style=\"font-weight: 400\">In the following example of Unittest in Python, we will take a simple function that calculates the modulus 3 of a value.<\/span><\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; import unittest\r\n&gt;&gt;&gt; def modthree(x):                             #defining the function\r\n      return x%3\r\n&gt;&gt;&gt; class Tests(unittest.TestCase):\r\n      def test(self):                                  #test method\r\n               self.assertEqual(modthree(4),1)\r\n&gt;&gt;&gt; if __name__=='__main__':\r\n      unittest.main()<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">.<br \/>\n&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br \/>\nRan 1 test in 0.010s<br \/>\nOK<\/div>\n<p>Did you see the output?<\/p>\n<h4>1. One More Example of Python Unittest<\/h4>\n<p><span style=\"font-weight: 400\">Now let\u2019s try testing for string methods; we won\u2019t need a function for this.<\/span><\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; class TestStringMethods(unittest.TestCase):\r\n        def test_lstrip(self): #testing for left stripping\r\n                 self.assertEqual('   hello '.lstrip(),'hello ')\r\n        def test_isupper(self): #testing for isupper\r\n                 self.assertTrue('HELLO'.isupper())\r\n                 self.assertFalse('HELlO'.isupper())\r\n        def test_split(self): #testing for split\r\n                 self.assertEqual('Hello World'.split(),['Hello','World'])\r\n                 with self.assertRaises(TypeError):\r\n                         'Hello World'.split(2)\r\n&gt;&gt;&gt; if __name__=='__main__':\r\n        unittest.main()<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">&#8230;.<br \/>\n&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br \/>\nRan 4 tests in 0.031s<br \/>\nOK<\/div>\n<h3>How Python Unittest Works?<\/h3>\n<p><span style=\"font-weight: 400\">So we\u2019ve seen that Unit Testing with Python works without much effort. But how does this happen behind the scenes? <\/span><\/p>\n<p><span style=\"font-weight: 400\">Let\u2019s find out.<\/span><\/p>\n<h4>1. Subclassing unittest.TestCase<\/h4>\n<p><span style=\"font-weight: 400\">Consider the following line-<\/span><\/p>\n<pre class=\"EnlighterJSRAW\">class TestStringMethods(unittest.TestCase):<\/pre>\n<p>Here, we subclass unittest.TestCase. What we mean is we make our class TestStringMethods inherit from the class unittest.TestCase. Then, we define three methods, the names for which begin with \u2018test\u2019:<\/p>\n<p><strong>test_lstrip()<\/strong><br \/>\n<strong>test_isupper()<\/strong><br \/>\n<strong>test_split()<\/strong><\/p>\n<h4>2. Python Unittest Assert Methods<\/h4>\n<p><span style=\"font-weight: 400\">Now, let\u2019s take a look at what methods we can call within Unit testing with Python:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\"><strong>assertEqual()-<\/strong> Tests that the two arguments are equal in value.<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\"><strong>assertNotEqual()-<\/strong> Tests that the two arguments are unequal in value.<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\"><strong>assertTrue()-<\/strong> Tests that the argument has a Boolean value of True.<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\"><strong>assertFalse()-<\/strong> Tests that the argument has a Boolean value of False.<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\"><strong>assertIs()-<\/strong> Tests that the arguments evaluate to the same object.<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\"><strong>assertIsNot()-<\/strong> Tests that the arguments do not evaluate to the same object.<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\"><strong>assertIsNone()-<\/strong> Tests that the argument evaluates to none.<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\"><strong>assertIsNotNone()-<\/strong> Tests that the argument does not evaluate to none.<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\"><strong>assertIn()-<\/strong> Tests that the first argument is in the second.<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\"><strong>assertNotIn()-<\/strong> Tests that the first argument is not in the second.<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\"><strong>assertIsInstance()-<\/strong> Tests that the first argument (object) is an instance of the second (class).<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\"><strong>assertRaises()-<\/strong> Tests that Python raises an exception when we call the callable with positional\/ keyword arguments we also passed to this method.<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\"><strong>assertRaisesRegex()-<\/strong> Tests that regex matches on the string representation of the exception raised; similar to assertRaises().<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\"><strong>assertWarns()-<\/strong> Tests that Python triggers a warning when we call the callable with positional\/ keyword arguments we also passed to this method.<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\"><strong>assertWarnsRegex()-<\/strong> Tests that regex matches on the message for the triggered warning; similar to assertWarns().<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\"><strong>assertLogs()-<\/strong> Tests that Python has logged at least one message on the logger or a child of the logger; ensures this is with at least the level we mention.<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\"><strong>assertAlmostEqual()-<\/strong> Tests that the first and second arguments have approximately equal values.<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\"><strong>assertNotAlmostEqual()-<\/strong> Tests that the first and second arguments do not have approximately equal values.<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\"><strong>assertGreater()-<\/strong> Tests that the first argument is greater than the second.<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\"><strong>assertGreaterEqual()-<\/strong> Tests that the first argument is greater than or equal to the second.<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\"><strong>assertLess()-<\/strong> Tests that the first argument is lesser than the second.<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\"><strong>assertLessEqual()-<\/strong> Tests that the first argument is lesser than or equal to the second.<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\"><strong>assertRegex()-<\/strong> Tests that a regex search matches the text.<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\"><strong>assertNotRegex()-<\/strong> Tests that a regex search does not match the text.<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\"><strong>assertCountEqual()-<\/strong> Tests that the first argument, which is a sequence, contains the same as does the second.<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\"><strong>assertMultiLineEqual()-<\/strong> Tests that the first argument, which is a multiline string, is equal to the second.<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\"><strong>assertSequenceEqual()-<\/strong> Tests that two sequences are equal.<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\"><strong>assertListEqual()-<\/strong> Tests that two lists are equal.<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\"><strong>assertTupleEqual()-<\/strong> Tests that two lists are equal.<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\"><strong>assertSetEqual()-<\/strong> Tests that two sets are equal.<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\"><strong>assertDictEqual()-<\/strong> Tests that two dictionaries are equal.<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400\">Now that we\u2019ve discussed all these, you can go check the code once again. We used the methods assertEqual(), assertTrue(), assertFalse(), and assertRaises().<\/span><\/p>\n<h4>3. unittest.main()<\/h4>\n<p><span style=\"font-weight: 400\">This delivers a command-line interface to the test script. The output suggests whether the tests ran okay or failed.<\/span><\/p>\n<h3>Tests That Fail in Python Unittesting<\/h3>\n<p><span style=\"font-weight: 400\">What happens if a test fails? To make this happen, we refer to a string variable that doesn\u2019t already exist.<\/span><\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; class TestStringMethods(unittest.TestCase):\r\n      def test_lstrip(self):\r\n              self.assertEqual('   hello '.lstrip(),'hello ')\r\n      def test_isupper(self):\r\n              self.assertTrue('HELLO'.isupper())\r\n              self.assertFalse('HELlO'.isupper())\r\n      def test_split(self):\r\n              self.assertEqual('Hello World'.split(),['Hello','World'])\r\n              with self.assertRaises(TypeError):\r\n                      s.split(2)\r\n&gt;&gt;&gt; if __name__=='__main__':\r\n      unittest.main()<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">..E.<br \/>\n======================================================================<br \/>\nERROR: test_split (__main__.TestStringMethods)<br \/>\n&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br \/>\nTraceback (most recent call last):<br \/>\nFile &#8220;&lt;pyshell#21&gt;&#8221;, line 10, in test_split<br \/>\nNameError: name &#8216;s&#8217; is not defined<br \/>\n&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br \/>\nRan 4 tests in 0.016s<br \/>\nFAILED (errors=1)<\/div>\n<p><span style=\"font-weight: 400\">You can see the error in the output. One of the tests failed and returned an error. It did so because we did not define a string <\/span><i><span style=\"font-weight: 400\">s<\/span><\/i><span style=\"font-weight: 400\">.<\/span><\/p>\n<h3>Python Unittest &#8211; Testing With pytest<\/h3>\n<p><span style=\"font-weight: 400\">It was fun working with Python Unittest. But before leaving, we want to introduce you to pytest, a framework that makes it fun to write small tests. <\/span><\/p>\n<p><span style=\"font-weight: 400\">But that doesn\u2019t limit it- we can scale it to support complex functional testing for applications and libraries.<\/span><\/p>\n<p><span style=\"font-weight: 400\">First, install pytest with Python pip-<\/span><br \/>\n<strong>pip install pytest<\/strong><br \/>\n<span style=\"font-weight: 400\">You don\u2019t have to import this in the IDLE; we create the following Python file on our Desktop-<\/span><\/p>\n<pre class=\"EnlighterJSRAW\">def modthree(x):\r\n   return x%3\r\ndef test_value():\r\n   assert(modthree(4)==1)<\/pre>\n<p><span style=\"font-weight: 400\">We save this as demo.py. Then, we open the command line and get to the desktop. After that, we run a test-<\/span><\/p>\n<div id=\"attachment_25717\" style=\"width: 650px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/Untitled-4.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-25717\" class=\"wp-image-25717 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/Untitled-4.png\" alt=\"Python Unittest\" width=\"640\" height=\"134\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/Untitled-4.png 640w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/Untitled-4-150x31.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/Untitled-4-300x63.png 300w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/a><p id=\"caption-attachment-25717\" class=\"wp-caption-text\">Python Unittest &#8211; Testing with pytest<\/p><\/div>\n<p><span style=\"font-weight: 400\">Let\u2019s make a test fail.<\/span><\/p>\n<pre class=\"EnlighterJSRAW\">def modthree(x):\r\n   return x%3\r\ndef test_value():\r\n   assert(modthree(4)==2)<\/pre>\n<div id=\"attachment_25718\" style=\"width: 653px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/Untitled1.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-25718\" class=\"wp-image-25718 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/Untitled1.png\" alt=\"Python Unittest\" width=\"643\" height=\"241\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/Untitled1.png 643w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/Untitled1-150x56.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/Untitled1-300x112.png 300w\" sizes=\"auto, (max-width: 643px) 100vw, 643px\" \/><\/a><p id=\"caption-attachment-25718\" class=\"wp-caption-text\">Python Unittest &#8211; pytest Testing in Python<\/p><\/div>\n<p>So, this was all in Unit Testing with Python Unittest. Hope you like our explanation.<\/p>\n<p><strong>Difference between unittest and pytest in Python:<\/strong><\/p>\n<p><strong>PYTEST:<\/strong><\/p>\n<ul>\n<li>It is easy to read and write, and has a very simple setup code.<\/li>\n<li>The test must be kept in specific classes or folders to find them easily.<\/li>\n<li>The tests can be organised in small, flexible pieces.<\/li>\n<li>It has multiple ways to check if your code has worked or not.<\/li>\n<li>It can perform the same test multiple times with different data.<\/li>\n<\/ul>\n<p><strong>UNITTEST:<\/strong><\/p>\n<ul>\n<li>It is long and has too many words. Also, it requires a lot of setup code.<\/li>\n<li>It automatically finds out the test; it doesn&#8217;t require any special classes or folders.<\/li>\n<li>The tests have to be grouped according to their type.<\/li>\n<\/ul>\n<h3>Python Interview Questions on Unittest<\/h3>\n<ol>\n<li>What is a Unit Test in Python?<\/li>\n<li>How do you write a Unittest in Python? Give an example.<\/li>\n<li>What are the best Python unit testing frameworks?<\/li>\n<li>What are the best practices for Python unittesting?<\/li>\n<li>What is the difference between Pytest and Unittest?<\/li>\n<\/ol>\n<h3>Conclusion<\/h3>\n<p><span style=\"font-weight: 400\">Hence, in this Python Unittest tutorial, we discussed Unit Testing with Python. <\/span><span style=\"font-weight: 400\">Moreover, we saw a Python Unittest example working. <\/span><\/p>\n<p><span style=\"font-weight: 400\">Also, we discussed Python Unit Testing frameworks and a test case example with Python Unittest assert. <\/span><\/p>\n<p><span style=\"font-weight: 400\">We hope you can run your own tests for your code. In this tutorial, we saw how to do that with the Python Unittest and pytest modules. <\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this Python Unittest tutorial, we will learn how to set up unit tests for our Python code. For this, we will use the module Unittest\u00a0in Unit Testing with Python. Right before leaving, we&#46;&#46;&#46;<\/p>\n","protected":false},"author":5,"featured_media":25715,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[10907,10908,10909,10910,10911,10912,10946,14632,15151,15152,15153,15154,15155],"class_list":["post-25681","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-python-unit-testing-frameworks","tag-python-unittest","tag-python-unittest-assert","tag-python-unittest-example","tag-python-unittest-exception","tag-python-unittest-tutorial","tag-python3-unittest","tag-test-case-example","tag-unit-test-with-python","tag-unit-testing","tag-unit-testing-in-python","tag-unit-testing-tools","tag-unittest-python-3"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Unit Testing With Python Unittest - Example &amp; Working - DataFlair<\/title>\n<meta name=\"description\" content=\"Let&#039;s learn how to set up unit tests for our Python code. For this, we will use the module Unittest\u00a0in Unit Testing with Python.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/python-unittest\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Unit Testing With Python Unittest - Example &amp; Working - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Let&#039;s learn how to set up unit tests for our Python code. For this, we will use the module Unittest\u00a0in Unit Testing with Python.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/python-unittest\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2018-08-23T00:06:38+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-04-24T12:13:26+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/Unit-Testing-With-Python-Unittest-01.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Unit Testing With Python Unittest - Example &amp; Working - DataFlair","description":"Let's learn how to set up unit tests for our Python code. For this, we will use the module Unittest\u00a0in Unit Testing with Python.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/python-unittest\/","og_locale":"en_US","og_type":"article","og_title":"Unit Testing With Python Unittest - Example &amp; Working - DataFlair","og_description":"Let's learn how to set up unit tests for our Python code. For this, we will use the module Unittest\u00a0in Unit Testing with Python.","og_url":"https:\/\/data-flair.training\/blogs\/python-unittest\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2018-08-23T00:06:38+00:00","article_modified_time":"2026-04-24T12:13:26+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/Unit-Testing-With-Python-Unittest-01.jpg","type":"image\/jpeg"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/python-unittest\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/python-unittest\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823"},"headline":"Unit Testing With Python Unittest &#8211; Example &amp; Working","datePublished":"2018-08-23T00:06:38+00:00","dateModified":"2026-04-24T12:13:26+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/python-unittest\/"},"wordCount":1314,"commentCount":4,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/python-unittest\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/Unit-Testing-With-Python-Unittest-01.jpg","keywords":["python unit testing frameworks","Python Unittest","python unittest assert","Python unittest example","python unittest exception","Python Unittest tutorial","python3 unittest","Test case example","Unit test with Python","unit testing","Unit Testing in Python","unit testing tools","unittest python 3"],"articleSection":["Python Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/python-unittest\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/python-unittest\/","url":"https:\/\/data-flair.training\/blogs\/python-unittest\/","name":"Unit Testing With Python Unittest - Example &amp; Working - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/python-unittest\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/python-unittest\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/Unit-Testing-With-Python-Unittest-01.jpg","datePublished":"2018-08-23T00:06:38+00:00","dateModified":"2026-04-24T12:13:26+00:00","description":"Let's learn how to set up unit tests for our Python code. For this, we will use the module Unittest\u00a0in Unit Testing with Python.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/python-unittest\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/python-unittest\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/python-unittest\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/Unit-Testing-With-Python-Unittest-01.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/Unit-Testing-With-Python-Unittest-01.jpg","width":1200,"height":628,"caption":"Unit Testing With Python Unittest - Example &amp; Working"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/python-unittest\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Python Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/python\/"},{"@type":"ListItem","position":3,"name":"Unit Testing With Python Unittest &#8211; Example &amp; Working"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team creates expert-level guides on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our goal is to empower learners with easy-to-understand content. Explore our resources for career growth and practical learning.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam1\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/25681","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=25681"}],"version-history":[{"count":14,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/25681\/revisions"}],"predecessor-version":[{"id":147872,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/25681\/revisions\/147872"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/25715"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=25681"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=25681"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=25681"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}