Approval Tests
Approval tests capture the output (snapshot) of a piece of code and compare it with a previously approved version of the output.
It's most useful in environments where frequent changes are expected or where the output is of complex nature but can be easily verified by humans aided for example by a diff-tool or a visual representation of the output.
A picture’s worth a 1000 tests.
Once the output has been approved then as long as the output stays the same the test will pass. A test fails if the received output is not identical to the approved version. In that case, the difference of the received and the approved output is reported to the tester.
Reporters and Approvers
For outputs which can be represented by text a report can be as simple as printing the difference to the terminal. Using diff programs as reporter not only help visualizing the difference between received and approved but can also be used as approver by applying the changes to the approved file.
Most modern diff programs can also handle images files. Which broadening the reporting capabilities.
Test pattern:
Arrange, Act, Print, Verify
.
Be aware of the Guru Checks Output antipattern.
A good introduction into approval tests gives this video.
General Resources
- Official Website
- Blog posts:
- https://ccd-akademie.de/en/legacy-code-testen-mit-approval-tests/
- https://www.codium.ai/blog/automate-approval-testing/
- https://yelenagou.github.io/ApprovalTesting/
- https://medium.com/97-things/approval-testing-33946cde4aa8
- https://dev.to/sergiomarcial/unlocking-the-power-of-approval-testing-a-comprehensive-guide-for-software-engineers-3o6f
Python Related Resources
pytest \
--approvaltests-add-reporter="pycharm-community" \
--approvaltests-add-reporter-args="diff"
# -s: disable all capturing
pytest \
-s \
--approvaltests-add-reporter="nvim" \
--approvaltests-add-reporter-args="-d"
Open Questions
- How to set default Namer class
- How to set default Reporter with a config file which is in .gitignore?