pytest

A collection of 28 Posts

pytest fixtures nuts and bolts - revisited

A series on pytest fixtures gets updated

Unravelling t-strings with pytest

Brett Cannon recently released an article explaining Python 3.14's new t-strings. This article has pytest versions of the code from Brett's article.

Testing some tidbits with pytest

I noticed a fun post by Ned Batchelder called "Testing some tidbits". The post looks at different ways to see if a string has only 0 or 1 in it. I'm expanding on that with some pytest code to test the different ways.

pytest 8 is here

A few notes on some cool additions in pytest 8

pytest tips and tricks

This is a set of tips/tricks for learning and using pytest.

pytest expect fixture plugin, iteration 1

This is the first iteration that implements ’expect’ as a fixture.

This is really the third attempt at an ’expect()’ implementation that …

pytest delayed assert / multiple failure plugin, iteration 1

Further thoughts on the way to pytest-check

Delayed assert / multiple failures per test

The start of thinking that led to pytest-check

Mixing pytest fixture scope

Fixtures can only use other fixtures with equal or wider scope

pytest session scoped fixtures

We can use conftest.py to define fixtures that can be used by multiple files and still only run once per session

Using pytest fixtures by naming them

Including the fixture name as a parameter

Modularity - pytest fixtures using other fixtures

Fixtures can use other fixtures

Using multiple pytest fixtures

Separate your setup into separate pieces

Parametrizing pytest fixtures with params

Running fixtures (and the tests that depend on them) multiple times with different values

pytest fixture scope

Startup and teardown can surround functions, classes, modules/files, directories, or the entire test session.

pytest fixture return value

Return whatever value you want to the test

pytest fixture teardown

Including teardown code in a fixture

Using pytest fixtures with mark.usefixtures

If you want to apply a fixture to all tests in a class or file

Using pytest autouse fixtures

autouse is powerful and potentially dangerous

Basic pytest fixtures example

Fixtures are used to set up stuff before a test runs

pytest xUnit style fixtures

Using xUnit style with pytest

pytest fixtures nuts and bolts

fixtures reference

pytest support for unittest style fixtures

They behave the same

Run a single test class with unittest and pytest

How to, with examples

pytest debug print logging in real time

How to get print to work with tests

Running doctest from pytest

You can run some doctests from pytest, according to the documentation.
However, it can be a bit fiddly.

With my examples of putting doctests in text files, if I …

Running unittest from pytest

To show how pytest handles unittests, here’s a sample run of pytest on the simple unittests I wrote in the unittest introduction:

$ pytest …

pytest introduction

I think of pytest as the run-anything, no boilerplate, no required api, use-this-unless-you-have-a-reason-not-to test framework.
This is really where testing …