Python Testing with pytest
274 pages
English
PDF
4.5

Python Testing with pytest

Python Testing with pytest

Author:

Year:

Summary:

Introduction

The book "Python Testing with pytest" (second edition, 2022) by Brian Okken is one of the most authoritative and practical guides to testing in Python using the pytest framework. If you want not just to write tests, but to do it efficiently, elegantly, and with minimal code, this book will become your desktop reference. It is aimed at developers already familiar with the basics of Python and offers a deep dive into the capabilities of pytest, starting from basic assert statements and ending with complex plugins and CI/CD integration.

What the book is about

The book is structured as a sequential practical guide. The author does not overload the reader with theory but gets straight to the point: showing how to write simple tests and then gradually complicating the examples. The main topics covered in the book are:

  • pytest basics: installation, running, writing first tests, using assert.
  • Fixtures: creation, scope, fixture parametrization, built-in fixtures (tmp_path, capsys, monkeypatch).
  • Test parametrization: the @pytest.mark.parametrize decorator, combining parameters, generating test data.
  • Marks: built-in markers (skip, xfail, parametrize), creating custom markers, filtering tests.
  • Plugins and extensions: overview of popular plugins (pytest-cov, pytest-mock, pytest-django, pytest-flask), writing custom plugins.
  • Integration with other tools: unittest, doctest, tox, nox, CI systems (GitHub Actions, Jenkins).
  • Testing web applications and APIs: working with Flask, Django, FastAPI, testing HTTP requests.
  • Advanced techniques: using conftest.py, overriding fixtures, dynamic test generation, testing asynchronous code.

Who this book is for

The book will be useful for a wide range of readers:

  • Beginner Python developers who already know how to write code but want to learn testing from scratch.
  • Experienced programmers using unittest or nose who want to switch to the more modern and flexible pytest.
  • Full-stack and backend developers building web applications with Flask, Django, or FastAPI.
  • DevOps engineers responsible for setting up CI/CD and code quality.
  • Team leads and technical managers who want to introduce a testing culture in their team.

What the reader will learn

After reading the book, you will be able to:

  • Write clean, concise, and maintainable tests using pytest.
  • Effectively use fixtures to set up and clean up the test environment.
  • Parametrize tests to check multiple scenarios without code duplication.
  • Apply markers for grouping, skipping, and expecting failures in tests.
  • Integrate pytest with popular libraries (mock, coverage, selenium).
  • Configure automatic test execution in CI systems.
  • Write custom plugins to extend pytest's capabilities.
  • Test web applications, REST APIs, and asynchronous code.

Strengths of the book

  • Practical focus. Each chapter contains numerous code examples that can be immediately run and modified. The author not only explains concepts but shows how to apply them in real projects.
  • Relevance. The second edition (2022) covers the latest versions of pytest (7.x), new features such as fixture parametrization, improved async support, and modern CI/CD tools.
  • Structure. The book is logically organized: from simple to complex. It can be read sequentially or used as a reference, transition

Recommendations