← Back to Python Course | Chapter 14: Advanced Python & Tools | Lesson 3 of 15

Python में unittest से Testing

unittest आपको छोटी-छोटी checks लिखने देता है जो यह test करती हैं कि आपका code सही जवाब दे रहा है या नहीं, बिल्कुल एक teacher की तरह जो आपका homework अपने आप check करती है। इन्हें दोबारा चलाने से पता चलता है कि कुछ टूटा तो नहीं।

Unit Testing का परिचय

Unit testing का मतलब है ऐसा code लिखना जो automatically यह verify करे कि आपके application के अलग-अलग हिस्से -- functions या classes -- सही तरीके से behave कर रहे हैं, बजाय इसके कि हर बदलाव के बाद हाथ से behavior दोबारा check किया जाए।

unittest, Python का built-in testing framework है, जो कई अन्य languages में मिलने वाले xUnit family पर आधारित है।

उदाहरण: Introduction to Unit Testing

python
import unittest

def add(a, b):
    return a + b

class TestAdd(unittest.TestCase):
    def test_add(self):
        self.assertEqual(add(2, 3), 5)  # verifies add() behaves correctly

unittest.main(argv=[""], exit=False)  # runs the tests without exiting the interpreter

Assertions लिखना

assertEqual(a, b) जैसे assertion methods यह जाँचते हैं कि दो values match करती हैं या नहीं, और अगर नहीं करतीं तो दोनों values का नाम लेते हुए एक स्पष्ट, specific failure message देते हैं -- यह एक bare assert a == b से कहीं ज़्यादा जानकारीपूर्ण है, जो सिर्फ यह बताता है कि assertion fail हुई, बिना यह दिखाए कि actual values क्या थीं।

उदाहरण: Writing Assertions

python
import unittest

class TestMath(unittest.TestCase):
    def test_equal(self):
        self.assertEqual(2 + 2, 4)  # gives a clear failure message naming both values

unittest.main(argv=[""], exit=False)

Exceptions को Test करना

assertRaises() एक context manager की तरह इस्तेमाल होता है यह verify करने के लिए कि invalid input देने पर code का कोई block कोई specific exception type raise करता है या नहीं, जिससे आप error-handling paths को भी उतनी ही सख़्ती से test कर सकते हैं जितनी सामान्य success path को।

उदाहरण: Testing Exceptions

python
import unittest

def divide(a, b):
    return a / b

class TestDivide(unittest.TestCase):
    def test_zero_division(self):
        with self.assertRaises(ZeroDivisionError):  # verifies this specific exception is raised
            divide(1, 0)

unittest.main(argv=[""], exit=False)

Setup और Teardown Methods

setUp(), test class के हर individual test method से पहले automatically चलता है, और tearDown() हर test के बाद automatically चलता है -- यह shared fixtures (जैसे mock files या database connections) तैयार करने और फिर उन्हें साफ करने की standard जगह है, बिना हर एक test में वह setup code दोहराए।

उदाहरण: Setup and Teardown Methods

python
import unittest

class TestExample(unittest.TestCase):
    def setUp(self):  # runs automatically before every test method
        self.value = 10

    def test_value(self):
        self.assertEqual(self.value, 10)

unittest.main(argv=[""], exit=False)

Script के अंदर unittest चलाना

if __name__ == __main__: guard के अंदर unittest.main() जोड़ने से आप उस script को directly execute करके file की सभी tests चला सकते हैं, बिना कोई अलग test-runner command invoke किए -- यह छोटे, single-file test suites के लिए सुविधाजनक है।

उदाहरण: Running unittest inside Script

python
import unittest

class TestBasic(unittest.TestCase):
    def test_true(self):
        self.assertTrue(1 == 1)

if __name__ == "__main__":  # lets this file run its tests directly
    unittest.main(argv=[""], exit=False)
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.