| OUnit User's Guide | ||
|---|---|---|
| Prev | ||
In the Ocaml module 'tests.ml' where your tests will reside, import module OUnit
open OUnitDefine test cases as appropriate
let test1 =
TestCase (fun _ ->
assert_equal "x" (unity "x"));;
let test2 =
TestCase (fun _ ->
assert_equal 100 (unity 100));;
Name the test cases and group them together::
let suite = TestLabel ("suite",
TestList [ TestLabel ("test1", test1);
TestLabel ("test2", test2)]);;
And compile the module
$ocamlfind ocamlc -c tests.ml
File "tests.ml", line 3, characters 48-51: Unbound value foo
Now it is time to program 'foo', create a new module named 'foo.ml',
and type
let unity a = aAnd compile
$ocamlc -c foo.mlThe 'Foo' module must now also be opened in the 'Tests' module, so we need to add the following line.::
open FooNow it is possible to compile test.ml
$ocamlc -c foo.cmo tests.mlWith the ocamlunit toplevel it is possible to run the tests interactively
$ ./ocamlunit
# Objective Caml version 3.04
# load "tests.cmo";;
# OUnit.run_test_tt Tests.suite;;
..
Ran: 2 tests in: 0.00 Seconds
OK
- : OUnit.counts =
{OUnit.cases = 2; OUnit.tried = 2; OUnit.errors = 0; OUnit.failures = 0}
#
You can specify the tests even more succinctly using special
operators.
let suite =
"suite" >::: ["test1" >:: test1; "test2" >:: test2;];;
It is also possible to add the following line to the 'Tests'
module.
let _ =
run_test_tt suite
When compiled as follows:
$ocamlfind ocamlc -o tests -package oUnit -linkpkg foo.cmo tests.mlA executable named 'tests' will be created. When run it produces the following output.
$ ./tests
..
Ran: 2 tests in: 0.00 Seconds
OK
Test Case
A test case is the unit of test execution. That is distinct test
cases are executed independently. The failure of one is independant
of the failure of any other, provided the test case does not rely on
side-effects.