-
Notifications
You must be signed in to change notification settings - Fork 137
Description
Versions
Godot 4+
The Feature
GUT offers the possibility for each test class that inherits GutTest to redefine before_each, before_all, after_each and after_all hooks to execute certain code before or after each event. However, this is limited to each test suite and does not allow to register common behaviour for each event and thus is necessary for state to reset to duplicate code all throughout.
One simple solution to this problem exists, which is editing the GutTest class (or extending it with a custom script) to redefine the hook methods, but this also requires the explicit call of super in every other test, which may be error prone.
I suggest for GUT to have a mechanism to register global hooks in any point and for them to affect all tests, similar to Kotest's extensions system.
The following is an example of what the extension could be like:
# defines methods for the hooks with default, null behaviour for every one of them, user is expected to override whichever method/s they need
class GutExtension
extends RefCounted
# called before every test in every suite
func before_each():
pass
# called once for every suite, before every suite
func before_suite():
pass
# called only once before any test is run
func before_any():
pass
# called after every test in every suite
func after_each():
pass
# called once for every suite, after every suite
func after_suite():
pass
# called only once after every test is run
func after_every(): # bad name, expressive but doesn't match with before_any
passAs for using it, the following is an example use from a pre-run hook script.
extend GutHookScript
class CustomHook extends GutHook:
func before_each():
reset_my_state()
func after_all():
tear_down()
func run():
GutMain.register(CustomHook.new())I'm working on a little PoC on the project I'm working right now, as I commonly need to reset state to defaults, and I end up having a lot of before and afters with the same code. Would it be fine if I extract it into a fork to show the way it could be used?