如何在prolog

时间:2015-11-22 08:01:13

标签: prolog plunit

我很难让plunit执行测试,看似最简单的案例。这是我的设置:

foo.pl

x(5) :- !.
not(x(6)).

foo.plt

:- begin_tests(foo).
test_something(x) :- not(x(5)).

test_seomthing_else(x) :- x(6).
:- end_tests(foo).

这在swipl中按预期工作:

?- [foo].
% foo compiled 0.00 sec, 3 clauses
true.

?- x(5).
true.

?- x(6).
false.

但我似乎无法让foo.plt文件失败

?- load_test_files(foo).
% /tmp/example/foo.plt compiled into plunit 0.00 sec, 4 clauses
true.

?- run_tests.
% PL-Unit: foo  done
% No tests to run
true.

test_something_else测试用例显然应该失败,但是plunit的run_tests/0似乎没有意识到有任何测试需要运行。

1 个答案:

答案 0 :(得分:3)

来自plunit manual

  

入口点由规则使用头部测试(名称)或测试(名称,选项)[...]

定义

因此,您需要使用test_something(x)而不是test_something_else(x)test(x)

:- begin_tests(foo).
test(x) :- not(x(5)).

test(x) :- x(6).
:- end_tests(foo).

运行它将得到预期的输出:

?- run_tests.
% PL-Unit: foo 
ERROR: [...]
    test x: failed

ERROR: [...]
    test x: failed

 done
% 2 tests failed
% 0 tests passed
false.