מפגש 19: assert, פונקציות בדיקה, fixtures ו-parametrize.Session 19: assert, test functions, fixtures and parametrize.
assert, פונקציות בדיקה, fixtures ו-parametrize. בסוף המפגש מוסיפים צעד לפרויקט המלווה.assert, test functions, fixtures and parametrize. At the end we add a step to the companion project.
מה נעשה היוםToday's plan
למהWhy
pytestpytest
כישלוןFailure
קצוותEdges
fixturesFixtures
TDDTDD
עוברים על סדר היום ומזכירים את הפרויקט המלווה.Walk through the plan and mention the companion project.
בדיקה אוטומטית רצה בשנייהAn automated test runs in a second
אומץ לשנות קודCourage to change code
כל פעם ששיניתם קוד, הרצתם ובדקתם בעיניים שהכול עדיין עובד. בדיקה אוטומטית עושה את זה בשבילכם: היא מריצה את הקוד עם קלט ידוע ובודקת עם assert שהתוצאה נכונה. ככל שהפרויקט גדל, הבדיקות הן מה שנותן אומץ לשנות בלי לשבור. התרגילים באתר הזה בנויים בדיוק כך.Every time you changed code, you ran it and eyeballed that everything still works. An automated test does that for you: it runs code with known input and asserts the result is right. As a project grows, tests give you the courage to change it without breaking things. This site's exercises are built exactly this way.
מריצים ומשווים לניחושים.Run it and compare with the guesses.
אנלוגיהAnalogy
בדיקות הן כמו רשימת בדיקה של טייס לפני המראה: גם טייס ותיק לא סומך על הזיכרון, כי פספוס אחד יקר מדי.Tests are like a pilot's pre-flight checklist: even a veteran doesn't trust memory, because one miss costs too much.
שואלים אם מישהו מכיר דוגמה נוספת מהחיים.Ask for another real-life example.
נושא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
pytest מחפש בתיקייה קבצים ששמם מתחיל ב-test_, ובתוכם פונקציות שמתחילות ב-test_, ומריץ כל אחת בנפרד. נקודה ירוקה לכל בדיקה שעברה. הנה קובץ קוד shelf.py וקובץ בדיקות test_shelf.py:pytest looks in the folder for files whose name starts with test_, and inside them for functions starting with test_, and runs each separately. A dot for every passing test. Here's a code file shelf.py and a test file test_shelf.py:
אנלוגיהAnalogy
pytest הוא כמו בוחן שעובר על כל השאלות במבחן לפי הסדר ומסמן וי או איקס, בלי להתעייף ובלי לדלג.pytest is like an examiner going through every question in order, marking each right or wrong, never tiring and never skipping.
שואלים אם מישהו מכיר דוגמה נוספת מהחיים.Ask for another real-life example.
נושאTopic 3
כישלוןFailure
לקרוא כישלוןReading a failure
F במקום נקודהF instead of a dot
pytest מראה את הערכים בפועלpytest shows the actual values
כישלון = מידע, לא אסוןA failure = information, not disaster
כשבדיקה נכשלת, pytest מסמן F ומראה בדיוק באיזו שורה ומה השתבש. בואו נכניס באג: find משווה בלי להתעלם מאותיות גדולות. הבדיקה תופסת אותו מיד:When a test fails, pytest marks F and shows exactly which line and what went wrong. Let's plant a bug: find compares without ignoring case. The test catches it at once:
אנלוגיהAnalogy
כישלון בבדיקה הוא כמו גלאי עשן שמצפצף: מעצבן לרגע, אבל עדיף בהרבה על לגלות את השריפה מאוחר.A failing test is like a smoke alarm beeping: annoying for a moment, but far better than finding the fire late.
שואלים אם מישהו מכיר דוגמה נוספת מהחיים.Ask for another real-life example.
נושאTopic 4
קצוותEdges
בדיקת שגיאות ומקרי קצהTesting errors and edge cases
with pytest.raises(ValueError):with pytest.raises(ValueError):
לפעמים ההתנהגות הנכונה היא לזרוק שגיאה. pytest.raises בודק שהשגיאה באמת נזרקת. ואיפה כדאי לבדוק? במקרי קצה: רשימה ריקה, מחרוזת של רווחים, אפס. וכל באג שמצאתם מקבל בדיקה משלו, כדי שלא יחזור בשקט (רגרסיה).Sometimes the right behaviour is to raise an error. pytest.raises checks the error really is raised. Where to test? At edge cases: an empty list, a string of spaces, zero. And every bug you find gets its own test, so it can't quietly come back (a regression).
אנלוגיהAnalogy
מקרי קצה הם כמו לבדוק גשר עם המשאית הכי כבדה ולא רק עם אופניים: אם הוא מחזיק את הקצה, הוא מחזיק את הכול.Edge cases are like testing a bridge with the heaviest truck, not just a bicycle: if it holds the extreme, it holds everything.
שואלים אם מישהו מכיר דוגמה נוספת מהחיים.Ask for another real-life example.
נושא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
כשכמה בדיקות צריכות את אותו מדף, כותבים fixture: פונקציה עם @pytest.fixture שמחזירה אותו, ובדיקה שרוצה אותו פשוט מקבלת פרמטר באותו שם. כל בדיקה מקבלת עותק חדש. parametrize מריץ בדיקה אחת על רשימת קלטים, וכל קלט נספר כבדיקה נפרדת.When several tests need the same shelf, write a fixture: a function with @pytest.fixture that returns it, and a test that wants it just takes a parameter with that name. Each test gets a fresh copy. parametrize runs one test over a list of inputs, each counted as a separate test.
אנלוגיהAnalogy
fixture הוא כמו מטבח שמסודר מחדש לפני כל מנה בתחרות בישול: כל מתחרה מקבל אותם מצרכים טריים.A fixture is like a kitchen reset before every dish in a cooking contest: each contestant gets the same fresh ingredients.
שואלים אם מישהו מכיר דוגמה נוספת מהחיים.Ask for another real-life example.
נושאTopic 6
TDDTDD
לכתוב בדיקה קודם (TDD)Test first (TDD)
אדום: בדיקה שנכשלתRed: a failing test
ירוק: הקוד הכי פשוט שמעבירGreen: the simplest code that passes
סידור: משפרים כשהבדיקות מגינותRefactor: improve while tests protect you
ב-TDD כותבים קודם את הבדיקה, רואים אותה נכשלת (אדום), כותבים את הקוד הפשוט ביותר שמעביר אותה (ירוק), ואז משפרים את הקוד בביטחון, כי הבדיקות ישמרו עליכם. זה מכריח לחשוב מה בדיוק רוצים לפני שכותבים.In TDD you write the test first, watch it fail (red), write the simplest code that passes (green), then improve the code with confidence, because the tests protect you. It forces you to decide exactly what you want before you write.
אנלוגיהAnalogy
TDD הוא כמו לכתוב את השאלות למבחן לפני שמלמדים: כך יודעים בדיוק מה התלמיד צריך לדעת בסוף.TDD is like writing the exam questions before teaching: you know exactly what the student must know at the end.
שואלים אם מישהו מכיר דוגמה נוספת מהחיים.Ask for another real-life example.