Testing
pytest
Python's main testing framework: plain asserts, fixtures and parametrization.
What it is
pytest is an open source community project started by Holger Krekel. Tests are plain functions with a simple assert, no base classes or special methods. pytest rewrites assert to show a detailed diff on failure.
Its core strength is fixtures (setup and teardown of dependencies injected by argument name), parametrization (@pytest.mark.parametrize) and a plugin ecosystem (pytest-cov, pytest-asyncio, pytest-django, pytest-xdist).
It also runs unittest-style tests. pytest is used to test applications, FastAPI and Django APIs, as well as data and ML code.
Use it when
- Any Python project: libraries, web APIs, scripts.
- You need parametrization and reusable fixtures.
- You want readable failure messages without extra code.
Avoid it when
- A project where the team strictly sticks to
unittestand does not want to change. - Tests are not in Python: use your language's tools.
Key concepts
- Fixture
- A function with
@pytest.fixturethat prepares a resource; a test receives it by argument name. - Parametrize
- One test runs over a set of inputs.
- Markers
- Marks like
skip,xfailand custom ones to select tests. - monkeypatch and tmp_path
- Built-in fixtures for patching and temporary files.
- conftest.py
- A file with shared fixtures for a test directory.
Example
Interview questions
1. What is a fixture and why?
A function that prepares data or a resource for a test and cleans up after it if needed (via
yield). A test just names it in arguments.scopesets the lifetime: function, module, session.2. How do you parametrize a test?
With the
@pytest.mark.parametrize('a, b', [...])decorator. pytest creates a separate test for each value pair with its own result.3. How does pytest differ from unittest?
Plain functions and
assertinstead of classes andassertEqualmethods, powerful fixtures, parametrization and plugins. It also runsunittesttests.4. How do you replace an external dependency?
With the
monkeypatchfixture orunittest.mock.patch. Patch network calls, environment variables and time so the test is isolated.
Interview questions from the bank
- Does high code coverage guarantee the quality of tests?JuniorTesting
- What is TDD, and what are its advantages and drawbacks?JuniorTesting
- What makes a good test structure, and what are Arrange, Act, Assert?JuniorTesting
- Why use parameterized tests?JuniorTesting
- What is a regression test, and how do you add one when fixing a bug?JuniorTesting
- How do you test code that works with a database?MiddleTesting
- What are fixtures in pytest, and why are they better than setUp/tearDown?MiddleTesting
- How do you organize test data: fixtures, factories, a shared dump?MiddleTesting
Related technologies
- PythonA readable general-purpose language: web, data, automation and machine learning.
- FastAPIA modern Python API framework built on type hints, with automatic validation and OpenAPI docs.
- DjangoA batteries-included Python framework: ORM, admin, auth and templates.
- FlaskA Python microframework: a minimal core with routing and templates, the rest through extensions.
Logos belong to their owners and are used for identification only. Icons: Simple Icons (CC0), Devicon (MIT).