Writing tests for functions/components that use gui_hooks?

I’m developing a plugin and would like to add tests. My plugin is using gui_hooks like reviewer_did_show_question to get access to card fields, but writing tests for them seem to be facing complications by the fact that these hook into the anki code.

Does anyone have experience in mocking anki’s hooks or even running anki solely for running the test? How can we go about functions/components that use these components from Anki?

If you don’t need to directly test GUI code, the solution is to refactor your reviewer_did_show_question hook into smaller functions that can be tested individually and without relying on Anki running, mocking Anki’s objects such as Card if needed.

If you need to test the add-on in a real GUI environment though, @glutanimate’s pytest-anki is the popular choice. Some add-ons that use it include oakkitten/anki-wallpaper and the AnkiHub add-on (no public repo).

3 Likes