liveBook · Manning

Pytest

As explained in Tiny Python Projects: Learn coding and testing with puzzles and games

pytest is a Python testing framework that discovers and runs tests and reports their results.

pytest runs Python tests and reports which checks pass or fail. Use it repeatedly while developing and refactoring programs. section 1.3 section 1.12

Useful command patterns include:

  • Run a test file with verbose output: pytest -v test.py. section 1.3
  • Stop after the first failure and show details: pytest -xv test.py. section 1.3
  • Use make test as a shortcut when a Makefile delegates to pytest. section 1.12

Reading a test run

A successful run can collect five tests and report each as PASSED, ending with “5 passed.” With -xv, pytest stops at the first failing test and displays the assertion and error details. section 1.12 section 1.3

$ pytest -v test.py
============================= test session starts ==============================
...
collected 5 items
 
test.py::test_exists PASSED                                              [ 20%] #1
test.py::test_runnable PASSED                                            [ 40%] #2
test.py::test_executable FAILED                                          [ 60%] #3
test.py::test_usage FAILED                                               [ 80%] #4
test.py::test_input FAILED                                               [100%] #5
 
=================================== FAILURES ===================================

Inspect a verbose pytest run showing collected tests, failures, assertions, and command output. section 1.3

How Tiny Python Projects: Learn coding and testing with puzzles and games covers it

How deep it goes
Demonstrates command flags, pass/fail output, and integration versus unit tests without discussing pytest internals.
Examples
Uses hello.py and a bottles-of-beer program, with test.py, terminal listings, assertions, and Makefile shortcuts.
What it assumes
Assumes basic Python programming and requires the pytest module to be installed before running commands.
What you can do after
Diagnose failing assertions, verify complete programs, and safely compare alternative implementations.
How it connects
Connects pytest with Python programs, integration tests, unit tests, Makefiles, and refactoring.

Also explained in

Compare how 11 books explain pytest →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Tiny Python Projects: Learn coding and testing with puzzles and games. The section links point to the parts of the book it draws on.