Unit test building blocks (v2).
Context of a test.
The type of test.
The expected length of the test.
Assertions are the basic building blocks of unittests.
Signals a failure. This will raise an exception with the specified string.
Failure
signal a failure
Signals a failure when bool is false. The string identifies the failure.
Failure
signal a failure
Signals a failure when the string is non-empty. The string identifies the failure.
Failure
signal a failure
assert_command prg args
Run the command provided.
char Stream.t
as input of the process
assert_equal
to check it
stderr
to stdout
assert_equal expected real
Compares two values, when they are not equal a
failure is signaled.
=
diff fmt exp real
where fmt
is the formatter to use
Failure
signal a failure
Asserts if the expected exception was raised.
Failure
description
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.
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"
.
The associated test is still to be done, for the reason given.
Compare floats up to a given relative error.
epsilon
values are equal
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.
bracket_tmpfile test_ctxt
Create a temporary filename and matching output
channel. The temporary file is removed after the test.
Filename.open_temp_file
Filename.open_temp_file
Filename.open_temp_file
bracket_tmpdir test_ctxt
Create a temporary dirname. The temporary
directory is removed after the test.
Filename.open_temp_file
Filename.open_temp_file
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.
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 _ -> ())))]))
Severity level for log.
Log into OUnit logging system.
Build a filename for a file that should be located in the test data dir.
The test data dir, can be defined on the command line (preferably absolute) The default option is to locate it in topsrcdir/test/data.
Define command line options, environment variables and file configuration.
This module helps to define configuration options that are translated to command line options et al.
The name defined for the variable is:
make_string name default help
Create a string configuration
option with default value default
and a short help string.
The result of the partial application of the function can be used
inside tests to be evaluated to a value.
let my_option = Conf.make_string "my_option" "the default" "A default option."
let tests =
"ATest" >::
(fun test_ctxt -> let option_value = my_option test_ctxt in ())