【问题标题】:Unit testing in GNU PrologGNU Prolog 中的单元测试
【发布时间】:2021-01-22 11:41:12
【问题描述】:

我正在尝试将我的 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。

【问题讨论】:

    标签: unit-testing prolog gnu-prolog


    【解决方案1】:

    您可以使用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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-13
      相关资源
      最近更新 更多