我对无限延迟结构进行了一些测试,如果测试函数没有正确实现,这些测试可能会无限期地运行,但是我无法在OUnit文档中找到如何在测试中设置超时。
发布于 2014-01-04 23:36:38
我不认为oUnit提供这个功能。我记得有一段时间我不得不这么做,这是我想出的快速黑客:
let race seconds ~f =
let ch = Event.new_channel () in
let timeout = Thread.create (fun () ->
Thread.delay seconds;
`Time_out |> Event.send ch |> Event.sync
) () in
let tf = Thread.create (fun () ->
`Result (f ()) |> Event.send ch |> Event.sync) () in
let res = ch |> Event.receive |> Event.sync in
try
Thread.kill timeout;
Thread.kill tf;
res
with _ -> res
let () =
let big_sum () =
let arr = Array.init 1_000_000 (fun x -> x) in
Array.fold_left (+) 0 arr in
match race 0.0001 ~f:big_sum with
| `Time_out -> print_endline "time to upgrade";
| `Result x -> Printf.printf "sum is: %d\n" x这对于我的用例来说已经足够好了,但是我肯定不会推荐使用它,因为如果race不像您所期望的那样工作,如果~f不进行任何分配或者手动调用Thread.yield的话。
发布于 2014-02-23 09:15:14
如果您使用的是OUnit2,那么下面的操作应该是有效的:
let tests =
"suite" >::: [OUnitTest.TestCase (
OUnitTest.Short,
(fun _ -> assert_equal 2 (1+1))
);
OUnitTest.TestCase (
OUnitTest.Long,
(fun _ -> assert_equal 4 (2+2))
)]test_length类型被定义为:
type test_length =
| Immediate
| Short
| Long
| Huge
| Custom_length of floathttps://stackoverflow.com/questions/20921661
复制相似问题