ConfigsApr 12, 2026·2 min read

pytest — The Python Testing Framework That Scales

pytest makes it easy to write small tests, yet scales to support complex functional testing. Fixtures, parameterization, plugins, markers, and a rich assertion introspection system. The de facto testing standard for the Python ecosystem.

TL;DR
pytest provides fixtures, parameterization, and plugins for Python testing at any scale.
§01

What it is

pytest is the de facto standard testing framework for Python. It supports simple unit tests, complex functional tests, fixtures for dependency injection, parameterized test generation, markers, and a rich assertion introspection system that provides detailed failure messages without boilerplate.

pytest is for Python developers at every level -- from solo scripts to large enterprise codebases with thousands of test cases.

The project is actively maintained with regular releases and a growing user community. Documentation covers common use cases, and the open-source nature means you can inspect the source code, contribute fixes, and adapt the tool to your specific requirements.

§02

How it saves time or tokens

pytest eliminates the ceremony required by unittest. No test classes needed, no self.assertEqual calls. A plain function with an assert statement is a valid test. Fixtures compose automatically, parameterize generates test matrices from data, and the plugin ecosystem (1000+ plugins) handles everything from coverage to parallel execution.

§03

How to use

  1. Install pytest via pip.
  2. Write test functions in files named test_*.py using plain assert statements.
  3. Run pytest from your project root to discover and execute all tests.
§04

Example

# test_calc.py
def add(a, b):
    return a + b

def test_add_integers():
    assert add(2, 3) == 5

def test_add_strings():
    assert add('hello ', 'world') == 'hello world'

# Fixtures for shared setup
import pytest

@pytest.fixture
def db_connection():
    conn = create_connection()
    yield conn
    conn.close()

def test_query(db_connection):
    result = db_connection.execute('SELECT 1')
    assert result is not None

# Parameterized tests
@pytest.mark.parametrize('input,expected', [
    (1, 2), (2, 4), (3, 6)
])
def test_double(input, expected):
    assert input * 2 == expected
§05

Related on TokRepo

§06

Common pitfalls

  • Test files must follow the test_*.py naming convention or pytest will not discover them. Same for test functions: they must start with test_.
  • Fixture scope matters. A session-scoped fixture runs once; a function-scoped fixture runs per test. Using the wrong scope causes either slow tests or shared state bugs.
  • Importing the module under test with a relative path fails when running pytest from a different directory. Use pyproject.toml or conftest.py to set the Python path correctly.

Before adopting this tool, evaluate whether it fits your team's existing workflow. Read the official documentation thoroughly, and start with a small proof-of-concept rather than a full migration. Community forums, GitHub issues, and Stack Overflow are valuable resources when you encounter edge cases not covered in the documentation.

Frequently Asked Questions

How is pytest different from unittest?+

pytest uses plain functions and assert statements instead of test classes and self.assertEqual methods. It provides automatic fixture injection, parameterized tests, and detailed assertion introspection out of the box. unittest requires more boilerplate for the same functionality.

What are pytest fixtures?+

Fixtures are functions decorated with @pytest.fixture that provide setup and teardown logic. They are injected into test functions by name. Fixtures can be scoped to function, class, module, or session level, and they compose automatically.

How do I run tests in parallel with pytest?+

Install the pytest-xdist plugin with pip install pytest-xdist, then run pytest -n auto to distribute tests across all available CPU cores. This can reduce test suite execution time dramatically on multi-core machines.

Can pytest run unittest-style tests?+

Yes. pytest is backwards compatible with unittest. It can discover and run unittest.TestCase subclasses without any modification. You can gradually migrate from unittest to pytest-style tests.

How do parameterized tests work in pytest?+

Use the @pytest.mark.parametrize decorator to supply multiple input/output pairs to a single test function. pytest generates a separate test case for each parameter set, with clear reporting on which combination failed.

Citations (3)

Discussion

Sign in to join the discussion.
No comments yet. Be the first to share your thoughts.

Related Assets