OCaml OUnit括号示例:设置和拆除

Oll*_*edt 2 ocaml unit-testing

我真的不知道如何使用OUnit(版本2)使用支架设置和拆卸.有人想提供一个完整的例子吗?

这是OUnit2.bracket功能文档:

val bracket : (test_ctxt -> 'a) ->
       ('a -> test_ctxt -> unit) -> test_ctxt -> 'a

bracket set_up tear_down test_ctxt set up an object 
and register it to be tore down in test_ctxt.
Run Code Online (Sandbox Code Playgroud)

你设置了这样一个测试套件:

let test_suite =
  "suite" >::: [
    "test1" >:: test1_fun
  ]
Run Code Online (Sandbox Code Playgroud)

并运行它:

let _ =
  run_test_tt_main test_suite
Run Code Online (Sandbox Code Playgroud)

我在哪里将括号放在此工作流程中?

链接到OUnit文档.

该文件test_stack.mlounit-2.0.0/examples测试支架OUnit第1版,所以这是没有用的.

Oll*_*edt 5

好的,看了看这个文件后得到了它: TestLog.ml

此示例将在每次测试后系统地销毁哈希表作为拆卸功能.

open ListLabels (* map with label *)

let test_sentence test_ctxt =
  assert_equal "Foo" "Foo"

(** In my case, clear a hashtable after each test *)
let tear_down () test_ctxt =
  Hashtbl.clear Globals.flags_tbl

(** List of test cases as (name, function) tuples *)
let test_list = [
  "sentence", test_sentence;
]

(** Test suite for all tags *)
let tag_test_suite =
  "tags" >::: (map test_list ~f:(fun (name, test_fn) ->
    name >:: (fun test_ctxt ->
      bracket ignore tear_down test_ctxt;
      test_fn test_ctxt
    )
  ))
Run Code Online (Sandbox Code Playgroud)