# Shared variables in unit test cleanup

**URL:** <https://swi-prolog.discourse.group/t/shared-variables-in-unit-test-cleanup/7250>\
**Category:** General\
**Created:** [February 18, 2024, 6:36pm UTC](https://swi-prolog.discourse.group/t/shared-variables-in-unit-test-cleanup/7250 "2024-02-18T18:36:53Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![turibe](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/turibe/32/6097_2.png) [@turibe](https://swi-prolog.discourse.group/u/turibe)\
**Post date:** [February 18, 2024, 6:36pm UTC](https://swi-prolog.discourse.group/t/shared-variables-in-unit-test-cleanup/7250/1 "2024-02-18T18:36:53Z")

</div>

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

```prolog
:- 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.

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [February 19, 2024, 12:06pm UTC](https://swi-prolog.discourse.group/t/shared-variables-in-unit-test-cleanup/7250/2 "2024-02-19T12:06:41Z")

</div>

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.
