With SWI-Prolog unit testing each test has a few options.
The option foral(:Generator)
is useful for creating multiple test examples for a single test.
The options setup(:Goal)
and cleanup(:Goal)
are nice when working with test that use resources such as streams.
Since forall/1 will acquire the values of a test example item before the test calls setup/1, values needed for setup/1 can be placed in the test example item.
Example.
File: example_module.pl
:- module(example_module,[]).
:- load_test_files([]).
File: example_module.plt
:- begin_tests(example_tests).
goal_check :-
format('goal~n',[]).
setup_check(Values) :-
format('~nsetup - Values: ~w~n',[Values]).
cleanup_check(Values) :-
format('cleanup - Values: ~w~n',[Values]).
% The following line is the test example item as noted in the dialog.
setup_cleanup_with_options_case(1,success,[a,b,c]).
test(setup_cleanup_with_options,[forall(setup_cleanup_with_options_case(_,success,Values)),setup(setup_check(Values)),cleanup(cleanup_check(Values))]) :-
goal_check.
:- end_tests(example_tests).
Example run.
Welcome to SWI-Prolog (threaded, 64 bits, version 8.3.27-20-gd7b1d9a0b)
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software.
Please run ?- license. for legal details.
For online help and background, visit https://www.swi-prolog.org
For built-in help, use ?- help(Topic). or ?- apropos(Word).
?- working_directory(_,'C:/Users/Groot').
true.
?- [example_module].
true.
?- run_tests.
% PL-Unit: example_tests
setup - Values: [a,b,c]
goal
cleanup - Values: [a,b,c]
. done
% test passed
true.