Skip to main content

ApprovalTests.Python

Contents

Capturing Human Intelligence - ApprovalTests is an open source assertion/verification library to aid testing.
approvaltests is the ApprovalTests port for Python.

For more information see: www.approvaltests.com.

PyPI version Python versions Build Status Build Status Build Status Discord

What can I use ApprovalTests for?

You can use ApprovalTests to verify objects that require more than a simple assert including long strings, large arrays, and complex hash structures and objects. ApprovalTests really shines when you need a more granular look at the test failure. Sometimes, trying to find a small difference in a long string printed to STDOUT is just too hard!
ApprovalTests solves this problem by providing reporters which let you view the test results in one of many popular diff utilities.

Getting Started

What Are Approvals

If you need to gain a better understanding or are new to this concept, start here.

New Projects

If you are starting a new project, we suggest you use the Starter Project. You can just clone this and go. It's great for exercises, katas, and green field projects.

Minimal Example Tutorial

If this is first time approvaltesting in python, consider starting here: Minimal Example Tutorial

Adding to Existing Projects

From pypi:

pip install approvaltests

Overview

Approvals work by comparing the test results to a golden master. If no golden master exists you can create a snapshot of the current test results and use that as the golden master. The reporter helps you manage the golden master.
Whenever your current results differ from the golden master, Approvals will launch an external application for you to examine the differences. Either you will update the master because you expected the changes and they are good, or you will go back to your code and update or roll back your changes to get your results back in line with the golden master.

Example using pytest

from approvaltests.approvals import verify


def test_simple() -> None:
    result = "Hello ApprovalTests"
    verify(result)

snippet source | anchor

Example using unittest

import unittest

from approvaltests.approvals import verify


class GettingStartedTest(unittest.TestCase):
    def test_simple(self) -> None:
        verify("Hello ApprovalTests")


if __name__ == "__main__":
    unittest.main()

snippet source | anchor

This example has the same behaviour as the pytest version, but uses the built-in test framework unittest instead.

Example using CLI

You can invoke a verify() call from the command line. This allows invoking python approvals from any other stack via subprocesses.

Usage

python -m approvaltests --test-id hello --received "hello world!"

or

python -m approvaltests -t hello -r "hello world!"

or

echo "hello world!" | python -m approvaltests -t hello

Argument Definitions

  • --test-id or -t: Test identifier used to name the approved.txt and received.txt files for the test.

  • --received or -r: The output of the program under test (a string) that is passed to the verify method.

    • stdin: Instead of providing a received argument, you may use stdin.

Reporters

A reporter controls how ApprovalTests handles a failing test result. The default behavior is to open a diff tool showing what you received vs what was approved. You can use this diff tool to change the approved to make the test pass the next time you run it. Reporters also have more advanced usages.

Support and Documentation

Missing Documentation?

If there is documentation you wish existed, please add a page request to this issue.

Dependencies

Python

ApprovalTests is tested on the following Python versions: 3.11, 3.12, 3.13, 3.14, 3.15.

For older versions of Python, either:

  • use an older version of ApprovalTests, or
  • use TextTest, or
  • hire us to help

Required packages

These dependencies are always required for approvaltests

pytest>=8.0.0
empty-files>=0.0.3
typing_extensions>=4.16.0

snippet source | anchor

Extra packages

These dependencies are needed if you are going to use the related functionality.
If you want the bare minimum you can use the pypi project approvaltests_minimal.

# For reporters.clipboard_reporter.ClipboardReporter
pyperclip>=1.5.29

# For approvals.verify_html()
beautifulsoup4>=4.9.0

# For pairwise_combinations.get_best_covering_pairs()
allpairspy>=2.1.0

# For utilities.logging.logging_approvals.verify_logging()
testfixtures >= 7.1.0, < 12
mock >= 5.1.0

# For reporters.intellij_reporter.IntelliJReporter
psutil >= 5.9.0

snippet source | anchor

For developers

Weekly Ensemble

The best way to contribute is to join our weekly mob/ensemble.

Pull Requests

Pull requests are welcomed, particularly those accompanied by automated tests.

To run the self-tests: ./build_and_test.sh

This will run the self-tests on several python versions. We support python 3.10 and above.

All pull requests will be pre-checked using GitHub actions to execute all these tests. You can see the results of test runs here.

Metadata

Release files for approvaltests 20.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for approvaltests 20.0.1
File Size Uploaded Provenance Actions
approvaltests-20.0.1.tar.gz 67.3 kB Without provenance Details for approvaltests-20.0.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for approvaltests 20.0.1
File Interpreter ABI Platform Actions
approvaltests-20.0.1-py3-none-any.whl Python 3 none any Details for approvaltests-20.0.1-py3-none-any.whl

Total release size: 135.5 kB

Release files / approvaltests-20.0.1.tar.gz

Download URL approvaltests-20.0.1.tar.gz
Size 67.3 kB
Tags Source
SHA-256 checksum
How to use checksums
55f6e66a06bf0e2d5ddf3a8b40181a4c82dedf766d193adbf9c19cdb74992ce9
BLAKE2b-256 checksum
How to use checksums
fb8c772e052134d691ac26563eff9bccf069e33a00deba430dcd84bb47149203
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.8

Release files / approvaltests-20.0.1-py3-none-any.whl

Download URL approvaltests-20.0.1-py3-none-any.whl
Size 68.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d1c771b1a6b2dad4b66f3930249e1afc05d3bc842e87e7c62ea6c3decbe601f8
BLAKE2b-256 checksum
How to use checksums
72a157581448c9b989970f97a26bedd0fb96e2efe0bd52dfb727b110b7e5d2cd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.8

Security for approvaltests 20.0.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

20.0.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page