Thuta Learning
ရှာဖွေရန်
AdvancedProgrammingbeginner

Unit Testing

စိတ်လျှော့ပါ။ ဒီခန်းကို စာအုပ်လိုမဟုတ်ဘဲ စကားပြောသလိုပဲ၊ နားလည်လွယ်အောင် ရှင်းပါမယ်။

Unit Testing သည် code များ၏ individual units များကို test လုပ်ခြင်းဖြစ်သည်။ Python ၏ unittest module သို့မဟုတ် pytest ကို အသုံးပြုနိုင်သည်။

✅ Testing Benefits:

• Find bugs early

• Confident refactoring

• Documentation

• Better design

🎯 Test Structure:

• Arrange (setup)

• Act (execute)

• Assert (verify)

python
import unittest

# Function to test
def add(a, b):
    return a + b

def divide(a, b):
    if b == 0:
        raise ValueError("Cannot divide by zero")
    return a / b

# Test class
class TestMathFunctions(unittest.TestCase):
    def test_add_positive_numbers(self):
        result = add(5, 3)
        self.assertEqual(result, 8)
    
    def test_add_negative_numbers(self):
        result = add(-5, -3)
        self.assertEqual(result, -8)
    
    def test_divide_normal(self):
        result = divide(10, 2)
        self.assertEqual(result, 5)
    
    def test_divide_by_zero(self):
        with self.assertRaises(ValueError):
            divide(10, 0)

# Run tests (simplified output)
print("Running tests...")
print("test_add_positive_numbers ... OK")
print("test_add_negative_numbers ... OK")
print("test_divide_normal ... OK")
print("test_divide_by_zero ... OK")
print("\nAll tests passed!")
You should see
Running tests... test_add_positive_numbers ... OK test_add_negative_numbers ... OK test_divide_normal ... OK test_divide_by_zero ... OK All tests passed!
Unit Testing | Thuta Learning