Testing with pytest
لم يُترجم بعد؛ يُعرض باللغة الإنجليزية
A test is a small function that calls your code and checks the answer with assert. pytest is the tool that finds those functions, runs each one and reports what happened. Follow one run of test_area.py: one test passes by luck, the other fails, pytest's report points straight at a typo, and after the fix both tests pass.
5 أسئلة سريعة
اختر إجابة لتعرف السبب. تُحفَظ نتيجتك مع تقدّمك.
01Which of these will pytest collect as a test, with its default settings?
pytest looks in files named test_*.py or *_test.py, and inside them collects functions whose names start with test. Only test_total() in test_total.py matches both rules.
02A test runs assert area(3, 3) == 9 and the condition is True. What happens?
assert does nothing when its condition is True. A test passes when it finishes without raising an error.
03pytest prints test_area.py .F What does that mean?
pytest prints one character per test, in order: a dot for a pass and an F for a failure. A failing test does not stop the others from running.
04Why could test_square never catch the bug in return w * w ?
With w = h = 3, the buggy w * w and the correct w * h are both 9. A test with different sides, such as area(2, 5), is needed to expose the typo.
05Plain Python stops a false assert with a bare AssertionError. How does pytest show assert 4 == 10, where 4 = area(2, 5) ?
pytest's assertion rewriting changes the asserts in test modules at import time so a failure can report each intermediate value.
المسرد
- Test
- A small function that calls your code and checks the result, usually with assert. In pytest its name starts with test.
- pytest
- A widely used third-party test runner for Python: it finds tests, runs them and reports the results. Install it with pip.
- Test runner
- A tool that collects your tests, runs each one and reports which passed and which failed.
- assert
- A statement that does nothing when its condition is True and raises AssertionError when it is False.
- AssertionError
- The exception a false assert raises. pytest catches it and marks the test as failed.
- Collection (test discovery)
- pytest's first phase: finding test_*.py and *_test.py files and the test functions inside them.
- Pass / fail
- A test passes when it finishes without raising an error, and fails when it raises one, such as AssertionError.
- Exit code
- The number a program returns when it ends. pytest returns 0 when all tests passed and 1 when some failed.
الملاحظات والمصادر
- pytest is not part of the standard library: install it with python -m pip install pytest, ideally inside a virtual environment. python -m pytest runs it too. Python's built-in framework is unittest, and pytest runs unittest-style tests as well.
- With no arguments, pytest searches the current folder and its subfolders for test_*.py and *_test.py files, and collects functions whose names start with test (and test methods inside classes whose names start with Test).
- pytest rewrites the assert statements in test modules as it imports them, which is how it can print the values (where 4 = area(2, 5)). Plain Python raises a bare AssertionError, and skips assert entirely under python -O.
- The figure shortens the terminal: real pytest output also shows the platform and folder, a progress percentage, a short summary of failures and the run time, framed by full-width lines of = signs.
- pytest exits with code 0 when every test passed and 1 when some failed, which is how automated build systems know a change broke something.
- pytest documentation: Get started
- pytest documentation: Conventions for Python test discovery
- pytest documentation: How to write and report assertions in tests
- pytest documentation: Exit codes
- pytest documentation: How to use unittest-based tests with pytest
- Python reference: The assert statement
- Python library: Built-in exceptions (AssertionError)
- Python library: unittest, the standard testing framework
- Python: Installing Python modules (pip)
اختبار يتاح بعد الخطوة الأخيرة.