Testing¶
Clausal Prolog has two kinds of tests: Python-level pytest tests in tests/, and inline test/1 clauses in source files: Clausal Prolog (.clausal), seam (.seam), and Prolog .pl files (through the Prolog importer). Examples on this page are Clausal Prolog.
Inline test/1 clauses¶
Any source file can include test clauses of the form:
In a Clausal Prolog (.clausal) or .pl file the name is a lowercase atom, test(fib_5) :- fib(5, 5)., which is the primary spelling. A string name, test("fib(5) = 5") :- ..., also loads and is accepted; the examples on this page use it for readable names. In a seam file the name is a string: test("fib(5) = 5") <- fib(5, 5).
A test passes if its body succeeds (produces at least one solution); a body that raises fails the test and reports the error. The head is test/1, lowercase like every predicate name: the old Test/1 spelling is now a load-time error (TitleCase names have no role in Clausal Prolog), and the runner names the rename. Tests live alongside the predicates they exercise:
:- use_module(library(clpz)).
fib(0, 0).
fib(1, 1).
fib(N, F) :-
N #> 1,
N1 #= N - 1,
N2 #= N - 2,
fib(N1, F1),
fib(N2, F2),
F #= F1 + F2.
test("fib(5) = 5") :-
fib(5, 5).
test("fib(7) = 13") :-
fib(7, F),
F == 13.
Negative tests: test(Name, fail)¶
A test that must find no solution is plunit's test/2 with the option fail. It passes iff its body has no solution; a body that succeeds fails the test (reported as test(..., fail) succeeded), and a body that raises is an error, as for test/1:
A .pl file writes it the same way, as plunit does, and the seam spelling is the same term: test("bob is not tom's parent", fail) <- parent("bob", "tom"). fail is the only option supported. Any other option -- true(Cond), throws(Error), nondet, blocked(Reason), setup(Goal) and the rest of plunit's list, or a list of options -- is a collection error naming the option: the file is one failing <collect> item (<collect> in the CLI report too), never a silently skipped test.
Running test/1 tests standalone¶
Use clausal.testing as a command-line tool:
# Run all .seam, .clausal and .pl files under a directory
python -m clausal.testing clausal/examples/
# Run a single file
python -m clausal.testing clausal/examples/fibonacci.seam
# Verbose output (shows individual PASS/FAIL and every skipped file)
python -m clausal.testing -v clausal/examples/
Exit codes:
| Code | Meaning |
|---|---|
| 0 | every collected test passed |
| 1 | a test failed, or a file failed to load (a .pl file the front end refuses included) |
| 2 | usage error: a path does not exist, or names a file that is not .seam, .clausal or .pl |
| 5 | no tests collected (the same number as pytest's "no tests collected") |
A file that fails to load is reported as one failing <load> item, with the load error and its source line.
An empty run fails. A directory with no test files, or whose files hold no test/1 clauses, exits 5, as does a single file with no test/1 clauses: a mistyped root or a renamed extension must not read as a green run. Pass --allow-empty when an empty directory is genuinely expected; the run then exits 0. --strict (alias --fail-on-empty) used to opt in to failing on an empty run; that is now the default, so the flag is accepted for compatibility and changes nothing (it cannot be combined with --allow-empty). An empty run used to exit 1 under --strict; it now exits 5 either way.
Skipped files are reported. A directory scan names the files it collected nothing from, with the reason: unsupported suffix (a README.md, a .py helper), no test/1 clauses (a module of helpers), no-collect marker (see below) or ignored by conftest.py. The last is a file a conftest.py at or below the scanned directory excludes through collect_ignore or collect_ignore_glob, so the CLI and the pytest plugin skip the same files. The CLI reads those two names only when they are assigned a literal list; it never runs the conftest. As in pytest, a file you name on the command line is run even if a conftest list covers it. Up to 5 are listed in full; a longer list is one count line, and -v lists every file. Files under hidden directories and __pycache__ are not reported.
1 file(s) skipped (unsupported suffix: 1):
docs/README.md (unsupported suffix)
no test files (.seam, .clausal or .pl) found
0 tests [NO TESTS] (exit 5; pass --allow-empty to accept an empty run)
Prolog .pl test files¶
A .pl file is a test file too. It is loaded through the same experimental Prolog importer a -import_from of a .pl file uses, and its test/1 clauses are the tests:
double(X, Y) :- Y is X * 2.
test('double of two is four') :- double(2, 4).
test('append works') :- append([1], [2], [1, 2]).
A .pl file the front end rejects (a syntax error, or a refused construct such as a cut) is a failing <load> item carrying the error -- never a skipped file. The native front end (the default) reads the .pl source directly, so a failure names its source line. Under CLAUSAL_PL_FRONTEND=translator the translator keeps no source map, so a failing .pl test is reported without a line number, and its goals are shown in their seam translation.
Running test/1 tests via pytest¶
The conftest.py at the project root registers a pytest plugin that automatically collects .seam files, .clausal files, Prolog .pl files (through the Prolog importer; a file that does not load is one failing <load> item), and also every ```clausal block (loaded as Clausal Prolog) and ```seam block (loaded as seam) in docs/*.md: each test/1 clause in a block is an item, and a block without one is checked to load. The import hook handles loading and compilation. Each test/1 clause appears as an individual pytest item:
Output looks like:
clausal/examples/fibonacci.seam::fib(0) = 0 PASSED
clausal/examples/fibonacci.seam::fib(5) = 5 PASSED
This means test/1 tests and Python tests can run together in one pytest invocation. A test-data file that is meant not to be collected (a fixture that fails to load on purpose, Prolog source that is data rather than tests) carries the line # clausal: no-collect within its first 30 lines -- % clausal: no-collect in a .pl or .clausal file. Both runners honour it: the plugin does not collect the file, and python -m clausal.testing reports it as skipped. Prolog .pl files with test/1 clauses are collected and run like .seam files -- the spelling is preserved across the translation (a Prolog test/1 clause stays test/1, as every predicate name does), so nothing about the imported file is deprecated. See Importing Prolog.
Running Python tests¶
Writing good test/1 clauses¶
- Each test should be a single rule with a descriptive name.
- Test bodies can use any predicates defined in the module, plus builtins like
append,in_,member, etc. - Use
==to check a computed result that is already bound, numeric or not:test(check) :- some_pred(X), X == 13. - Use
=to unify with an expected answer:test(colour) :- colour_of(apple, C), C = red. - Use CLP(ℤ) for arithmetic, with
:- use_module(library(clpz)).:test(arith) :- N #= 2 + 3, N #= 5.ISON is 2 + 3, N =:= 5also works when the right-hand side is ground (see Arithmetic). - In the seam, bare
==is the CLP(ℤ) constraint,X is Tunifies, andstructural_eq(X, Y)is ISO==.
See also: Examples — for real test predicates used in example programs.