<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Vaibhav Solanki on Medium]]></title>
        <description><![CDATA[Stories by Vaibhav Solanki on Medium]]></description>
        <link>https://medium.com/@vaibhavsolanki?source=rss-2597c616ffc0------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*Egk3RVhdizxnC7jH.</url>
            <title>Stories by Vaibhav Solanki on Medium</title>
            <link>https://medium.com/@vaibhavsolanki?source=rss-2597c616ffc0------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 00:20:03 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@vaibhavsolanki/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Testing more in less time: subTest in Python unittest module]]></title>
            <link>https://medium.com/@vaibhavsolanki/testing-more-in-less-time-subtest-in-python-unittest-module-92a81abe4785?source=rss-2597c616ffc0------2</link>
            <guid isPermaLink="false">https://medium.com/p/92a81abe4785</guid>
            <category><![CDATA[efficient-coding]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[unittest]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[python3]]></category>
            <dc:creator><![CDATA[Vaibhav Solanki]]></dc:creator>
            <pubDate>Thu, 02 Feb 2023 12:35:47 GMT</pubDate>
            <atom:updated>2023-02-02T12:35:47.528Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*raWtLJHVZAL9KTIY" /><figcaption>Photo by <a href="https://unsplash.com/@emilep?utm_source=medium&amp;utm_medium=referral">Emile Perron</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>So you have created your amazing script or added some new code to an existing application, now comes the dreaded work of writing tests for your code in order to make sure it works as you expect it to and don’t get that alarming page/message that your build brought down the production.</p><p>Writing new test cases or modifying existing test cases as per the new code is always time-consuming to get 100% coverage. You might have to write multiple test cases that check your code with different data types of input, different parameters for the function arguments, and possibly everything that could go wrong during the new code execution and catch those exceptions correctly.</p><p>Let’s see how subTest from Python unittest module can help you write good test cases while saving you some time.</p><p>Let’s take an example of a function inside a calculator app to keep things simple. Here is the code:</p><pre>def add_numbers(n,m):<br>    &quot;&quot;&quot; returns the sum of n and m &quot;&quot;&quot;<br>    return n+m</pre><p>Now, you wrote the most common scenario test case for your code.</p><pre>import unittest<br><br>class sumtest(unittest.TestCase):<br>    def test_01_positive_int_sum(self):<br>        self.assertEqual(add_numbers(5, 5), 10)</pre><p>You run your test case and get a successful response.</p><pre>.<br>----------------------------------------------------------------------<br>Ran 1 test in 0.000s<br><br>OK</pre><p>great, now you think of adding more test cases by providing negative numbers.</p><pre>class sumtest(unittest.TestCase):<br>    def test_01_positive_int_sum(self):<br>        self.assertEqual(add_numbers(5, 5), 10)<br><br>    def test_02_higher_negative_number(self):<br>        self.assertLess(add_numbers(-5, 4),0)<br>    <br>    def test_03_lower_negative_number(self):<br>        self.assertGreater(add_numbers(5,-2),0)<br>    <br>    def test_04_both_negative_number(self):<br>        self.assertLessEqual(add_numbers(-5,-6),-5)<br><br>    def test_05_equal_but_diff_sign(self):<br>        self.assertEqual(add_numbers(-5,5),0)</pre><p>Here, it’s obvious the code was copied and the test name and arguments were modified as per need which resulted in 5 successful tests.</p><pre>.....<br>----------------------------------------------------------------------<br>Ran 5 tests in 0.000s<br><br>OK</pre><p>Let’s try to do better by writing a loop to iterate the values we expect to add.</p><pre>class sumtest(unittest.TestCase):<br>    def test_01_test_all_additions(self):<br>        sum_number_mapping = {<br>            10: (5, 5),<br>            -1: (-5, 4),<br>            3: (5,-2),<br>            -11: (-5,-6)<br>        }<br>        for result,numbers in sum_number_mapping.items():<br>            self.assertEqual(add_numbers(*numbers), result)</pre><p>One test to check all conditions. Let’s run the test case.</p><pre>.<br>----------------------------------------------------------------------<br>Ran 1 test in 0.000s<br><br>OK</pre><p>Let’s see if the test case can handle incorrect results for the addition of two numbers. Let’s change -1 to -2 in the second option.</p><p>Here is the test run result:</p><pre> ======================================================================<br>FAIL: test_01_test_all_additions (__main__.sumtest)<br>----------------------------------------------------------------------<br>Traceback (most recent call last):<br>  File &quot;add_numbers.py&quot;, line 33, in test_01_test_all_additions<br>    self.assertEqual(add_numbers(*numbers), result)<br>AssertionError: -1 != -2<br><br>----------------------------------------------------------------------<br>Ran 1 test in 0.000s<br><br>FAILED (failures=1)</pre><p>The problem here is that it’s not evident which option failed.</p><p>So, let’s try to fix that.</p><p><strong>Enter subText</strong></p><p>From python 3.4, there is a new feature in unittest module called subTest. Let’s implement that in our test case.</p><pre>def test_01_test_all_additions(self):<br>    sum_number_mapping = {<br>        10: (5, 5),<br>        -2: (-5, 4),<br>        3: (5,-2),<br>        -11: (-5,-6)<br>    }<br>    for result,numbers in sum_number_mapping.items():<br>        with self.subTest(numbers):<br>            self.assertEqual(add_numbers(*numbers), result)</pre><p>Now, the output clearly tells us which particular number tuple failed in the title of the test case.</p><pre>======================================================================<br>FAIL: test_01_test_all_additions (__main__.sumtest) [(-5, 4)]<br>----------------------------------------------------------------------<br>Traceback (most recent call last):<br>  File &quot;add_numbers.py&quot;, line 34, in test_01_test_all_additions<br>    self.assertEqual(add_numbers(*numbers), result)<br>AssertionError: -1 != -2<br><br>----------------------------------------------------------------------<br>Ran 1 test in 0.001s<br><br>FAILED (failures=1)</pre><p>Let’s see how the result would look like with multiple failures.</p><p>Let’s add more wrong key, value pairs in <em>sum_number_mapping</em>.</p><pre>def test_01_test_all_additions(self):<br>    sum_number_mapping = {<br>        10: (5, 5),<br>        -2: (-5, 4),<br>        3: (5,-2),<br>        -12: (-5,-6)<br>    }<br>    for result,numbers in sum_number_mapping.items():<br>        with self.subTest(numbers):<br>            self.assertEqual(add_numbers(*numbers), result)</pre><pre>======================================================================<br>FAIL: test_01_test_all_additions (__main__.sumtest) [(-5, 4)]<br>----------------------------------------------------------------------<br>Traceback (most recent call last):<br>  File &quot;add_numbers.py&quot;, line 34, in test_01_test_all_additions<br>    self.assertEqual(add_numbers(*numbers), result)<br>AssertionError: -1 != -2<br><br>======================================================================<br>FAIL: test_01_test_all_additions (__main__.sumtest) [(-5, -6)]<br>----------------------------------------------------------------------<br>Traceback (most recent call last):<br>  File &quot;add_numbers.py&quot;, line 34, in test_01_test_all_additions<br>    self.assertEqual(add_numbers(*numbers), result)<br>AssertionError: -11 != -12<br><br>----------------------------------------------------------------------<br>Ran 1 test in 0.001s<br><br>FAILED (failures=2)</pre><p>Quite evidently, this makes your test cases more readable, the errors are easily distinguishable, and you can confidently flex by writing smaller but efficient test cases.</p><p>Happy Coding!!!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=92a81abe4785" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>