Module OUnit


module OUnit: sig .. end
The OUnit library can be used to implement unittests

To uses this library link with ocamlc oUnit.cmo or ocamlopt oUnit.cmx
Author(s): Maas-Maarten Zeeman



Assertions

Assertions are the basic building blocks of unittests.

val assert_failure : string -> 'a
Signals a failure. This will raise an exception with the specified string.
Raises Failure to signal a failure
val assert_bool : string -> bool -> unit
Signals a failure when bool is false. The string identifies the failure.
Raises Failure to signal a failure
val (@?) : string -> bool -> unit
Shorthand for assert_bool
Raises Failure to signal a failure
val assert_string : string -> unit
Signals a failure when the string is empty. The string identifies the failure
Raises Failure to signal a failure
val assert_equal : ?printer:('a -> string) -> ?msg:string -> 'a -> 'a -> unit
Compares two values, when they are not equal a failure is signaled. The optional printer can be used to convert the value to string, so a nice error message can be formatted. When msg is also set it can be used to identify the failure
Raises Failure description
val assert_raises : ?msg:string -> exn -> (unit -> 'a) -> unit
Asserts if the expected exception was raised. When msg is set it can be used to identify the failure
Raises Failure description

Bracket

A bracket is a functional implementation of the commonly used setUp and tearDown feature in unittests. It can be used like this:

"MyTestCase" >:: (bracket test_set_up test_fun test_tear_down)

val bracket : (unit -> 'a) -> ('a -> 'b) -> ('a -> 'c) -> unit -> 'c

Constructing Tests


type test =
| TestCase of (unit -> unit)
| TestList of test list
| TestLabel of string * test
The type of tests
val (>:) : string -> test -> test
Create a TestLabel for a test
val (>::) : string -> (unit -> unit) -> test
Create a TestLabel for a TestCase
val (>:::) : string -> test list -> test
Create a TestLabel for a TestList

Some shorthands which allows easy test construction.

Examples:



Retrieve Information from Tests

val test_case_count : test -> int
Returns the number of available test cases

Types needed to represent the path of a test

type node =
| ListItem of int
| Label of string
type path = node list 
The path to the test (in reverse order).
val string_of_node : node -> string
Make a string form a node
val string_of_path : path -> string
Make a string from a path. The path will be reversed before it is tranlated into a string
val test_case_paths : test -> path list
Returns a list with paths of the test

type counts = {
   cases : int;
   tried : int;
   errors : int;
   failures : int;
}
Counts
val was_successful : counts -> bool
Returns true if the counts indicate that the test run was successful. This means that the errors and failures must be 0

Performing Tests


type test_event =
| EStart of path * counts (*Indicates the start of a test-case*)
| EEnd of path * counts (*Indicates the end of a test-case*)
| ESuccess of path * counts (*Indicates success of the test-case*)
| EFailure of path * string * counts (*Indicates an failure has occurred*)
| EError of path * string * counts (*Indicates that an error has occurred*)
Events which can occur during a test
val perform_test : (test_event -> 'a) -> test -> counts
Perform the test, allows you to build your own test runner
val run_test_tt : ?verbose:bool -> test -> counts
A simple text based test runner. It prints out information during the test.
val run_test_tt_main : test -> counts
Main version of the text based test runner. It reads the supplied command line arguments to set the verbose level