ApprovalTests.Python Options Diagram

Every way to configure a verify() call, shown as one chained Options() example.

Hover over any colored part of the code to see what that option does.

This example is a real, runnable test: test_demonstrate_available_approvaltests_options in tests/test_options.py

change the extension of your file name
update text in your test output
a second pass on top of that
change the name of your test output file
decide what counts as a match
choose how a failure is shown to you
172def test_demonstrate_available_approvaltests_options() -> None:
173    # begin-snippet: options_with_all_options
174    options = (
175        Options()
176
177        # write the .approved/.received files as Markdown instead of .txt
178        .for_file.with_extension(".md")
179
180        # uppercase the text before it is written/compared, so the effect
181        # is visible in the received/approved files themselves
182        .with_scrubber(lambda text: text.upper())
183
184        # a second scrubber runs after the first; this one turns "!" into "!!!"
185        .with_scrubber(lambda text: text.replace("!", "!!!"))
186
187        # add a scenario name to the file name, so one test can keep a separate
188        # approved file per case (e.g. one per language)
189        .for_file.add_additional_information("english")
190
191        # use a custom comparator that ignores trailing "!"s, so the mismatch
192        # created by the second scrubber above still counts as approved
193        .with_comparator(IgnoreTrailingExclamationsComparator())
194
195        # print a diff and the approve command to the test output on any mismatch
196        .with_reporter(PythonNativeReporter())
197    )
198
199    verify("Hello, Approvals!", options=options)
200    # end-snippet
for_file.with_extension changes the file type at the end of the name
test_options.test_demonstrate_available_approvaltests_options
.approved.md  (instead of .txt)
with_namer decides the file name
before: …_options.approved.md
after:  …_options.english.approved.md
More about namers: multiple approvals per test
with_scrubber changes your text before it is saved and compared
in  → Hello, Approvals!
out → HELLO, APPROVALS!
a 2nd with_scrubber runs on the output of the 1st
in  → HELLO, APPROVALS!
out → HELLO, APPROVALS!!!
with_comparator decides what counts as a match
class IgnoreTrailingExclamationsComparator(Comparator):
    @override
    def compare(self, received_path, approved_path) -> bool:
        received = ….rstrip("!\n")
        approved = ….rstrip("!\n")
        return received == approved
with_reporter decides how a failing test shows you the difference
PythonNativeReporter prints the diff in your test output (instead of opening a diff tool):
--- demo.approved.md
+++ demo.received.md
@@ -1 +1 @@
-HELLO, APPROVALS!
+HELLO, APPROVALS!!!

to approve this result: (a copy command)
received  HELLO, APPROVALS!!!
approved  HELLO, APPROVALS!
IgnoreTrailingExclamationsComparator strips trailing "!"s from both before comparing — so despite 2nd with_scrubber's extra "!!!", the test PASSES