OCaml OUnit支架示例:设置和拆卸 [英] OCaml OUnit bracket example: setup and tear-down

查看:87
本文介绍了OCaml OUnit支架示例:设置和拆卸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I do not really get how to use bracket setup and tear-down with OUnit (version 2). Anyone feel like supplying a full example ?

这是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.

您可以这样设置测试套件:

You setup a test suite like this:

let test_suite =
  "suite" >::: [
    "test1" >:: test1_fun
  ]

并像这样运行它:

let _ =
  run_test_tt_main test_suite

在此工作流程中,该放在哪里?

Where do I put the bracket in this workflow ?

链接到 OUnit文档.

用于OUnit版本1的ounit-2.0.0/examples测试支架中的文件test_stack.ml,因此没有用.

The file test_stack.ml in ounit-2.0.0/examples test bracket for OUnit version 1, so that's not useful.

推荐答案

好的,在查看此文件后得到了它:

OK, got it after having a look at this file: TestLog.ml

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

This example will systematically destroy the hashtables after each test as a teardown function.

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
    )
  ))

这篇关于OCaml OUnit支架示例:设置和拆卸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆