נבנה על ידיBuilt by Ariel Yohimovich

חזרה למפגשBack
PythonPython

בדיקות עם pytestTesting with pytest

מפגש 19: assert, פונקציות בדיקה, fixtures ו-parametrize.Session 19: assert, test functions, fixtures and parametrize.

מה נעשה היוםToday's plan

  • למהWhy
  • pytestpytest
  • כישלוןFailure
  • קצוותEdges
  • fixturesFixtures
  • TDDTDD
נושאTopic 1

למהWhy

למה לבדוק?Why test?

  • בדיקה ידנית = לשכוח משהוManual checking = forgetting something
  • בדיקה אוטומטית רצה בשנייהAn automated test runs in a second
  • אומץ לשנות קודCourage to change code
שאלה לכיתהQuestion for the class

מה ידפיס הקוד?What will this print?

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

def test_add():
    assert add(2, 3) == 5
    assert add(-1, 1) == 0
    assert add(0, 0) == 0

test_add()
print("כל הבדיקות עברו")
def add(a, b):
    return a + b

def test_add():
    assert add(2, 3) == 5
    assert add(-1, 1) == 0
    assert add(0, 0) == 0

test_add()
print("all tests passed")

    למהWhy

    first_test.py
    def add(a, b):
        return a + b
    
    def test_add():
        assert add(2, 3) == 5
        assert add(-1, 1) == 0
        assert add(0, 0) == 0
    
    test_add()
    print("כל הבדיקות עברו")
    def add(a, b):
        return a + b
    
    def test_add():
        assert add(2, 3) == 5
        assert add(-1, 1) == 0
        assert add(0, 0) == 0
    
    test_add()
    print("all tests passed")
    פלטOutput
    כל הבדיקות עברו
    all tests passed

    אנלוגיהAnalogy

    בדיקות הן כמו רשימת בדיקה של טייס לפני המראה: גם טייס ותיק לא סומך על הזיכרון, כי פספוס אחד יקר מדי.Tests are like a pilot's pre-flight checklist: even a veteran doesn't trust memory, because one miss costs too much.
    נושאTopic 2

    pytestpytest

    הרצה עם pytestRunning pytest

    • קובץ בשם test_*.pyA file named test_*.py
    • פונקציות בשם test_*Functions named test_*
    • python3 -m pytest מוצא ומריץ הכולpython3 -m pytest finds and runs everything

    אנלוגיהAnalogy

    pytest הוא כמו בוחן שעובר על כל השאלות במבחן לפי הסדר ומסמן וי או איקס, בלי להתעייף ובלי לדלג.pytest is like an examiner going through every question in order, marking each right or wrong, never tiring and never skipping.
    נושאTopic 3

    כישלוןFailure

    לקרוא כישלוןReading a failure

    • F במקום נקודהF instead of a dot
    • pytest מראה את הערכים בפועלpytest shows the actual values
    • כישלון = מידע, לא אסוןA failure = information, not disaster

    אנלוגיהAnalogy

    כישלון בבדיקה הוא כמו גלאי עשן שמצפצף: מעצבן לרגע, אבל עדיף בהרבה על לגלות את השריפה מאוחר.A failing test is like a smoke alarm beeping: annoying for a moment, but far better than finding the fire late.
    נושאTopic 4

    קצוותEdges

    בדיקת שגיאות ומקרי קצהTesting errors and edge cases

    אנלוגיהAnalogy

    מקרי קצה הם כמו לבדוק גשר עם המשאית הכי כבדה ולא רק עם אופניים: אם הוא מחזיק את הקצה, הוא מחזיק את הכול.Edge cases are like testing a bridge with the heaviest truck, not just a bicycle: if it holds the extreme, it holds everything.
    נושאTopic 5

    fixturesFixtures

    fixtures ו-parametrizeFixtures and parametrize

    • fixture: נתונים מוכנים לכל בדיקהfixture: ready data for each test
    • @pytest.mark.parametrize: כמה קלטים, בדיקה אחת@pytest.mark.parametrize: many inputs, one test
    • כל בדיקה מקבלת עותק טריEach test gets a fresh copy

    אנלוגיהAnalogy

    fixture הוא כמו מטבח שמסודר מחדש לפני כל מנה בתחרות בישול: כל מתחרה מקבל אותם מצרכים טריים.A fixture is like a kitchen reset before every dish in a cooking contest: each contestant gets the same fresh ingredients.
    נושאTopic 6

    TDDTDD

    לכתוב בדיקה קודם (TDD)Test first (TDD)

    • אדום: בדיקה שנכשלתRed: a failing test
    • ירוק: הקוד הכי פשוט שמעבירGreen: the simplest code that passes
    • סידור: משפרים כשהבדיקות מגינותRefactor: improve while tests protect you

    אנלוגיהAnalogy

    TDD הוא כמו לכתוב את השאלות למבחן לפני שמלמדים: כך יודעים בדיוק מה התלמיד צריך לדעת בסוף.TDD is like writing the exam questions before teaching: you know exactly what the student must know at the end.

    בדיקה ידנית = לשכוח משהוManual checking = forgetting something

    בדיקה אוטומטית רצה בשנייהAn automated test runs in a second

    אומץ לשנות קודCourage to change code

    קובץ בשם test_*.pyA file named test_*.py

    פונקציות בשם test_*Functions named test_*

    python3 -m pytest מוצא ומריץ הכולpython3 -m pytest finds and runs everything

    F במקום נקודהF instead of a dot

    pytest מראה את הערכים בפועלpytest shows the actual values

    כישלון = מידע, לא אסוןA failure = information, not disaster

    with pytest.raises(ValueError):with pytest.raises(ValueError):

    מקרי קצה: ריק, אפס, גבולEdge cases: empty, zero, limit

    בדיקה לכל באג = אין רגרסיהA test per bug = no regression

    fixture: נתונים מוכנים לכל בדיקהfixture: ready data for each test

    @pytest.mark.parametrize: כמה קלטים, בדיקה אחת@pytest.mark.parametrize: many inputs, one test

    כל בדיקה מקבלת עותק טריEach test gets a fresh copy

    אדום: בדיקה שנכשלתRed: a failing test

    מה למדנוWhat we learned

    • בדיקה ידנית = לשכוח משהוManual checking = forgetting something
    • קובץ בשם test_*.pyA file named test_*.py
    • F במקום נקודהF instead of a dot
    • with pytest.raises(ValueError):with pytest.raises(ValueError):
    • fixture: נתונים מוכנים לכל בדיקהfixture: ready data for each test
    • אדום: בדיקה שנכשלתRed: a failing test

    עכשיו לתרגולNow, practice

    5 תרגילים ו-2 אתגרים, עם בדיקה אוטומטית5 exercises and 2 challenges, auto-checked
    הערות מרצהSpeaker notes

    נבנה על ידיBuilt by Ariel Yohimovich