【问题标题】:The correct way to write unit tests for a module in OCaml在 OCaml 中为模块编写单元测试的正确方法
【发布时间】:2015-11-04 14:22:14
【问题描述】:

我在module.mli 文件中有一个给定的接口规范。我必须在module.ml 文件中编写它的实现。

module.mli 提供抽象类型

type abstract_type

我正在使用 OUnit 创建测试。我需要在其中使用类型的实现。 (例如比较值)一种解决方案是扩展接口以包含测试中使用的附加功能。

但是不修改界面就可以做这样的事情吗?

【问题讨论】:

    标签: unit-testing ocaml ounit


    【解决方案1】:

    在不接触模块接口的情况下公开测试的唯一方法是将测试注册到某个全局容器中。如果您有一个名为 Tests 的模块提供了一个函数 register,那么您的 module.ml 将包含如下内容:

    让 some_test = ... let () = Tests.register some_test

    我不推荐这种方法,因为Tests 模块无法控制它要运行的测试。

    我建议导出测试,即将它们添加到module.mli

    请注意,不依赖于 OUnit,您可以导出以下类型的任何人都可以运行的测试。我们的测试如下所示:

    让 test_cool_feature () = ... 断言……; ... 断言……; 真的 让 test_super_feature () = ... a = b 让测试= [ “酷功能”,test_cool_feature; “超级特征”,test_super_feature; ]

    界面是:

    ... (**/**) (* ocamldoc 忽略的开始部分 *) val test_cool_feature : 单位 -> 布尔 val test_super_feature : 单位 -> bool val 测试 : (string * (unit -> bool)) 列表

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-25
      • 2020-12-03
      • 1970-01-01
      • 2019-12-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多