- What can I use ApprovalTests for?
- Getting Started
- Overview
- Reporters
- Support and Documentation
- For developers
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.
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.
If you need to gain a better understanding or are new to this concept, start here.
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.
If this is first time approvaltesting in python, consider starting here: Minimal Example Tutorial
From pypi:
pip install approvaltests
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.
from approvaltests.approvals import verify
def test_simple() -> None:
result = "Hello ApprovalTests"
verify(result)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
import unittest
from approvaltests.approvals import verify
class GettingStartedTest(unittest.TestCase):
def test_simple(self) -> None:
verify("Hello ApprovalTests")
if __name__ == "__main__":
unittest.main()This example has the same behaviour as the pytest version, but uses the built-in test framework unittest instead.
You can invoke a verify() call from the command line. This allows invoking python approvals from any other stack via subprocesses.
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
-
--test-idor-t: Test identifier used to name theapproved.txtandreceived.txtfiles for the test. -
--receivedor-r: The output of the program under test (a string) that is passed to the verify method.stdin: Instead of providing areceivedargument, you may usestdin.
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.
-
ApprovalTests Homepage: http://www.approvaltests.com
If there is documentation you wish existed, please add a page request to this issue.
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
These dependencies are always required for approvaltests
pytest>=9.0.3
empty-files>=0.0.3
typing_extensions>=4.12.0These 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.0The best way to contribute is to join our weekly mob/ensemble.
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.