ApprovalTests

17.4.3 · active · verified Fri Apr 17

ApprovalTests.Python is a powerful assertion/verification library for Python, aiding in test-driven development by comparing generated output to a pre-approved baseline. It is currently at version 17.4.3 and maintains a fairly active release cadence, with several minor releases and occasional major updates.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates a basic approval test. Running this code will create a `.received.txt` file with the generated content and, if configured, open an external diff tool (like Beyond Compare or Meld) to compare it against a `.approved.txt` file. On the first run, you'll need to approve the `.received` content, usually by copying it to the `.approved` file via the diff tool. Subsequent runs will pass if the content has not changed.

from approvaltests import verify, Options

def generate_report_content():
    return (
        "User Report:\n" 
        "- ID: 123\n" 
        "- Name: Alice Wonderland\n" 
        "- Email: alice@example.com\n"
    )

# This will generate a .received.txt file and attempt to open a diff tool.
# The first run will likely show a diff, subsequent runs pass if content matches.
verify(generate_report_content())

view raw JSON →