Suppose I have a hello_name.pl:
greeting (Name): -
write ('hello'),
write (Name),
writeln ('!').
And I want to put in my plunit something like
catch_output (greeting ('Moncho'), ConsoleOutput),
assertion ('hello Moncho!' =:= ConsoleOutput).
If you are using swi-prolog
See: with_output_to/2
Note: with_output_to/2 is implemented using C in SWI-Prolog so is not portable as Prolog code.
With corrections to your code and using SWI-Prolog unit tests