Skip to content
BENIBIX Learn

pytest

Development🧪 Testing & QualityTest Frameworks

Also known as Pytest, py.test

The best-known test tool for Python. It finds and runs your tests.

Think of it like

Think of it like a helper. They find every quiz in the house and mark them all.

Example

Lucy writes test_total in test_cart.py. She types pytest, and it finds the test, runs it and prints 1 passed.

Why it matters

Tests are short to write, and failures show exactly which values did not match.

Definition

pytest is the most popular tool for testing Python code. A test is a plain function whose name starts with test_, inside a file named test_ something. You check results with Python's own assert, and pytest shows the values when one fails. Fixtures let tests share set-up, like a fresh database. Hundreds of plugins add things like coverage reports.

Example code

console
$ pytest -v test_cart.py
test_cart.py::test_total PASSED     [100%]
1 passed in 0.02s
pytest names each test it ran and whether it passed.

Common mistake

Naming a test check_total instead of test_total. pytest skips it, so it never runs and never fails.

Key takeaways

  1. 1
    Plain functions. Named test_ something.
  2. 2
    Plain assert. Shows values on failure.
  3. 3
    Fixtures. Shared set-up.