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 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 Stream.t->?foutput:(char Stream.t-> unit)->?use_stderr:bool ->?backtrace:bool ->?chdir:string ->?env:string array->ctxt:test_ctxt->string ->string list->
unit
assert_command prg args Run the command provided.
parameterexit_code
expected exit code
parametersinput
provide this char Stream.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
parameterbacktrace
Set OCAMLRUNPARAM=b
parameterchdir
Chdir into a directory before running the command.
parameterenv
Unix environment
parameterverbose
if a failed, dump stdout/stderr of the process to stderr
val assert_equal :
?ctxt:test_ctxt->?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
parameterctxt
if provided, always print expected and real value
raisesFailure
signal a failure
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".
val todo : string -> unit
The associated test is still to be done, for the reason given.
Compare Functions
val cmp_float : ?epsilon:float ->float ->float -> bool
Compare floats up to a given relative error.
In keeping with standard floating point semantics, NaN is not equal to anything: cmp_float nan nan = false.
parameterepsilon
if the difference is smaller epsilon values are equal
Bracket
A bracket is a registered object with setUp and tearDown in unit tests. Data generated during the setUp will be automatically tearDown when the test ends.
with_bracket_chdir test_ctxt dn f change directory to dn during execution of function f. In order to Sys.chdir, we need to take a lock to avoid other tests trying to do change the current directory at the same time. So this bracket is not directly accessible in order to use it only on shorter piece of code.
non_fatal ctxt f Run f but if an exception is raised or an assert fails, don't stop, just register the result. The global test running result will mix in the non fatal result to determine the success or failure of the test.