מדריך pytest מקיף בעל מספר חלקים.
בחלק זה נכיר את pytest, ונריץ את הטסט הראשון.
התקנת pytest
את pytest נתקין בתוך סביבת virtualenv רגילה.
אפשר לעשות זאת באמצעות PyCharm, אולם כאן נדגים זאת בסביבת cli:
yuval> python3 -m venv localenv
yuval> source localenv/bin/activate
(localenv) yuval> pip install -U pytest
Collecting pytest
Using cached pytest-6.1.1-py3-none-any.whl (272 kB)
Collecting packaging
Using cached packaging-20.4-py2.py3-none-any.whl (37 kB)
Collecting attrs>=17.4.0
Using cached attrs-20.2.0-py2.py3-none-any.whl (48 kB)
.....
.....
(localenv) yuval>
(localenv) yuval> pytest --version
pytest 6.1.1
(localenv) yuval>
כדי לאפשר את pytest ב – PyCharm, פתח את החלון הבא:
File->settings->

ובחר את pytest כ – Default Test Runner.
לאחר מכן, בזמן ההרצה (כפי שנראה בהמשך), יופיע כפתור הרצה בצד שמאל, המאפשר הרצת pytest:

הטסט הראשון
ניצור קובץ חדש בשם first_test.py, עם הקוד הבא:
def func(x):
return x + 1
def test_answer():
assert func(3) == 5
אפשר להריץ את pytest כבר כעת:
(localenv) yuval> ls
first_test.py localenv __pycache__
(localenv) yuval>
(localenv) yuval> pytest
================================================= test session starts ==================================================
platform linux -- Python 3.8.5, pytest-6.1.1, py-1.9.0, pluggy-0.13.1
rootdir: /home/yuval/Documents/languages/python/progs/pytest
collected 1 item
first_test.py F [100%]
======================================================= FAILURES =======================================================
_____________________________________________________ test_answer ______________________________________________________
def test_answer():
> assert func(3) == 5
E assert 4 == 5
E + where 4 = func(3)
first_test.py:6: AssertionError
=============================================== short test summary info ================================================
FAILED first_test.py::test_answer - assert 4 == 5
================================================== 1 failed in 0.02s ===================================================
(localenv) yuval>
בהרצה כזו, pytest יריץ:
- את כל הקבצים ששמם נראה כמו: test_*.py או *_test.py
- pytest יחפש קבצים כאלה ב – directory המקומי, וגם ב – sub-directories שלו.
- בתוך קבצים אלה, pytest יחפש פונקציות ששמן מתחיל במילה test (אפשר "לשחק" עם שם הפונקציה ב PyCharm ולראות איך כפתור הריצה מופיע ונעלם)
- רשימת החוקים לגבי גילוי אוטומטי של קבצי pytest נמצאת כאן.
שימוש ב assert
הפקודה assert בפייתון מוודאת שערך מסויים הוא truthy (כלומר נכון, דומה ל true). במקרה שהביטוי אינו כזה, הפקודה תייצר AssertionError exception.
ל – pytest יש תמיכה חזקה בשימוש בתכונה טבעית זו של python.
הנה דוגמאות ל assert:
>>> assert 1
>>> assert 0
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AssertionError
>>> assert ['a','b']
>>> assert []
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AssertionError
>>>
>>> assert 4>5
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AssertionError
>>>
אפשר לבדוק שפונקציה מייצרת exception כאשר קוראים לה. לשם כך צריך להיעזר בפונקציית עזרה (helper):
import pytest
def f():
raise SystemExit(1)
def test_mytest():
with pytest.raises(SystemExit):
f()
הנה ההרצה:
(localenv) yuval> pytest second_test.py
================================================= test session starts ==================================================
platform linux -- Python 3.8.5, pytest-6.1.1, py-1.9.0, pluggy-0.13.1
rootdir: /home/yuval/Documents/languages/python/progs/pytest
collected 1 item
second_test.py . [100%]
================================================== 1 passed in 0.02s ===================================================
(localenv) yuval>