Introduction
Testing ka matlab hota hai apne code ko check karna ki wo sahi se kaam kar raha hai ya nahi. Debugging ka matlab hota hai galti (bug) ko dhundhna aur fix karna. Dono process ek professional Python developer ke liye bahut important hain.
1️⃣ Manual Testing
Shuru me hum simple programs manually test karte hain — jaise code run karke dekha ki output sahi aa raha hai ya nahi. Lekin jaise-jaise project bada hota hai, manual testing time-consuming ho jata hai. Isliye hum automated testing tools use karte hain.
2️⃣ assert Statement
assert ek simple tarika hai condition test karne ka. Agar condition galat hui to program error (AssertionError) dega.
def add(a, b):
return a + b
assert add(2, 3) == 5
assert add(-1, 1) == 0
print("Sab tests pass ho gaye!")
Agar koi bhi assert fail hua to program wahi ruk jaayega aur error dikha dega.
Agar sab sahi hai to "Sab tests pass ho gaye!" print hoga.
3️⃣ unittest Module
Python ke built-in testing framework ka naam hai unittest.
Isse hum test cases likh sakte hain aur automatically run kar sakte hain.
import unittest
def multiply(a, b):
return a * b
class TestMath(unittest.TestCase):
def test_multiply(self):
self.assertEqual(multiply(3, 4), 12)
self.assertEqual(multiply(-2, 5), -10)
if __name__ == "__main__":
unittest.main()
Har test class ko unittest.TestCase se inherit karte hain.
assertEqual() check karta hai ki actual aur expected values match kar rahi hain ya nahi.
Agar koi test fail hota hai, framework clearly dikha deta hai kaunsa fail hua.
4️⃣ pytest Framework
pytest ek popular aur simple testing framework hai jo beginners aur professionals dono use karte hain.
Install karne ke liye likhiye:
pip install pytest
Example:
# test_calc.py
from calc import add
def test_add():
assert add(2, 3) == 5
assert add(-1, 1) == 0
Ab terminal me run karo:
pytest test_calc.py
Pytest automatically sab functions jinka naam test_ se start hota hai, unhe test ke roop me run karta hai.
Fail hone par wo clear report deta hai aur pass hone par “✓ PASSED” likha aata hai.
5️⃣ Debugging (Code me bug dhundhna)
Python me built-in debugger aata hai — pdb.
Isse hum code ko step-by-step run karke variables ka state dekh sakte hain.
import pdb
def divide(a, b):
pdb.set_trace() # debugger start hoga
return a / b
print(divide(10, 2))
Jab code pdb.set_trace() par aata hai, program ruk jaata hai aur aap interactive mode me command likh sakte ho jaise:
• n – next line execute karo
• p variable – variable ka value dekho
• c – continue till next breakpoint
6️⃣ Logging
Debugging me help karne ke liye Python ka logging module use hota hai.
Isse hum apne program ke important events ko log file me likh sakte hain.
import logging
logging.basicConfig(filename="app.log", level=logging.INFO)
logging.info("Program start ho gaya")
logging.warning("Low disk space!")
logging.error("Kuch galat ho gaya!")
Logging se hume code ka flow samajhne me madad milti hai jab kuch galat hota hai. Ye production code me bhi use hota hai taaki hum system ke issues track kar sakein.