Skip to content
IT glossary
TestingUpdated December 3, 2026

pytest

A Python testing framework that finds and runs automated tests. When a check fails, it shows what failed and often displays the values involved so you can see what went wrong.

What pytest does

pytest runs tests that describe how Python code should behave. A test can be a plain function: call the code, then use Python’s assert to compare the result with what you expected. You don’t need a class or a special assertion method. When an assertion fails, pytest usually shows the values in the expression, not just the line number. That makes assert total == 100 useful even without a custom error message.

Finding and running tests

Under its default naming rules, pytest looks for files such as test_cart.py or cart_test.py and collects functions whose names start with test_. After installing pytest in your project’s Python environment, run python -m pytest from the project root. To run just one file, use python -m pytest tests/test_cart.py. If a new test is missing from the results, python -m pytest --collect-only shows what pytest found without running it. Check the file and function names before debugging the test itself.

Example: give each input its own result

import pytest

from cart import total_price


@pytest.mark.parametrize("prices, expected", [([], 0), ([100, 50], 150)])
def test_total_price(prices, expected):
    assert total_price(prices) == expected

Assume total_price adds up the prices in a shopping cart. This test checks both an empty cart and one with two items. @pytest.mark.parametrize gives each input and expected result its own test case in the report. A loop inside a single test would stop at the first failed assertion, leaving later inputs unchecked in that run. To test a different kind of outcome, such as invalid input raising ValueError, wrap the call in with pytest.raises(ValueError):.

Fixtures: set up what a test needs

A fixture supplies a test with data or a resource. To request one, put its name in the test function’s parameter list. For example, pytest’s built-in tmp_path fixture gives each test that requests it a separate temporary directory. Define your own fixtures with @pytest.fixture, and share them across test files by placing them in a conftest.py file in the relevant directory. A fixture can use yield to hand a resource to the test, then close or clean it up afterward.

Fixtures are function-scoped by default. Broader scopes can save time when setup is expensive, but sharing mutable state can make a test pass on its own and fail after another test changes that state. Choose a broader scope only when the tests can still run independently.

Where it fits and what interviewers ask

Teams run pytest locally and in CI, for quick unit tests as well as integration tests that use a database or a test server. The runner doesn’t determine the test level: a test that exercises a real database is still an integration test even if it looks like a short Python function.

In a Python interview, you might be asked to test a normal return value and an expected exception, turn repetitive cases into a parametrized test, or explain why a shared fixture causes intermittent failures. Be ready to explain the distinction: fixtures set up what tests depend on; parametrization runs the same check as separate cases with different inputs.