Legend:
Library
Module
Module type
Parameter
Class
Class type
Unit test building blocks
author Maas-Maarten Zeeman
author Sylvain Le Gall
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.
raisesFailure
signal a failure
val assert_bool : string ->bool -> unit
Signals a failure when bool is false. The string identifies the failure.
raisesFailure
signal a failure
val (@?) : string ->bool -> unit
Shorthand for assert_bool
raisesFailure
to signal a failure
val assert_string : string -> unit
Signals a failure when the string is non-empty. The string identifies the failure.
raisesFailure
signal a failure
val 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.
parameterexit_code
expected exit code
parametersinput
provide this char Seq.t as input of the process
parameterfoutput
run this function on output, it can contains an assert_equal to check it
parameteruse_stderr
redirect stderr to stdout
parameterenv
Unix environment
parameterverbose
if failed, dump stdout/stderr of the process to stderr
since 1.1.0
val 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.
parametercmp
customize function to compare, default is =
parameterprinter
value printer, don't print value otherwise
parameterpp_diff
if not equal, ask a custom display of the difference using diff fmt exp real where fmt is the formatter to use
parametermsg
custom message to identify the failure
raisesFailure
signal a failure
version 1.1.0
val assert_raises : ?msg:string ->exn ->(unit ->'a)-> unit
Asserts if the expected exception was raised.
parametermsg
identify the failure
raisesFailure
description
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.
val 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
val todo : string -> unit
The associated test is still to be done, for the reason given.
since 1.0.3
Compare Functions
val cmp_float : ?epsilon:float ->float ->float -> bool
Compare floats up to a given relative error.
parameterepsilon
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:
val 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.
val bracket_tmpfile :
?prefix:string ->?suffix:string ->?mode:Pervasives.open_flag list->((string * Pervasives.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.