package ounit2

  1. Overview
  2. Docs
OUnit testing framework

Install

Dune Dependency

Authors

Maintainers

Sources

ounit-2.2.7.tbz
sha256=90f6e63bd1240a51d8b9b2f722059bd79ce00b5276bdd6238b8f5c613c0e7388
sha512=53463e5b1b5a40f424e19f5f6a86338a544079600d1fd121ffc1a6fcaa239630194018faf91ccf360ba40b1b2a8b01cf491935e014c68d2947f6e027a2f0a0f9

doc/ounit2/OUnit/index.html

Module OUnitSource

Unit test building blocks

  • author Maas-Maarten Zeeman
  • author Sylvain Le Gall

Assertions

Assertions are the basic building blocks of unittests.

Sourceval assert_failure : string -> 'a

Signals a failure. This will raise an exception with the specified string.

Sourceval assert_bool : string -> bool -> unit

Signals a failure when bool is false. The string identifies the failure.

Sourceval (@?) : string -> bool -> unit

Shorthand for assert_bool

Sourceval assert_string : string -> unit

Signals a failure when the string is non-empty. The string identifies the failure.

Sourceval assert_command : ?exit_code:Unix.process_status -> ?sinput:char Seq.t -> ?foutput:(char Seq.t -> unit) -> ?use_stderr:bool -> ?env:string array -> ?verbose:bool -> string -> string list -> unit

assert_command prg args Run the command provided.

  • parameter exit_code

    expected exit code

  • parameter sinput

    provide this char Seq.t as input of the process

  • parameter foutput

    run this function on output, it can contains an assert_equal to check it

  • parameter use_stderr

    redirect stderr to stdout

  • parameter env

    Unix environment

  • parameter verbose

    if failed, dump stdout/stderr of the process to stderr

  • since 1.1.0
Sourceval assert_equal : ?cmp:('a -> 'a -> bool) -> ?printer:('a -> string) -> ?pp_diff:(Format.formatter -> ('a * 'a) -> unit) -> ?msg:string -> 'a -> 'a -> unit

assert_equal expected real Compares two values, when they are not equal a failure is signaled.

  • parameter cmp

    customize function to compare, default is =

  • parameter printer

    value printer, don't print value otherwise

  • parameter pp_diff

    if not equal, ask a custom display of the difference using diff fmt exp real where fmt is the formatter to use

  • parameter msg

    custom message to identify the failure

  • version 1.1.0
Sourceval assert_raises : ?msg:string -> exn -> (unit -> 'a) -> unit

Asserts if the expected exception was raised.

  • parameter msg

    identify the failure

Skipping tests

In certain condition test can be written but there is no point running it, because they are not significant (missing OS features for example). In this case this is not a failure nor a success. Following functions allow you to escape test, just as assertion but without the same error status.

A test skipped is counted as success. A test todo is counted as failure.

Sourceval skip_if : bool -> string -> unit

skip cond msg If cond is true, skip the test for the reason explain in msg. For example skip_if (Sys.os_type = "Win32") "Test a doesn't run on windows".

  • since 1.0.3
Sourceval todo : string -> unit

The associated test is still to be done, for the reason given.

  • since 1.0.3

Compare Functions

Sourceval cmp_float : ?epsilon:float -> float -> float -> bool

Compare floats up to a given relative error.

  • parameter epsilon

    if the difference is smaller epsilon values are equal

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)

Sourceval bracket : (unit -> 'a) -> ('a -> unit) -> ('a -> unit) -> unit -> unit

bracket set_up test tear_down The set_up function runs first, then the test function runs and at the end tear_down runs. The tear_down function runs even if the test failed and help to clean the environment.

Sourceval bracket_tmpfile : ?prefix:string -> ?suffix:string -> ?mode:open_flag list -> ((string * out_channel) -> unit) -> unit -> unit

bracket_tmpfile test The test function takes a temporary filename and matching output channel as arguments. The temporary file is created before the test and removed after the test.

  • parameter prefix

    see Filename.open_temp_file

  • parameter suffix

    see Filename.open_temp_file

  • parameter mode

    see Filename.open_temp_file

  • since 1.1.0

Constructing Tests

Sourcetype test_fun = unit -> unit

The type of test function

Sourcetype test =
  1. | TestCase of test_fun
  2. | TestList of test list
  3. | TestLabel of string * test

The type of tests

Sourceval (>:) : string -> test -> test

Create a TestLabel for a test

Sourceval (>::) : string -> test_fun -> test

Create a TestLabel for a TestCase

Sourceval (>:::) : string -> test list -> test

Create a TestLabel for a TestList

Some shorthands which allows easy test construction.

Examples:

  • "test1" >: TestCase((fun _ -> ())) => TestLabel("test2", TestCase((fun _ -> ())))
  • "test2" >:: (fun _ -> ()) => TestLabel("test2", TestCase((fun _ -> ())))
  • "test-suite" >::: ["test2" >:: (fun _ -> ());] => TestLabel("test-suite", TestSuite([TestLabel("test2", TestCase((fun _ -> ())))]))
Sourceval test_decorate : (test_fun -> test_fun) -> test -> test

test_decorate g tst Apply g to test function contains in tst tree.

  • since 1.0.3
Sourceval test_filter : ?skip:bool -> string list -> test -> test option

test_filter paths tst Filter test based on their path string representation.

  • parameter skip

    if set, just use skip_if for the matching tests.

  • since 1.0.3

Retrieve Information from Tests

Sourceval test_case_count : test -> int

Returns the number of available test cases

Sourcetype node =
  1. | ListItem of int
  2. | Label of string

Types which represent the path of a test

Sourcetype path = node list

The path to the test (in reverse order).

Sourceval string_of_node : node -> string

Make a string from a node

Sourceval string_of_path : path -> string

Make a string from a path. The path will be reversed before it is translated into a string

Sourceval test_case_paths : test -> path list

Returns a list with paths of the test

Performing Tests

Sourcetype test_result =
  1. | RSuccess of path
  2. | RFailure of path * string
  3. | RError of path * string
  4. | RSkip of path * string
  5. | RTodo of path * string

The possible results of a test

Sourcetype test_event =
  1. | EStart of path
    (*

    A test start.

    *)
  2. | EEnd of path
    (*

    A test end.

    *)
  3. | EResult of test_result
    (*

    Result of a test.

    *)

Events which occur during a test run.

Sourcetype test_results = test_result list

Results of a test run.

Sourceval perform_test : (test_event -> unit) -> test -> test_results

Perform the test, allows you to build your own test runner

Sourceval run_test_tt : ?verbose:bool -> test -> test_results

A simple text based test runner.

  • parameter verbose

    print verbose message

Sourceval run_test_tt_main : ?arg_specs:(Arg.key * Arg.spec * Arg.doc) list -> ?set_verbose:(bool -> unit) -> test -> test_results

Main version of the text based test runner. It reads the supplied command line arguments to set the verbose level and limit the number of test to run.

  • parameter arg_specs

    add extra command line arguments

  • parameter set_verbose

    call a function to set verbosity

  • parameter fexit

    call a final function after test, by default exit 1.

  • version 1.1.0
Sourceval ounit2_of_ounit1 : test -> OUnit2.test
OCaml

Innovation. Community. Security.