Testing & Quality Assurance

pytest

The tool that lets you write 'assert this works' in plain Python and get a genuinely helpful answer when it doesn't.

Install it: pip install pytest

What does it do?

Pytest is the tool developers use to check that their code actually does what it’s supposed to, by writing small test functions that make claims, like adding 2 and 2 should equal 4, and then running all of them automatically. If something fails, pytest doesn’t just say nope, it shows exactly what value was expected versus what actually happened, like a teacher marking a wrong answer and showing their work. It scales from a single quick test to massive test suites with thousands of checks, and its plugin ecosystem lets teams bolt on extra capabilities for almost any testing need imaginable.

See it in action

This defines a simple function that adds two numbers and a test that checks the answer is correct; running the ‘pytest’ command in a terminal checks it automatically.

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

def test_add():
    assert add(2, 2) == 4

Why would a non-developer care?

Every time a software update doesn’t break the app you use every day, there’s a good chance an automated test caught the problem before it ever reached you. Pytest is one of the most widely used tools making that kind of quiet, invisible quality control possible across the Python world.

Real-world examples

Pytest has become the de facto standard testing tool in the Python ecosystem, used by an enormous share of open-source projects and companies, often preferred over Python’s own built-in unittest module because of its far friendlier syntax and error messages. A team without automated tests tends to discover bugs the hard way, from angry users, rather than catching them before release, which is precisely the gap pytest closes.

Who uses it

Virtually any Python developer or team that writes automated tests, from solo hobbyists to large engineering organizations.

How it compares to alternatives

It’s the dominant alternative to Python’s built-in unittest module and to nose2, largely because of a much simpler syntax, plain assert statements instead of unittest’s more verbose assertEqual-style methods.

Fun fact

Pytest grew out of an earlier project called py.test, part of a broader testing toolkit, before splitting off to become its own hugely popular standalone project.

New to Python and want to actually try libraries like this yourself?

Find a beginner-friendly course

Related libraries