Skip to content
Home / Fundamentals

Pytest Built-In Fixtures

Pytest fixtures are a powerful tool for writing and organizing tests in Python. They allow you to set up test dependencies and control the execution of your tests in a consistent and predictable way. Pytest provides a number of built-in fixtures that can be used for a variety of testing needs, including creating temporary directories, modifying and restoring attribute values, capturing output streams, and more. In this article, we will explore some of the most commonly used built-in pytest fixtures and provide examples of how to use them in your tests.

tmpdir

This fixture creates a temporary directory and returns its path, which can be used for testing file system operations.

import os

def test_tmpdir(tmpdir):
    # create a file in the temporary directory
    p = tmpdir.mkdir("sub").join("hello.txt")
    p.write("content")

    # check that the file was created
    assert os.path.exists(p.strpath)

tmpdir_factory

This fixture allows you to create multiple temporary directories and return their paths.

def test_tmpdir_factory(tmpdir_factory):
    # create a temporary directory
    tmpdir = tmpdir_factory.mktemp("temp")

    # create a file in the temporary directory
    p = tmpdir.join("hello.txt")
    p.write("content")

    # check that the file was created
    assert os.path.exists(p.strpath)

monkeypatch

This fixture allows you to modify the values of certain attributes or functions during the test, and then restore them to their original values after the test has completed.

import math

def test_monkeypatch(monkeypatch):
    def mock_sqrt(x):
        return 4

    # replace the math.sqrt function with the mock function
    monkeypatch.setattr(math, "sqrt", mock_sqrt)

    # the math.sqrt function should now return 4
    assert math.sqrt(16) == 4

capsys

This fixture captures the standard output and error streams (e.g., print statements) during the test, and allows you to assert on the captured output.

def test_capsys(capsys):
    print("Hello, world!")

    # capture the standard output and error streams
    captured = capsys.readouterr()

    # check that the output was as expected
    assert captured.out == "Hello, world!\n"

capfd

This fixture is similar to capsys, but captures the output of sys.stdout and sys.stderr to file descriptors, rather than strings.

import sys

def test_capfd(capfd):
    # redirect the standard output stream to a file descriptor
    sys.stdout = capfd.out

    print("Hello, world!")

    # capture the standard output stream
    out, _ = capfd.readouterr()

    # check that the output was as expected
    assert out == "Hello, world!\n"

recwarn

This fixture captures warning messages issued during the test, and allows you to assert on the captured warnings.

import warnings

def test_recwarn(recwarn):
    warnings.warn("This is a warning")

    # capture the warning messages
    w = recwarn.pop()

    # check that the warning message was as expected
    assert str(w.message) == "This is a warning"

client (for Flask application)

This fixture is useful for testing web applications and returns a FlaskClient object, which can be used to make HTTP requests to the application.

django_client (for Django application)

This fixture is similar to client, but is specific to Django applications and returns a Client object.

def test_django_client(django_client):
    # send an HTTP GET request to the application
    response = django_client.get("/")
    
    # check that the response status code was 200 (OK)