Running plunit tests from the command line -- who else has a script for that?

So, I’ve got a little CLI (shell) wrapper that lets me invoke plunit to run the unit tests in a Prolog file like so (pltest is the name of the script):


The script looks like this:


# First argument is the Prolog file we want to test.

# - Load the prolog file as a script
# - Run `run_tests` as the main goal.
swipl -s "$prolog_file" -g 'initialization(run_tests, main).'

As you can tell, this is a very limited script, especially considering what run_tests can do when invoked from the swipl REPL. I hope it can be useful as a conversation starter. Does anybody else have their own personal script to run plunit from the command line, perhaps with more features than this one? Or perhaps one written in Prolog, so it doesn’t depend on a Unix shell? Who wants to share?

The reason for this question: I’d like to contribute a small ‘Running the test suite from the command line’ section to section 4 of the plunit docs, and I think that my own script above is perhaps too simple.

1 Like

This is what I have been using in Makefile of packs:

	swipl -s tests/ -g run_tests,halt -t 'halt(1)'

1 Like

In the current version (also stable) you can use

swipl -g goal1 -g goal2 ... -t halt ...

That will load all Prolog files, run all -g goals and if any fails exit with status 1 and if non fails hit the -t halt causing it to hold with status 0 instead of becoming interactive.

Alternatively, use this in the load file:

:- initialization(mytests, main).

Now just swipl will run mytest/0 and exit 1 on failure and 0 on success. If you want to debug use

swipl -l

That will load the code but not run it.

Thank you both, I’ll start drafting a docs contribution.

I’ve drafted a patch to the docs, and made a pull request. If anybody is curious, you can read the patch here:

1 Like

Dear Sietse,

You might find pack(upsh) [1] of some help.

I would create a file in ~/bin/cline_upsh/
and you can call this from anywhere with
% upsh pltest myfile

I would make’s main predicate have a number
of options with defaults which you can change at runtime.

In the (untested) script below pack(options) is used, although it is easy to implement
your own or use library(option).



:- use_module(library(options)).
:- use_module(library(debug)).
:- use_module(library(apply)).

pltest( Args ) :-
options_append( pltest, Args, Opts, atoms(PlFiles) ),
options( debug(Dbg), Opts ),
maplist( pltest_file(Dbg,Opts), PlFiles )
debug( Dbg, ‘Finished pltest.’, [] ).

pltest_file( Dbg, Opts, PlFile ) :-
debug( Dbg, ‘Doing: ~p’, [PlFile] ),
locate_pl_file_possibly_by_looking_in_all_installed_packs_and_src_locations( PlFile, AbsFile ),
call_swi_on_file_with_opts( AbsFile, Dbg, Opts ).

% upsh pltest debug=true myfile


Nicos Angelopoulos

[1] "upsh" pack for SWI-Prolog

i have my own file written directly in prolog to run all tests like this:

#!/usr/bin/env prolog
:- use_module(<modules>).

:- consult(<tests>).

	halt ; halt(1)

But I like the idea of using make.