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

**URL:** <https://swi-prolog.discourse.group/t/running-plunit-tests-from-the-command-line-who-else-has-a-script-for-that/3500>\
**Category:** Help!\
**Tags:** how-to\
**Created:** [January 17, 2021, 2:52pm UTC](https://swi-prolog.discourse.group/t/running-plunit-tests-from-the-command-line-who-else-has-a-script-for-that/3500 "2021-01-17T14:52:19Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sietse](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/sietse/32/3155_2.png) [@Sietse](https://swi-prolog.discourse.group/u/Sietse)\
**Post date:** [January 17, 2021, 2:52pm UTC](https://swi-prolog.discourse.group/t/running-plunit-tests-from-the-command-line-who-else-has-a-script-for-that/3500/1 "2021-01-17T14:52:19Z")

</div>

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](https://eu.swi-prolog.org/pldoc/man?section=pldoc-running) of the [`plunit` docs](https://eu.swi-prolog.org/pldoc/doc_for?object=section('packages/plunit.html')), and I think that my own script above is perhaps _too_ simple.

---

<div class="post-metadata">

**Author:** ![rla](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/rla/32/6863_2.png) [@rla](https://swi-prolog.discourse.group/u/rla)\
**Post date:** [January 18, 2021, 5:14pm UTC](https://swi-prolog.discourse.group/t/running-plunit-tests-from-the-command-line-who-else-has-a-script-for-that/3500/2 "2021-01-18T17:14:52Z")

</div>

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

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

```

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

---

<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:** [January 18, 2021, 5:42pm UTC](https://swi-prolog.discourse.group/t/running-plunit-tests-from-the-command-line-who-else-has-a-script-for-that/3500/3 "2021-01-18T17:42:28Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Sietse](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/sietse/32/3155_2.png) [@Sietse](https://swi-prolog.discourse.group/u/Sietse)\
**Post date:** [January 19, 2021, 10:38am UTC](https://swi-prolog.discourse.group/t/running-plunit-tests-from-the-command-line-who-else-has-a-script-for-that/3500/4 "2021-01-19T10:38:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Sietse](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/sietse/32/3155_2.png) [@Sietse](https://swi-prolog.discourse.group/u/Sietse)\
**Post date:** [February 21, 2021, 1:35pm UTC](https://swi-prolog.discourse.group/t/running-plunit-tests-from-the-command-line-who-else-has-a-script-for-that/3500/5 "2021-02-21T13:35:33Z")

</div>

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

> <https://github.com/SWI-Prolog/packages-plunit/pull/9/files>

---

<div class="post-metadata">

**Author:** ![nicos](https://avatars.discourse-cdn.com/v4/letter/n/7feea3/32.png) [@nicos](https://swi-prolog.discourse.group/u/nicos)\
**Post date:** [February 22, 2021, 12:09pm UTC](https://swi-prolog.discourse.group/t/running-plunit-tests-from-the-command-line-who-else-has-a-script-for-that/3500/6 "2021-02-22T12:09:37Z")

</div>

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](http://stoics.org.uk~nicos)

[1] ["upsh" pack for SWI-Prolog](http://eu.swi-prolog.org/pack/list?p=upsh)

---

<div class="post-metadata">

**Author:** ![AmbientTea](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/ambienttea/32/954_2.png) [@AmbientTea](https://swi-prolog.discourse.group/u/AmbientTea)\
**Post date:** [February 22, 2021, 7:34pm UTC](https://swi-prolog.discourse.group/t/running-plunit-tests-from-the-command-line-who-else-has-a-script-for-that/3500/7 "2021-02-22T19:34:08Z")

</div>

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

```prolog
#!/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.
