{"id":1139,"date":"2023-06-29T16:11:00","date_gmt":"2023-06-29T10:41:00","guid":{"rendered":"https:\/\/geekpython.in\/?p=1139"},"modified":"2024-03-01T17:12:15","modified_gmt":"2024-03-01T11:42:15","slug":"unit-tests-in-python","status":"publish","type":"post","link":"https:\/\/geekpython.in\/unit-tests-in-python","title":{"rendered":"Understanding Unit Testing in Python with the unittest Module"},"content":{"rendered":"\n<p>Unit testing is a crucial part of software development, ensuring that functions and tasks within code work as intended. By dividing code into small units and testing them independently, developers can catch errors early and simplify the debugging process.<\/p>\n\n\n\n<p>This article explores how to implement unit testing in Python using the&nbsp;<code>unittest<\/code>&nbsp;module.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"heading-getting-started-with-unittest\"><a href=\"https:\/\/geekpython.in\/unit-tests-in-python#heading-getting-started-with-unittest\"><\/a>Getting Started With unittest<\/h2>\n\n\n\n<p>The&nbsp;<code>unittest<\/code>&nbsp;module includes a number of methods and classes for creating and running test cases. Let&#8217;s look at a simple example where we used the&nbsp;<code>unittest<\/code>&nbsp;module to create a test case.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \" title=\"basic.py\"># basic.py\nimport unittest\n\nclass TestSample(unittest.TestCase):\n    def test_equal(self):\n        self.assertEqual(round(3.155), 3.0)\n\n    def test_search(self):\n        self.assertIn(\"G\", \"Geek\")<\/pre><\/div>\n\n\n\n<p>First, we imported the&nbsp;<code>unittest<\/code>&nbsp;module, which will enable us to use the classes that will be used to write and execute test cases.<\/p>\n\n\n\n<p>The&nbsp;<code>TestSample<\/code>&nbsp;class is defined that inherits from the&nbsp;<code>unittest.TestCase<\/code>&nbsp;which will allow us to use the various&nbsp;<strong>assertion methods<\/strong>&nbsp;within our test cases.<\/p>\n\n\n\n<p>We defined two test methods within our&nbsp;<code>TestSample<\/code>&nbsp;class:&nbsp;<code>test_equal<\/code>&nbsp;and&nbsp;<code>test_search<\/code>.<\/p>\n\n\n\n<p>The test method&nbsp;<code>test_equal()<\/code>&nbsp;tests if&nbsp;<code>round(3.155)<\/code>&nbsp;is equal to&nbsp;<code>3.0<\/code>&nbsp;using the&nbsp;<code>assertEqual()<\/code>&nbsp;assertion method.<\/p>\n\n\n\n<p>The test method&nbsp;<code>test_search()<\/code>&nbsp;tests if the character&nbsp;<code>\"G\"<\/code>&nbsp;is present in the string&nbsp;<code>\"Geek\"<\/code>&nbsp;using the&nbsp;<code>assertIn()<\/code>&nbsp;assertion method.<\/p>\n\n\n\n<p>To run these tests, we need to execute the following command in the terminal.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:ps decode:true \">python -m unittest basic.py<\/pre><\/div>\n\n\n\n<p>This command will launch&nbsp;<code>unittest<\/code>&nbsp;as a module that searches for and executes the tests in the&nbsp;<code>basic.py<\/code>&nbsp;file.<\/p>\n\n\n\n<p>Note: The&nbsp;<code>unittest<\/code>&nbsp;module only discovers and executes those methods that start with&nbsp;<code>test_<\/code>&nbsp;or&nbsp;<code>test<\/code>.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"265\" src=\"https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/unitoutput1_1-1024x265.png\" alt=\"unittest output\" class=\"wp-image-1143\" srcset=\"https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/unitoutput1_1-1024x265.png 1024w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/unitoutput1_1-300x78.png 300w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/unitoutput1_1-768x199.png 768w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/unitoutput1_1-1536x397.png 1536w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/unitoutput1_1-2048x530.png 2048w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/unitoutput1_1.png 1920w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>By the way, these dots represent a successful test.<\/p>\n\n\n\n<p>We can use the&nbsp;<code>unittest.main()<\/code>&nbsp;function and put it in the following form at the end of the test script to load and run the tests from the module.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">if __name__ == '__main__':\n    unittest.main()<\/pre><\/div>\n\n\n\n<p>This will allow us to run our test file,&nbsp;<code>basic.py<\/code>&nbsp;in this case, as the main module.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"862\" height=\"223\" src=\"https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/unitoutput2_2.png\" alt=\"Output\" class=\"wp-image-1144\" srcset=\"https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/unitoutput2_2.png 862w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/unitoutput2_2-300x78.png 300w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/unitoutput2_2-768x199.png 768w\" sizes=\"auto, (max-width: 862px) 100vw, 862px\" \/><\/figure>\n\n\n\n<p><strong>More Detailed Result<\/strong><\/p>\n\n\n\n<p>We can use the&nbsp;<code>-v<\/code>&nbsp;flag in the terminal or pass an argument&nbsp;<code>verbosity=2<\/code>&nbsp;inside the&nbsp;<code>unittest.main()<\/code>&nbsp;function to get a detailed output of the test.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1920\" height=\"497\" src=\"https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/image-scaled.jpeg\" alt=\"Vervosity added using -v flag\" class=\"wp-image-1145\" srcset=\"https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/image-scaled.jpeg 1920w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/image-300x78.jpeg 300w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/image-1024x265.jpeg 1024w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/image-768x199.jpeg 768w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/image-1536x397.jpeg 1536w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/image-2048x530.jpeg 2048w\" sizes=\"auto, (max-width: 1920px) 100vw, 1920px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-commonly-used-assertion-methods\"><a href=\"https:\/\/geekpython.in\/unit-tests-in-python#heading-commonly-used-assertion-methods\"><\/a>Commonly Used Assertion Methods<\/h3>\n\n\n\n<p>Here is the list of the most commonly used assertion methods in unit testing.<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><thead><tr><td>Method<\/td><td>Checks that<\/td><\/tr><\/thead><tbody><tr><td><code>assertEqual(a, b)<\/code><\/td><td><code>a == b<\/code><\/td><\/tr><tr><td><code>assertNotEqual(a, b)<\/code><\/td><td><code>a != b<\/code><\/td><\/tr><tr><td><code>assertTrue(x)<\/code><\/td><td><code>bool(x) is True<\/code><\/td><\/tr><tr><td><code>assertFalse(x)<\/code><\/td><td><code>bool(x) is False<\/code><\/td><\/tr><tr><td><code>assertIs(a, b)<\/code><\/td><td><code>a is b<\/code><\/td><\/tr><tr><td><code>assertIsNot(a, b)<\/code><\/td><td><code>a is not b<\/code><\/td><\/tr><tr><td><code>assertIsNone(x)<\/code><\/td><td><code>x is None<\/code><\/td><\/tr><tr><td><code>assertIsNotNone(x)<\/code><\/td><td><code>x is not None<\/code><\/td><\/tr><tr><td><code>assertIn(a, b)<\/code><\/td><td><code>a in b<\/code><\/td><\/tr><tr><td><code>assertNotIn(a, b)<\/code><\/td><td><code>a not in b<\/code><\/td><\/tr><tr><td><code>assertIsInstance(a, b)<\/code><\/td><td><code>isinstance(a, b)<\/code><\/td><\/tr><tr><td><code>assertNotIsInstance(a, b)<\/code><\/td><td><code>not isinstance(a, b)<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"heading-example\"><a href=\"https:\/\/geekpython.in\/unit-tests-in-python#heading-example\"><\/a>Example<\/h2>\n\n\n\n<p>Assume we have some code and want to perform unit testing on it using the&nbsp;<code>unittest<\/code>&nbsp;module.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \" title=\"triangle.py\"># triangle.py\nclass Triangle:\n    def __init__(self, base, height):\n        self.base = base\n        self.height = height\n\n    def area(self):\n        return 0.5 * self.base * self.height\n\n    def perimeter(self, side):\n        return self.base + self.height + side<\/pre><\/div>\n\n\n\n<p>The code defines a class called&nbsp;<code>Triangle<\/code>&nbsp;which has an init method that initializes the object with the instance variables&nbsp;<code>self.base<\/code>&nbsp;and&nbsp;<code>self.height<\/code>.<\/p>\n\n\n\n<p>There are two more methods in the&nbsp;<code>Triangle<\/code>&nbsp;class:&nbsp;<code>area()<\/code>&nbsp;and&nbsp;<code>perimeter()<\/code>.<\/p>\n\n\n\n<p>The&nbsp;<code>area()<\/code>&nbsp;method returns the area of the triangle, which is half the product of the base and height (<code>0.5 * self.base * self.height<\/code>).<\/p>\n\n\n\n<p>The method&nbsp;<code>parameter()<\/code>&nbsp;accepts a parameter called&nbsp;<code>side<\/code>, and because the triangle&#8217;s parameter is the sum of its three sides, the&nbsp;<code>base<\/code>&nbsp;and&nbsp;<code>height<\/code>&nbsp;variables take the place of the other two sides.<\/p>\n\n\n\n<p>Now we can create another Python file in which we&#8217;ll write some tests and then execute them.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \" title=\"test_sample.py\"># test_sample.py\nfrom triangle import Triangle\nimport unittest\n\nclass TestTriangle(unittest.TestCase):\n    t = Triangle(9, 8)\n\n    def test_area(self):\n        self.assertEqual(self.t.area(), 36)\n\n    def test_perimeter(self):\n        self.assertEqual(self.t.perimeter(5), 22)\n\n    def test_valid_base(self):\n        self.assertGreater(self.t.base, 0)\n\n    def test_valid_height(self):\n        self.assertGreater(self.t.height, 0)\n\nif __name__ == '__main__':\n    unittest.main(verbosity=2)<\/pre><\/div>\n\n\n\n<p>The above code imports the&nbsp;<code>Triangle<\/code>&nbsp;class from the&nbsp;<code>triangle<\/code>&nbsp;module (<code>triangle.py<\/code>&nbsp;file) as well as imports the&nbsp;<code>unittest<\/code>&nbsp;module to write test cases.<\/p>\n\n\n\n<p>The&nbsp;<code>TestTriangle<\/code>&nbsp;class inherits from the&nbsp;<code>unittest.TestCase<\/code>&nbsp;which has four test methods. The&nbsp;<code>Triangle<\/code>&nbsp;class was instantiated with a&nbsp;<code>base<\/code>&nbsp;of&nbsp;<code>9<\/code>&nbsp;and&nbsp;<code>height<\/code>&nbsp;of&nbsp;<code>8<\/code>&nbsp;and stored inside the variable&nbsp;<code>t<\/code>.<\/p>\n\n\n\n<p>The&nbsp;<code>test_area<\/code>&nbsp;method tests whether&nbsp;<code>self.t.area()<\/code>&nbsp;is equal to the expected result&nbsp;<code>36<\/code>&nbsp;using the&nbsp;<code>assertEqual()<\/code>&nbsp;assertion.<\/p>\n\n\n\n<p>The&nbsp;<code>test_perimeter<\/code>&nbsp;method tests whether&nbsp;<code>self.t.perimeter(5)<\/code>&nbsp;is equal to&nbsp;<code>22<\/code>&nbsp;using the&nbsp;<code>assertEqual()<\/code>&nbsp;assertion.<\/p>\n\n\n\n<p>The&nbsp;<code>test_valid_base<\/code>&nbsp;and&nbsp;<code>test_valid_height<\/code>&nbsp;methods are defined to test if the base (<code>self.t.base<\/code>) and height (<code>self.t.height<\/code>) of the triangle are greater than&nbsp;<code>0<\/code>&nbsp;using the&nbsp;<code>assertGreater()<\/code>&nbsp;assertion.<\/p>\n\n\n\n<p>The&nbsp;<code>unittest.main(verbosity=2)<\/code>&nbsp;method retrieves and executes the tests from the&nbsp;<code>TestTriangle<\/code>&nbsp;class. We&#8217;ll get a detailed output because we used the&nbsp;<code>verbosity=2<\/code>&nbsp;argument.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"879\" height=\"294\" src=\"https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/unitoutput4.png\" alt=\"TestTriangle class output\" class=\"wp-image-1146\" srcset=\"https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/unitoutput4.png 879w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/unitoutput4-300x100.png 300w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/unitoutput4-768x257.png 768w\" sizes=\"auto, (max-width: 879px) 100vw, 879px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"heading-test-for-exception\"><a href=\"https:\/\/geekpython.in\/unit-tests-in-python#heading-test-for-exception\"><\/a>Test for Exception<\/h2>\n\n\n\n<p>If you&#8217;ve used&nbsp;<a target=\"_blank\" href=\"https:\/\/geekpython.in\/python-assert\" rel=\"noreferrer noopener\">assert statements<\/a>&nbsp;before, you&#8217;ll know that when one fails, it throws an&nbsp;<code>AssertionError<\/code>. Similarly, whenever a test method fails, an&nbsp;<code>AssertionError<\/code>&nbsp;is raised.<\/p>\n\n\n\n<p>We can predetermine the conditions under which our code will generate an error, and then test those conditions to see if they generate errors. This is possible with the&nbsp;<code>assertRaises()<\/code>&nbsp;method.<\/p>\n\n\n\n<p>The&nbsp;<code>assertRaises()<\/code>&nbsp;method can be used with&nbsp;<a rel=\"noreferrer noopener\" href=\"https:\/\/geekpython.in\/context-managers-and-python-with-statement\" target=\"_blank\">context manager<\/a>&nbsp;so we&#8217;ll use it in the following form:<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">def test_method(self):\n    with assertRaises(exception_name):\n        function_name(argument)<\/pre><\/div>\n\n\n\n<p>Consider the following function&nbsp;<code>gen_odd()<\/code>, which generates a series of odd numbers up to the argument&nbsp;<code>n<\/code>&nbsp;by incrementing the&nbsp;<code>num<\/code>&nbsp;by&nbsp;<code>3<\/code>&nbsp;and contains only a few checks, where the argument&nbsp;<code>n<\/code>&nbsp;must be of type&nbsp;<code>int<\/code>&nbsp;and greater than&nbsp;<code>0<\/code>.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \" title=\"odd.py\"># odd.py\ndef gen_odd(n):\n    if type(n) != int:\n        raise TypeError(\"Invalid argument type.\")\n    if n &lt; 0:\n        raise ValueError(\"Value must be greater than 0.\")\n    num = 0\n    while num &lt;= n:\n        if num % 2 == 1:\n            print(num)\n        num += 3<\/pre><\/div>\n\n\n\n<p>Now we&#8217;ll write test methods to simulate conditions that could cause the above code to fail.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">from odd import gen_odd\nimport unittest\n\nclass OddTestCase(unittest.TestCase):\n\n    def test_negative_val(self):\n        with self.assertRaises(ValueError):\n            gen_odd(-5)\n\n    def test_float_val(self):\n        with self.assertRaises(TypeError):\n            gen_odd(99.9)\n\n    def test_string_val(self):\n        with self.assertRaises(TypeError):\n            gen_odd('10')\n\nif __name__ == '__main__':\n    unittest.main(verbosity=2)<\/pre><\/div>\n\n\n\n<p>We wrote three test methods in the&nbsp;<code>OddTestCase<\/code>&nbsp;class to ensure that when invalid arguments are passed, the corresponding error is raised.<\/p>\n\n\n\n<p>The&nbsp;<code>test_negative_val()<\/code>&nbsp;method asserts that&nbsp;<code>ValueError<\/code>&nbsp;is raised when&nbsp;<code>gen_odd(-5)<\/code>&nbsp;is called.<\/p>\n\n\n\n<p>Similarly, the&nbsp;<code>test_float_val()<\/code>&nbsp;and&nbsp;<code>test_string_val()<\/code>&nbsp;methods assert that when&nbsp;<code>gen_odd(99.9)<\/code>&nbsp;and&nbsp;<code>gen_odd('10')<\/code>&nbsp;are called, respectively,&nbsp;<code>TypeError<\/code>&nbsp;is raised.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"886\" height=\"326\" src=\"https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/unitoutput5.png\" alt=\"OddTestCase output\" class=\"wp-image-1147\" srcset=\"https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/unitoutput5.png 886w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/unitoutput5-300x110.png 300w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/unitoutput5-768x283.png 768w\" sizes=\"auto, (max-width: 886px) 100vw, 886px\" \/><\/figure>\n\n\n\n<p>All three tests in the above code passed, which means they all raised corresponding errors, otherwise, the tests would have failed or raised the errors if another exception was raised. Let&#8217;s put it to the test.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">from odd import gen_odd\nimport unittest\n\nclass OddTestCase(unittest.TestCase):\n\n    def test_valid_arg(self):\n        with self.assertRaises(TypeError, msg=\"Valid argument\"):\n            gen_odd(10)\n\nif __name__ == '__main__':\n    unittest.main(verbosity=2)<\/pre><\/div>\n\n\n\n<p>The above condition within the&nbsp;<code>test_valid_arg()<\/code>&nbsp;method will not throw a&nbsp;<code>TypeError<\/code>&nbsp;because&nbsp;<code>gen_odd()<\/code>&nbsp;function is passed with a valid argument.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1088\" height=\"574\" src=\"https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/unitoutput6.png\" alt=\"Output\" class=\"wp-image-1148\" srcset=\"https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/unitoutput6.png 1088w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/unitoutput6-300x158.png 300w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/unitoutput6-1024x540.png 1024w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/unitoutput6-768x405.png 768w\" sizes=\"auto, (max-width: 1088px) 100vw, 1088px\" \/><\/figure>\n\n\n\n<p>The above test method failed and raised an&nbsp;<code>AssertionError<\/code>&nbsp;with the message&nbsp;<code>TypeError not raised : Valid argument<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"heading-skipping-tests\"><a href=\"https:\/\/geekpython.in\/unit-tests-in-python#heading-skipping-tests\"><\/a>Skipping Tests<\/h2>\n\n\n\n<p>The&nbsp;<code>unittest<\/code>&nbsp;makes use of the&nbsp;<code>skip()<\/code>&nbsp;decorator or&nbsp;<code>skipTest()<\/code>&nbsp;to skip any test method or whole test class on purpose, and we are required to specify the reason why the test is being skipped.<\/p>\n\n\n\n<p>Consider the previous example&#8217;s code, which we modified by adding the&nbsp;<code>skip()<\/code>&nbsp;decorator.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">from odd import gen_odd\nimport unittest\n\nclass OddTestCase(unittest.TestCase):\n\n    @unittest.skip(\"Valid argument\")\n    def test_valid_arg(self):\n        with self.assertRaises(TypeError):\n            gen_odd(10)\n\nif __name__ == '__main__':\n    unittest.main(verbosity=2)<\/pre><\/div>\n\n\n\n<p>It&#8217;s clear that the above condition will fail and throw an&nbsp;<code>AssertionError<\/code>, so we skipped the testing on purpose.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"862\" height=\"223\" src=\"https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/unitoutput7_1.png\" alt=\"Skipped test output\" class=\"wp-image-1150\" srcset=\"https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/unitoutput7_1.png 862w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/unitoutput7_1-300x78.png 300w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/unitoutput7_1-768x199.png 768w\" sizes=\"auto, (max-width: 862px) 100vw, 862px\" \/><\/figure>\n\n\n\n<p><strong>What if we wanted to skip the test if a particular condition was true?<\/strong>&nbsp;We can accomplish this by using the&nbsp;<code>skipIf()<\/code>&nbsp;decorator, which allows us to specify a condition and skip the test if it is true.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">from odd import gen_odd\nimport sys\nimport unittest\n\nclass OddTestCase(unittest.TestCase):\n\n    @unittest.skipIf(sys.getsizeof(gen_odd(10)) &gt; 10, \"Exceeded limit\")\n    def test_memory_use(self):\n        self.assertTrue(sys.getsizeof(gen_odd(10)) &gt; 10)\n        print(f\"Size: {sys.getsizeof(gen_odd(10))} bytes\")\n\nif __name__ == '__main__':\n    unittest.main(verbosity=2)<\/pre><\/div>\n\n\n\n<p>The condition in the above&nbsp;<code>skipIf()<\/code>&nbsp;decorator checks whether the size of&nbsp;<code>gen_odd(10)<\/code>&nbsp;is greater than&nbsp;<code>10<\/code>&nbsp;bytes, if the condition is&nbsp;<strong>true<\/strong>, the test method&nbsp;<code>test_memory_use()<\/code>&nbsp;is skipped, otherwise, the test is executed.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"933\" height=\"293\" src=\"https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/unitoutput8.png\" alt=\"Output\" class=\"wp-image-1151\" srcset=\"https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/unitoutput8.png 933w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/unitoutput8-300x94.png 300w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/unitoutput8-768x241.png 768w\" sizes=\"auto, (max-width: 933px) 100vw, 933px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-expected-failure\"><a href=\"https:\/\/geekpython.in\/unit-tests-in-python#heading-expected-failure\"><\/a>Expected Failure<\/h3>\n\n\n\n<p>If we have a test method or test class with conditions that are expected to be false, we can use the&nbsp;<code>expectedFailure()<\/code>&nbsp;decorator to mark them as expected failures instead of checking for errors.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">from odd import gen_odd\nimport sys\nimport unittest\n\nclass OddTestCase(unittest.TestCase):\n\n    @unittest.expectedFailure\n    def test_memory_use(self):\n        self.assertTrue(sys.getsizeof(gen_odd(10)) &lt; 10, msg=\"Expected to be failed\")\n        print(f\"Size: {sys.getsizeof(gen_odd(10))} bytes\")\n\nif __name__ == '__main__':\n    unittest.main(verbosity=2)<\/pre><\/div>\n\n\n\n<p>We&#8217;ve modified the previous code and the condition we are checking inside the&nbsp;<code>test_memory_use()<\/code>&nbsp;method is expected to be false, which is why the method is decorated with the&nbsp;<code>@unittest.expectedFailure<\/code>&nbsp;decorator.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"936\" height=\"293\" src=\"https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/unitoutput9.png\" alt=\"Expected failure output\" class=\"wp-image-1152\" srcset=\"https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/unitoutput9.png 936w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/unitoutput9-300x94.png 300w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/unitoutput9-768x240.png 768w\" sizes=\"auto, (max-width: 936px) 100vw, 936px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"heading-conclusion\"><a href=\"https:\/\/geekpython.in\/unit-tests-in-python#heading-conclusion\"><\/a>Conclusion<\/h2>\n\n\n\n<p>We can use the&nbsp;<code>unittest<\/code>&nbsp;module to write and run tests to ensure that the code is working properly. The test can result in one of three outcomes: OK, FAIL, or Error.<\/p>\n\n\n\n<p>The&nbsp;<code>unittest<\/code>&nbsp;module provides several assertion methods that are used to validate the code.<\/p>\n\n\n\n<p>Let&#8217;s recall, what we&#8217;ve learned:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>the basic usage of&nbsp;<code>unittest<\/code>&nbsp;module.<\/li>\n\n\n\n<li>CLI commands to run the tests.<\/li>\n\n\n\n<li>testing if the condition is raising an exception.<\/li>\n\n\n\n<li>skipping the tests on purpose and when a certain condition is true.<\/li>\n\n\n\n<li>marking a test as an expected failure.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\ud83c\udfc6<strong>Other articles you might be interested in if you liked this one<\/strong><\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" href=\"https:\/\/geekpython.in\/python-assert\" rel=\"noreferrer noopener\">How to use assert statements for debugging in Python<\/a>?<\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" href=\"https:\/\/geekpython.in\/asterisk-in-python\" rel=\"noreferrer noopener\">What are the uses of asterisk(*) in Python<\/a>?<\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" href=\"https:\/\/geekpython.in\/init-vs-new\" rel=\"noreferrer noopener\">What are __init__ and __new__ methods in Python<\/a>?<\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" href=\"https:\/\/geekpython.in\/implement-getitem-setitem-and-delitem-in-python\" rel=\"noreferrer noopener\">How to implement getitem, setitem and delitem in Python classes<\/a>?<\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" href=\"https:\/\/geekpython.in\/str-and-repr-in-python\" rel=\"noreferrer noopener\">How to change the string representation of the object using str and repr methods<\/a>?<\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" href=\"https:\/\/geekpython.in\/tempfile-in-python\" rel=\"noreferrer noopener\">How to generate temporary files and directories using tempfile in Python<\/a>?<\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" href=\"https:\/\/geekpython.in\/using-transfer-learning-for-deep-learning-model\" rel=\"noreferrer noopener\">Build a custom deep learning model using the transfer learning technique<\/a>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>That&#8217;s all for now<\/strong><\/p>\n\n\n\n<p><strong>Keep coding\u270c\u270c<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Unit testing is a crucial part of software development, ensuring that functions and tasks within code work as intended. By dividing code into small units and testing them independently, developers can catch errors early and simplify the debugging process. This article explores how to implement unit testing in Python using the&nbsp;unittest&nbsp;module. Getting Started With unittest [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1141,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ocean_post_layout":"","ocean_both_sidebars_style":"","ocean_both_sidebars_content_width":0,"ocean_both_sidebars_sidebars_width":0,"ocean_sidebar":"0","ocean_second_sidebar":"0","ocean_disable_margins":"enable","ocean_add_body_class":"","ocean_shortcode_before_top_bar":"","ocean_shortcode_after_top_bar":"","ocean_shortcode_before_header":"","ocean_shortcode_after_header":"","ocean_has_shortcode":"","ocean_shortcode_after_title":"","ocean_shortcode_before_footer_widgets":"","ocean_shortcode_after_footer_widgets":"","ocean_shortcode_before_footer_bottom":"","ocean_shortcode_after_footer_bottom":"","ocean_display_top_bar":"default","ocean_display_header":"default","ocean_header_style":"","ocean_center_header_left_menu":"0","ocean_custom_header_template":"0","ocean_custom_logo":0,"ocean_custom_retina_logo":0,"ocean_custom_logo_max_width":0,"ocean_custom_logo_tablet_max_width":0,"ocean_custom_logo_mobile_max_width":0,"ocean_custom_logo_max_height":0,"ocean_custom_logo_tablet_max_height":0,"ocean_custom_logo_mobile_max_height":0,"ocean_header_custom_menu":"0","ocean_menu_typo_font_family":"0","ocean_menu_typo_font_subset":"","ocean_menu_typo_font_size":0,"ocean_menu_typo_font_size_tablet":0,"ocean_menu_typo_font_size_mobile":0,"ocean_menu_typo_font_size_unit":"px","ocean_menu_typo_font_weight":"","ocean_menu_typo_font_weight_tablet":"","ocean_menu_typo_font_weight_mobile":"","ocean_menu_typo_transform":"","ocean_menu_typo_transform_tablet":"","ocean_menu_typo_transform_mobile":"","ocean_menu_typo_line_height":0,"ocean_menu_typo_line_height_tablet":0,"ocean_menu_typo_line_height_mobile":0,"ocean_menu_typo_line_height_unit":"","ocean_menu_typo_spacing":0,"ocean_menu_typo_spacing_tablet":0,"ocean_menu_typo_spacing_mobile":0,"ocean_menu_typo_spacing_unit":"","ocean_menu_link_color":"","ocean_menu_link_color_hover":"","ocean_menu_link_color_active":"","ocean_menu_link_background":"","ocean_menu_link_hover_background":"","ocean_menu_link_active_background":"","ocean_menu_social_links_bg":"","ocean_menu_social_hover_links_bg":"","ocean_menu_social_links_color":"","ocean_menu_social_hover_links_color":"","ocean_disable_title":"default","ocean_disable_heading":"default","ocean_post_title":"","ocean_post_subheading":"","ocean_post_title_style":"","ocean_post_title_background_color":"","ocean_post_title_background":0,"ocean_post_title_bg_image_position":"","ocean_post_title_bg_image_attachment":"","ocean_post_title_bg_image_repeat":"","ocean_post_title_bg_image_size":"","ocean_post_title_height":0,"ocean_post_title_bg_overlay":0.5,"ocean_post_title_bg_overlay_color":"","ocean_disable_breadcrumbs":"default","ocean_breadcrumbs_color":"","ocean_breadcrumbs_separator_color":"","ocean_breadcrumbs_links_color":"","ocean_breadcrumbs_links_hover_color":"","ocean_display_footer_widgets":"default","ocean_display_footer_bottom":"default","ocean_custom_footer_template":"0","ocean_post_oembed":"","ocean_post_self_hosted_media":"","ocean_post_video_embed":"","ocean_link_format":"","ocean_link_format_target":"self","ocean_quote_format":"","ocean_quote_format_link":"post","ocean_gallery_link_images":"off","ocean_gallery_id":[],"footnotes":""},"categories":[2,64,67],"tags":[32,12,31,68],"class_list":["post-1139","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","category-debugging","category-testing","tag-debugging","tag-python","tag-python3","tag-unit-testing","entry","has-media"],"_links":{"self":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts\/1139","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/comments?post=1139"}],"version-history":[{"count":4,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts\/1139\/revisions"}],"predecessor-version":[{"id":1284,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts\/1139\/revisions\/1284"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/media\/1141"}],"wp:attachment":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/media?parent=1139"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/categories?post=1139"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/tags?post=1139"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}