我正在尝试将我的 SWI Prolog 应用程序迁移到 GNU Prolog。不幸的是,我对单元测试有疑问。在 SWIPL 中,我们可以简单地使用 plunit模块并编写如下测试用例:
:- begin_tests(my_tests).
test(my_predicate_test) :- my_predicate(Result), assertion(Result == [foo, bar]).
test(second_test) :- foo(10, X), assertion(X == "hello world").
:- end_tests(my_tests).
但是如何在 GNU Prolog 中实现单元测试呢?甚至一些额外的库,如 crisp不适用于 gprolog。
最佳答案
您可以使用 lgtunit
.其主要特点总结here .您的大多数测试都可以按原样运行或轻松转换。使用您的示例:
:- object(tests, extends(lgtunit)).
:- uses(lgtunit, [assertion/1]).
:- uses(user, [my_predicate/1, foo/2]).
test(my_predicate_test) :-
my_predicate(Result),
assertion(Result == [foo, bar]).
test(second_test) :-
foo(10, X),
assertion(X == "hello world").
:- end_object.
该工具支持多个test dialects ,其中一些为 plunit
所共有.例如。:- object(tests, extends(lgtunit)).
:- uses(user, [my_predicate/1, foo/2]).
test(my_predicate_test, true(Result == [foo, bar]) :-
my_predicate(Result).
test(second_test, true(X == "hello world")) :-
foo(10, X).
:- end_object.
假设您正在测试纯 Prolog 代码(假设您使用的是 GNU Prolog),Logtalk 的 Prolog 标准 compliance suite提供了大量的例子。您还可以以多种行业标准(例如 TAP 和 xUnit)导出测试结果,并生成报告以便于浏览(参见例如 https://infradig.github.io/trealla/)。
有关测试的更多建议,另请参阅 blog posts .
关于unit-testing - GNU Prolog 中的单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65844487/