PYTHON LESSON 25 OF 32

Tests that give you confidence

A test checks an expected behavior. assert fails if a condition is false. Test ordinary inputs, edge cases and errors. A few passing tests increase confidence but do not prove every possible input is correct.

हिन्दी में समझें

टेस्ट अपेक्षित व्यवहार जाँचता है। assert गलत शर्त पर विफल होता है। सामान्य, किनारे के और गलत इनपुट जाँचें। कुछ टेस्ट पास होने से हर इनपुट सही सिद्ध नहीं होता।

अभ्यास: add(0, 0) के लिए assert जोड़ें और 3 tests passed प्रिंट करें।

Hinglish mein samjhein

Test expected behavior check karta hai. assert false hone par fail hota hai. Normal, edge cases aur errors test karo. Kuch tests pass hone se sab inputs guaranteed sahi nahi hote.

Practice: add(0,0) ka assert jodo aur 3 tests passed print karo.

A small Python example

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

assert add(2, 3) == 5
assert add(-1, 1) == 0
print("2 tests passed")

The browser playground runs real Python with Pyodide. Use sample variables instead of interactive input(). Browser package support differs from desktop Python.

Try this example in the Python playground →

Practise this idea

Add an assertion for add(0, 0) and print 3 tests passed.

Check answer & explanation
def add(a, b):
    return a + b

assert add(2, 3) == 5
assert add(-1, 1) == 0
assert add(0,0)==0
print("3 tests passed")

The added zero-input assertion covers another behavior. Three test cases pass before the success message prints.

Stuck in the course? Choose Check answer for either the challenge or quiz. Read the explanation and continue to the next lesson at your own pace.

Check your understanding

When does assert fail?

  1. When its condition is false
  2. Always
  3. Only on strings
Check the quiz answer

When its condition is false

A false assertion raises AssertionError.

References & corrections

Use the official Python documentation for full language rules. Read how these lessons are made and checked. Found a mistake? Report it to Excel Keys with this lesson URL and a reproducible example.

FROM LEARNING TO BUSINESS RESULTS

Need a dashboard for your team?

Excel Keys designs custom Excel and HTML dashboards around your own KPIs and Excel files. They run offline in the browser, so your data stays on your computer.