Code:
TargetFrame:SetScript("OnLoad", test)
Given this is a Blizzard frame and non "load-on-demand" Blizzard code loads before 3rd party addons, by the time your code tries to set the TargetFrame OnLoad script, it is well past having done its thing.
Code:
TargetFrame:SetScript("WHATEVER", test)
This should give an error saying the frame has no WHATEVER script (assuming that's actual code) so the following line (test(true)) won't run because the error would have stopped execution.
Using a SetScript on someone else's frame is not a good idea as it overrides the original.
Code:
TargetFrame:HookScript("OnShow", function(self) "do whatever" end)
Runs your code after the original script has run.