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

Install the plugin pytest-approvaltests and use it to select a reporter:

pip install pytest-approvaltests
pytest --approvaltests-use-reporter='PythonNative'

The reporter is used both to alert you to changes in your test output, and to provide a tool to update the golden master. In this snippet, we chose the 'PythonNative' reporter when we ran the tests. For more information about selecting reporters see the documentation

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.10, 3.11, 3.12, 3.13, 3.14.

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.12.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.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

approvaltests_minimal-19.1.1.tar.gz (66.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

approvaltests_minimal-19.1.1-py3-none-any.whl (68.8 kB view details)

Uploaded Python 3

File details

Details for the file approvaltests_minimal-19.1.1.tar.gz.

File metadata

  • Download URL: approvaltests_minimal-19.1.1.tar.gz
  • Upload date:
  • Size: 66.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.14.6

File hashes

Hashes for approvaltests_minimal-19.1.1.tar.gz
Algorithm Hash digest
SHA256 0f21ebc35c5b9ceb0e24ef1f94b985f5f820a24fa1cbefc172b12dc5e7eb72bb
MD5 df569d634cccd7d08f3358b70914653b
BLAKE2b-256 e2ca8198a97336405d9bc3110ecfa1d424ad48da6858598eb7d8a829b1fcdc90

See more details on using hashes here.

File details

Details for the file approvaltests_minimal-19.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for approvaltests_minimal-19.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 4c201d73280573c9b084c8a846edaa63ddcc048b59d5e793550a9f2bb9b08b09
MD5 0cc79a2199d8a2b5d7b918f61ac4fbfa
BLAKE2b-256 62ae8b0b892861d6526ec3a8eba94ee40912b4362ad4bd8e8e3588b100672943

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

19.1.1 This release

2 files

19.1.0

2 files

19.0.0

2 files

18.2.0

2 files

18.1.2

2 files

18.1.1

2 files

18.1.0

2 files

18.0.5

2 files

18.0.3

2 files

18.0.1

2 files

18.0.0

2 files

17.4.3

2 files

17.4.2

1 file

17.4.1

2 files

17.4.0

2 files

17.3.0

2 files

17.2.1

2 files

17.2.0

2 files

17.1.1

2 files

17.1.0

2 files

17.0.0

2 files

16.3.0

2 files

16.2.2

2 files

16.2.1

2 files

16.2.0

2 files

16.1.0

2 files

16.0.1

2 files

16.0.0

2 files

15.3.2

2 files

15.2.1

2 files

15.2.0

2 files

15.1.1

2 files

15.1.0

2 files

15.0.0

2 files

14.7.1

2 files

14.6.0

2 files

14.5.0

2 files

14.4.0

2 files

14.3.1

2 files

14.3.0

2 files

14.2.0

2 files

14.1.0

2 files

14.0.0

2 files

13.0.0

2 files

12.2.1

2 files

12.2.0

2 files

12.1.0

2 files

12.0.0

2 files

11.2.1

2 files

11.1.3

2 files

11.1.2

2 files

11.1.1

2 files

11.1.0

2 files

11.0.0

2 files

10.4.0

2 files

10.3.0

2 files

10.2.0

2 files

10.1.0

2 files

10.0.0

2 files

9.0.0

2 files

8.4.1

2 files

8.4.0

2 files

8.3.1

2 files

8.3.0

2 files

8.2.5

2 files

8.2.4

2 files

8.2.3

2 files

8.2.2

2 files

8.2.1

2 files

8.2.0

2 files

8.1.0

2 files

8.0.0

2 files

7.4.0

2 files

7.3.0

2 files

7.2.0

2 files

7.1.0

2 files

7.0.1

2 files

6.2.0

2 files

6.1.1

2 files

6.1.0

2 files

6.0.0

2 files

5.13.0

2 files

5.12.0

2 files

5.11.0

2 files

5.10.0

2 files

5.9.0

2 files

5.8.0

2 files

5.7.2

2 files

5.3.3

2 files

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page