Testing with pytest
Testing with pytest
A test in pytest is a function whose name starts with test_, in a file whose name starts with test_, containing at least one assert. There is no base class to inherit and no special assertion library to learn. When an assert fails, pytest rewrites it to show the values on each side, so assert ticket.status is Status.ESCALATED failing tells you that the status was Status.OPEN without you writing a message.
That minimalism is why pytest wins. The rest of the framework is three ideas layered on top: fixtures for the things a test needs before it starts, parametrize for running one test body over many inputs, and plugins, of which pytest-asyncio is the one an AI backend cannot do without.
Continue reading
Pro unlocks every video lesson, the full notes and runnable code across the Python with AI course, from the language itself to agents, MCP, Langfuse and deployment.