Skip to content

These pytest examples are based on the book "Python Testing with pytest: Simple, Rapid, Effective, and Scalable", Second Edition, written by Brian Okken and published by The Pragmatic Programmers in 2022. They also cover pytest plugins. Special attention is paid to using Coverage.py, unittest.mock, and tox with pytest.

Notifications You must be signed in to change notification settings

ax-va/Pytest-Okken-2022

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Learning pytest

These pytest examples are based on the book "Python Testing with pytest: Simple, Rapid, Effective, and Scalable", Second Edition, written by Brian Okken and published by The Pragmatic Programmers in 2022. They also cover pytest plugins. Special attention is paid to using Coverage.py, unittest.mock, and tox with pytest.

Also see the pytest-summary directory for quick and easy information about pytest features. The Python versions used in the examples are 3.10, 3.11, and 3.12. The third-party packages are listed in requirements.txt.

Original source code

The link to the original source code is given on the official book's side

Original installable plugin for 15--building-plugins

Upgrade pip

$ python3 -m pip install -U pip

Install using pip

As of Python 3.3, the build-in venv package created a virtual environment python -m venv env_dir_name [--prompt my_proj]. The --prompt parameter is optional. If it is not supplied, the prompt will match the directory name. As of Python 3.9, providing --prompt . will tell venv to use the parent directory as the prompt.

See also:

Create a virtual environment, activate it on POSIX systems, and install pytest

$ python3 -m venv my_venv
$ source my_venv/bin/activate
(my_venv) ...$ pip install pytest

or using virtualenv

$ python3 -m pip install virtualenv
$ python3 -m virtualenv my_venv
$ source my_venv/bin/activate
(my_venv) ...$ pip install pytest

Deactivate the venv

(my_venv) ...$ deactivate

Create a virtual environment, activate it on Windows systems, and install pytest

>python -m venv my_venv
>my_venv\Scripts\activate.bat
(my_venv) ...>pip install pytest

Activate in PowerShell

>my_venv\Scripts\Activate.ps1

Install in editable mode from the current directory

(venv_editable) ...$ pip install -e .

Install in editable mode from a directory with optional dependencies for testing

(venv_editable) ...$ pip install -e "./cards_proj_failed/[test]"

[test] in the -e parameters refers to optional dependencies for testing given in pyproject.toml.

Other useful commands

(venv) ...$ pip --version
pip 24.2 from path/to/venv/lib/python3.11/site-packages/pip (python 3.11)
(venv) ...$ pip list
Package            Version
------------------ -----------
cachetools         5.5.0
cards              1.0.0
certifi            2024.8.30
...

See also:

Run pytest

Run a test modul:

$ pytest 01-introduction/test_01-1--passing.py

Run a test modul with the --verbose or -v flag:

$ pytest -v 01-introduction/test_01-1--passing.py

Run all tests starting with test_ or ending with _test in the current working directory without traceback

$ pytest --tb=no

Run tests given by their names or (sub)directories in which they are located

$ pytest --tb=no 01-introduction/test_01-1--passing.py 01-introduction/test_01-2--failing.py
$ pytest --tb=no 01-introduction

Run only specified functions

$ pytest -v 01-introduction/test_01-1--passing.py::test_passing

Conventions to keep your test code discoverable by pytest

  • test_<something>.py or <something>_test.py for files

  • test_<something> for methods and functions

  • Test<Something> for classes

Stop running tox if it runs too long

$ ps aux | grep tox
$ kill <id>

About

These pytest examples are based on the book "Python Testing with pytest: Simple, Rapid, Effective, and Scalable", Second Edition, written by Brian Okken and published by The Pragmatic Programmers in 2022. They also cover pytest plugins. Special attention is paid to using Coverage.py, unittest.mock, and tox with pytest.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages