Basic Unit Test
Below is a basic unit test that simply asserts that a particular string is printed.
ct_add_test(NAME hello_world)
function(${CMAKETEST_TEST})
message("Hello World")
ct_assert_prints("Hello World")
endfunction()
The ct_add_test
call tells CMakeTest that there is a test
with the name hello_world
. The function definition below it
defines the test. Note the odd ${CMAKETEST_TEST}
as the function name.
This is required to link the function definition with the test. The
value of the implicitly set CMAKETEST_TEST
variable is a unique
identifier for the test, it’s only used internally.
The name of the test may be any string and can include special characters. If the name has such special characters, pass it as a bracket argument instead of as a quoted argument.
Note
There is a secondary, deprecated form of naming tests and sections.
This secondary form has restrictions on the name given such that it
is a valid CMake variable identifier. New tests and sections should
use the above form instead. See ct_add_test
for information on the deprecated form.
Inside the function one will notice a call to
ct_assert_prints
.
This is one of the included assertion functions. If an assertion fails,
the test is stopped and labeled as failing in the output.
Subsequent tests will still be ran.