1. Brandon Mitchell
  2. ocaml-core


ocaml-core / base / pa_ounit /

The branch 'bash' does not exist.
Filename Size Date modified Message
1.1 KB
366 B
2.0 KB

% Pa_ounit

Pa_ounit is a syntax extension that helps writing in-line test in ocaml code. It allows user to register tests with a new TEST top-level expressions and automatically collects all the tests in a module (in a function ounit_tests of type unit -> OUnit.test).

Basic case


let is_prime = <magic>

TEST = is_prime 5
TEST = is_prime 7
TEST = not (is_prime 1)
TEST = not (is_prime 8)



This creates a small program extracted_test_runner.exe in the same directory as prime.ml.

Using modules and functors

Any test that you add in a submodule declared directly (i.e. using module .. = struct .. end) will be automatically registered in the parent module. Tests in other modules will need to be manually registered using TEST_MODULE.

tests in a functor.

module Make(C:S) = struct
     TEST = <some expression>


grouping test and side-effecting initialisation.

Since the module passed as an argument to TEST_MODULE is only initialised when we run the tests. It is therefore ok to perform side-effects in a TEST_MODULE

    module UID = Uniq_id.Int(struct end)

    TEST = UID.create() <> UID.create()

"piercing" through the signature

When a module contains TEST or TEST_MODULE the function ounit_test is exported even if the module has a signature that would hide it. Note that this not apply to modules that do not directly contain a TEST or TEST_MODULE statement or to modules that are packed as first class values (e.g. a module including a module that containing a TEST statement).


The TEST and TEST_MODULE directive can optionally take a name otherwise the name of the test is inferred based on the file name and the position in the file.

| TEST <quoted string>? = <boolean expr>
| TEST_MODULE <quoted string>? = <module with ounit_tests>