Glossary

pytest

pytest is the de facto standard testing framework for Python. It uses plain assert statements instead of special assertion methods, automatically discovers test files and functions by naming convention (test_*.py files, test_* functions), and provides detailed, readable failure output that shows exactly what was asserted and what the actual values were.

One of pytest's most powerful features is its fixture system. Fixtures are functions decorated with @pytest.fixture that provide test dependencies — database connections, temporary files, mock objects, application instances. Fixtures can be scoped (function, class, module, session), composed, and parameterised. They replace the setUp/tearDown pattern of unittest with a more flexible dependency injection approach.

pytest supports parameterised tests (@pytest.mark.parametrize), which run the same test with multiple sets of inputs; markers for categorising and selecting tests; plugins (there are hundreds, including pytest-cov for coverage, pytest-mock for mocking, and pytest-asyncio for async tests); and conftest.py files that share fixtures across test modules. While Python ships with unittest in the standard library, pytest is overwhelmingly preferred in the Python community for its simplicity, power, and extensibility.

Related terms: Fixture, Mock, Coverage

Discussed in:

This site is currently in Beta. Please email Chris Paton (cpaton@gmail.com) with any suggestions, questions or comments.