liveBook · Manning

Pytest

As explained in Practices of the Python Pro

pytest is a Python testing framework that discovers and runs tests and provides features for assertions, fixtures, and test configuration.

pytest is a Python testing framework for discovering and running tests with concise, readable assert statements. It helps check that new features work and that existing behavior remains correct after changes. section 5.0 section 5.5

Use pytest to keep tests straightforward and easy to organize:

A small pytest test

class TestProduct:\n def test_generate_sku(self):\n product = Product('shoes', 'S', 'black')\n assert product.generate_sku() == 'SHOES-S-BLACK'\n\nRun the suite with pytest. The command reports the platform, collected tests, individual results, and an overall summary. section 5.5.1 section 5.5.2

$ pytest
========== test session starts ==========
platform darwin -- Python 3.7.3, pytest-5.0.1, py-1.8.0,  #1
 pluggy-0.12.0
rootdir: /path/to/ecommerce/project
collected 15 items                                        #2

test_cart.py ............    [ 80%]                       #3
test_product.py ..           [ 93%]
test_tax.py .                [100%]

======= 15 passed in 0.12 seconds =======                 #4

Inspect a sample pytest session showing automatic collection, progress, individual test files, and the final pass count. section 5.5

How Practices of the Python Pro covers it

How deep it goes
Explains assertions, discovery, organization, output, and unittest conversion, while deferring pytest’s broader feature set.
Examples
Uses Product SKU tests, assert comparisons, automatic discovery, and a command-line session reporting fifteen passing tests.
What it assumes
Assumes basic Python test-writing familiarity while introducing pytest’s concise syntax and organization from unittest comparisons.
What you can do after
Apply pytest by organizing test classes, writing assert-based tests, running pytest, and converting unittest tests incrementally.
How it connects
Connects pytest with Python testing, unittest, unit testing, integration testing, test discovery, and test-driven development.

Also explained in

Compare how 11 books explain pytest →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Practices of the Python Pro. The section links point to the parts of the book it draws on.