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):

pltest myfile.pl

The script looks like this:

#!/bin/sh

# First argument is the Prolog file we want to test.
prolog_file="$1"

# - 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:

test:
	swipl -s tests/tests.pl -g run_tests,halt -t 'halt(1)'

https://github.com/rla/prolog-markdown/blob/2a905a91ea7987e3b26cfd86abd022110021f0d8/Makefile#L5

1 Like

In the current version (also stable) you can use

swipl -g goal1 -g goal2 ... -t halt file1.pl file2.pl ...

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 file.pl will run mytest/0 and exit 1 on failure and 0 on success. If you want to debug use

swipl -l file.pl

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 pltest.pl file in ~/bin/cline_upsh/
and you can call this from anywhere with
% upsh pltest myfile

I would make pltest.pl’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).

eg

pltest_defaults([debug(false)]).

:- 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

Regards,

Nicos Angelopoulos

http://stoics.org.uk~nicos

[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>).

:-
    portray_text(true),
	load_test_files([]),
	test_report(fixme),
	run_tests,
	halt ; halt(1)
.

But I like the idea of using make.