Python में unittest से Testing
In this page:
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
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
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
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
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
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)
Chapter Quiz — Complete all 15 topics to unlock
0/15 topics done
Complete these topics first:
- Python PEP 8 Style Guide
- Python Debugging Techniques
- Python Testing with unittest
- Python Common Mistakes
- Python Interview Questions
- Python map() & filter()
- Python reduce()
- Python zip() & enumerate()
- Python sorted() & key Functions
- Python Comprehensions Advanced
- Python Turtle Graphics
- Python tkinter Introduction
- Python tkinter Widgets
- Python pygame Introduction
- Python Mini Projects