unittest and pytest
Scenario: The project needs an automated command that finds and runs every test.
New words, in plain English
- Test runner - a tool that discovers and executes tests
- unittest - Python's built-in class-based testing framework
- pytest - a popular runner supporting plain assert tests
- Discovery - finding tests by naming conventions
Run built-in tests with python -m unittest; run pytest with python -m pytest. Pytest rewrites asserts for helpful reports and can also run many unittest suites.
Analogy: A Python program is like a clear set of instructions for a careful helper: names label things, indentation groups steps, and errors explain where the helper became confused.
A small, real example
import unittest
class AddTests(unittest.TestCase):
def test_add(self):
self.assertEqual(2 + 3, 5)
# Equivalent pytest style:
def test_add(): assert 2 + 3 == 5
Tip: Invoke tools through python -m ... to use the active environment's interpreter.
Goal: Practice this idea in the py-pytest-suite challenge at/labs/python. Fix the broken program, run it yourself, then usecheckto prove the real end state.