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
172 def 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
test_options.test_demonstrate_available_approvaltests_options
.approved.md (instead of .txt)
before: …_options.approved.md
after: …_options.english .approved.md
in → Hello, Approvals!
out → HELLO, APPROVALS!
in → HELLO, APPROVALS!
out → HELLO, APPROVALS!!!
class IgnoreTrailingExclamationsComparator (Comparator):
@override
def compare (self, received_path, approved_path) -> bool:
received = ….rstrip("!\n" )
approved = ….rstrip("!\n" )
return received == approved
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