# OUnit User's Guide

Maas-Maarten
Zeeman

                mmzeeman@xs4all.nl

2003
2004
2005
Maas-Maarten Zeeman

Ounit is a unit testing framework for Ocaml, inspired by HUnit for Haskell, and JUnit for Java. This guide describes how to use OUnit, assuming you are familiar with Ocaml, though not necessarily with JUnit.

This manual describes how to write unit-tests for OCaml programs.

## License

1. The package OUnit is Copyright © 2002, 2003, 2004, 2005 by Maas-Maarten Zeeman.

2. Permission is hereby granted, free of charge, to any person obtaining a copy of this document and the OUnit software ("the Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

3. The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

4. The Software is provided ``as is'', without warranty of any kind, express or implied, including but not limited to the warranties of merchantability, fitness for a particular purpose and noninfringement. In no event shall Maas-Maarten Zeeman be liable for any claim, damages or other liability, whether in an action of contract, tort or otherwise, arising from, out of or in connection with the Software or the use or other dealings in the software.

<chapter>

# What is Unit Testing?

A test-oriented methodology for software development is most effective whent tests are easy to create, change, and execute. The JUnit tool pioneerded for test-first development in Java. OUnit is an adaptation of JUnit to Ocaml, a general purpose functional programming language.

With OUnit, as with JUnit, you can easily create tests, name them, group them into suites, and execute them, with the framework checking the results automatically. Test specification in OUnit is even more concise and flexible than in JUnit, thanks to the nature of the Ocaml language, or the functional programming paradigm in general.

The next section helps you to get started using OUnit in simple ways.

<chapter>

# Getting Started

In the Ocaml module 'tests.ml' where your tests will reside, import module OUnit

        open OUnit

Define 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 = a

And compile

        $ocamlc -c foo.ml

The 'Foo' module must now also be opened in the 'Tests' module, so we need to add the following line.::

        open Foo

Now it is possible to compile `test.ml`

        $ocamlc -c foo.cmo tests.ml

With 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.ml

A 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.