Unit testing
لم يُترجم بعد؛ يُعرض باللغة الإنجليزية
A unit test runs one small piece of code with input whose right answer you already know, and checks the output with an assertion. Follow test_add as it calls add(2, 3), see pytest run every test in a second, catch a broken change straight away, and grow the code test-first: red, green, refactor.
5 أسئلة سريعة
اختر إجابة لتعرف السبب. تُحفَظ نتيجتك مع تقدّمك.
01What does the line assert add(2, 3) == 5 do when add(2, 3) returns 4?
assert checks that its expression is true. 4 == 5 is false, so Python raises an AssertionError and pytest reports the test as failed.
02Which function will pytest collect and run as a test, if it sits in test_calc.py?
By default pytest looks in files named test_*.py (or *_test.py) and runs the functions whose names start with test.
03Which of these is the best edge case for add?
A negative number and an answer of zero sit at the edge of ordinary use, where bugs like to hide. The others are all the same ordinary case.
04In test-driven development, what comes first?
Red: write a test for what you want next and watch it fail. Green: write just enough code to pass. Refactor: tidy up while the tests stay green.
05Why would a test use a mock instead of a real web service?
A mock is a stand-in that gives a fixed answer, so the test does not wait on the network or change when the service does. It still checks your code.
المسرد
- Unit
- The smallest piece of code you can test on its own, usually one function.
- Unit test
- A small function that calls a unit with known input and checks the output with an assertion.
- Assertion
- A check that must be true. In Python, assert raises an AssertionError when its expression is false.
- pytest
- A popular Python test runner. It finds test_*.py files, runs every test function and prints how many passed and failed.
- Edge case
- Input at the limits of normal use, such as zero, negative numbers, empty lists or very big values.
- Test-driven development (TDD)
- Writing the test before the code, in a cycle: red (failing test), green (just enough code), refactor (tidy up).
- Refactor
- Tidying the shape of code. Strictly it should not change what the code does; passing tests show the cases they check still work, but only those cases.
- Mock
- A stand-in object that replaces a slow or outside part, such as a web service, and gives a fixed answer in a test.
الملاحظات والمصادر
- test_calc.py also starts with the line from calc import add, so the tests can call add. The card leaves it out to save room.
- pytest is a package you install with pip (pip install pytest). Python also ships its own test framework, unittest, which writes tests as classes.
- In pytest a test fails when its assert is false or when anything inside it raises an error, like the TypeError in the red step.
- Every result in this figure was checked by running these exact files with pytest 9.1.1 on Python 3.14. The terminal is trimmed to fit: real pytest prints the file name before the dots (test_calc.py ..) and how long the run took.
- Strictly, a refactor changes the structure of code but not its behaviour. The *nums version also changes what add does for inputs no test checks (add() and add(5) now work), which is why passing tests are only as good as the cases they cover.
- A mock is not always the right tool: too many mocks can test the mocks instead of the code. Use them for the slow or outside parts.
- Python Language Reference: The assert statement
- pytest documentation: How to write and report assertions in tests
- pytest documentation: Conventions for Python test discovery
- pytest documentation: Get Started
- Python Library Reference: unittest.mock
- Python Tutorial: More on Defining Functions (default values, arbitrary argument lists)
- Martin Fowler: UnitTest
- Martin Fowler: TestDrivenDevelopment
اختبار يتاح بعد الخطوة الأخيرة.