Suggestion for better feedback from failing tests in Prolog

I have a suggestion for showing more informative feedback from the test runner in Prolog. Here’s an example failing test case to demonstrate.

f(x).

:- begin_tests(f).

test(f) :-
    f(Y),
    Y == y.

:- end_tests(f).

?- run_tests(f).
[1/1] f:f ........................ **FAILED (0.000 sec)
ERROR: /home/tmp.pl:6:
ERROR:     test f:f: failed
ERROR: 1 test failed

If the property being tested is wrapped in assertion/1, you get more informative output from the test runner.

test(f) :-
    f(Y),
    assertion(Y == y).

?- run_tests(f).
[1/1] f:f ........................ **FAILED (0.033 sec)
ERROR: /home/tmp.pl:6:
ERROR:     test f:f: assertion
ERROR: /home/tmp.pl:6:
ERROR:     test f:f: assertion at line 8 failed
  Assertion: x==y

ERROR: 1 test failed

Notice that with the test runner shows the incorrect returned value and expected value, instead of just stating that the test failed. This also helps distinguish between a predicate which failed unexpectedly and a predicate which returned an incorrect value, as the unexpected failure happens before the assertion call.

I do these exercises on my local machine, so I do this anyway, but I thought I’d share this for anyone else who doesn’t know about this feature. If this was added to the test cases, it’d be especially helpful for people trying to solve these exercises on the web site.

3 Likes