Shared variables in unit test cleanup

The docs say that a unit test cleanup goal can share variables with the test body, but this doesn’t quite work:

:- set_prolog_flag(plunit_output, always).

test(fail_test, [ cleanup(writeln(user_output,Message)), fail ]) :-
    Message = "cleanup message",
    writeln(user_output, "testing"),
    fail.

Looks like Message must be introduced in a setup goal instead — something to clarify in the docs.

The variables are shared fine, but as the test body fails, no binding is left. But yes. if you create some resource to be used in the test you should use the setup handler for it, so the cleanup is called correctly in case the body fails or raises an error.