CMakeUnit is a small set of functions and macros useful when testing CMake code. There are three main scenarios how you can use CMakeUnit:
- TDD the code, functions and macros
- Run a part of the code in isolation
- Use assertions in production code
There are at least three ways to add CMakeUnit to your project:
-
Add CMakeUnit using FetchContent, add CMakeUnit source folder to CMAKE_MODULE_PATH for your convenience and include CMakeUnit.
include(FetchContent) FetchContent_Declare( cmakeunit GIT_REPOSITORY "https://github.com/tawez/CMakeUnit.git" GIT_TAG "ccb7cfdcdcffcf76036f9dc53c83e7a313924e4c" UPDATE_DISCONNECTED ON ) FetchContent_MakeAvailable(cmakeunit) list(PREPEND CMAKE_MODULE_PATH "${cmakeunit_SOURCE_DIR}") ... include(CMakeUnit)
- Use git tag or commit hash as
GIT_TAG UPDATE_DISCONNECTED ONis optional
- Use git tag or commit hash as
-
Add CMakeUnit as a git submodule, add path to CMakeUnit root folder to CMAKE_MODULE_PATH for your convenience and include CMakeUnit.
list(PREPEND CMAKE_MODULE_PATH "path/to/CMakeUnit") ... include(CMakeUnit)
-
Add a copy of CMakeUnit.cmake to the folder of your choice, add this folder to CMAKE_MODULE_PATH for your convenience and include CMakeUnit.
list(PREPEND CMAKE_MODULE_PATH "path/to/folder") ... include(CMakeUnit)
This is enough to use CMakeUnit assertions in production code wherever needed.
- Include CTest
- Add the location of CMakeUnit module and location of every module you want to test to the CMAKE_MODULE_PATH
- Add all the tests that you need with add_cmake_test
- Add test target with add_cmakeunit_target
When done, follow the usual steps to run the test target:
mkdir build
cd build
cmake ..
make <cmakeunit target name>For complete project setup and test examples, see CMakeUnit-example.
Add a test to be run by ctest. Test will be run with CMAKE_MODULE_PATH set to its current value.
add_cmake_test(<name> <path>
[SKIP]
[WILL_FAIL]
[OPTIONS args...])nameTest namepathPath to test source resolved from the CMAKE_CURRENT_SOURCE_DIR. If path is a folder, CMakeLists.txt is expected to be inside.SKIPSkip this test but keep it in test reportWILL_FAILDenote this test as expected to fail. Will set test property WILL_FAIL to true.OPTIONSAdditional test options. This is a convenient way to set up test ENV.
Add a custom target that will run ctest to execute registered tests.
add_cmakeunit_target(<name> [args...])nameTarget name (usually there is no need to pass anything more).argsAdditional target options (all add_custom_target options are accepted).
Generate a fatal error and abort the current test/code.
FATAL([text...])textText to display when reporting error.
Generate a non-fatal error and allow the current test/code to continue.
FAIL([text...])textText to display when reporting error.
The following assertions come as a pair of variants ASSERT and EXPECT.
Upon failure, ASSERT generates fatal error and aborts the execution of
the current test/code, while EXPECT generates non-fatal error and allows
the current test/code to continue.
NOTE: If not specified, error means fatal error for
ASSERTand non-fatal error forEXPECTassertions.
- ASSERT_TRUE / EXPECT_TRUE
- ASSERT_FALSE / EXPECT_FALSE
- ASSERT_DEFINED / EXPECT_DEFINED
- ASSERT_UNDEFINED / EXPECT_UNDEFINED
- ASSERT_STREQ / EXPECT_STREQ
- ASSERT_NOT_STREQ / EXPECT_NOT_STREQ
- ASSERT_MATCH / EXPECT_MATCH
- ASSERT_NOT_MATCH / EXPECT_NOT_MATCH
- ASSERT_EQ / EXPECT_EQ
- ASSERT_NE / EXPECT_NE
- ASSERT_LT / EXPECT_LT
- ASSERT_LE / EXPECT_LE
- ASSERT_GT / EXPECT_GT
- ASSERT_GE / EXPECT_GE
- ASSERT_LIST_LENGTH / EXPECT_LIST_LENGTH
- ASSERT_LIST_EQ / EXPECT_LIST_EQ
- ASSERT_LIST_CONTAINS / EXPECT_LIST_CONTAINS
Verify if the value is true.
ASSERT_TRUE(<value>)
EXPECT_TRUE(<value>)valueValue to test
errorif the value is not true
Verify if the value is false.
ASSERT_FALSE(<value>)
EXPECT_FALSE(<value>)valueValue to test
errorif the value is not false
Verify if the variable is defined.
ASSERT_DEFINED(<variable>)
EXPECT_DEFINED(<variable>)variableVariable to test
errorif the variable is not defined
Verify if the variable is undefined.
ASSERT_UNDEFINED(<variable>)
EXPECT_UNDEFINED(<variable>)variableVariable to test
errorif the variable is defined
Verify if two strings, value and expected, have the same content.
ASSERT_STREQ(<value> <expected>)
EXPECT_STREQ(<value> <expected>)valueValue to testexpectedExpected value
errorif values differ
Verify if two strings, value and expected, have different content.
ASSERT_NOT_STREQ(<value> <expected>)
EXPECT_NOT_STREQ(<value> <expected>)valueValue to testexpectedExpected value
errorif values are equal
Verify if the value matches the pattern.
ASSERT_MATCH(<value> <pattern>)
EXPECT_MATCH(<value> <pattern>)valueValue to testpatternPattern to test value against
errorif the value does not match the pattern
Verify if the value does not match the pattern.
ASSERT_NOT_MATCH(<value> <pattern>)
EXPECT_NOT_MATCH(<value> <pattern>)valueValue to testpatternPattern to test value against
errorif the value matches the pattern
Verify if the value equals to the expected.
ASSERT_EQ(<value> <expected>)
EXPECT_EQ(<value> <expected>)valueValue to testexpectedExpected value
errorif values differ
Verify if the value does not equal to the expected.
ASSERT_NE(<value> <expected>)
EXPECT_NE(<value> <expected>)valueValue to testexpectedExpected value
errorif values are equal
Verify if the value is less than the expected.
ASSERT_LT(<value> <expected>)
EXPECT_LT(<value> <expected>)valueValue to testexpectedExpected value
errorif the value is not less than expected
Verify if the value is less than or equal to the expected.
ASSERT_LE(<value> <expected>)
EXPECT_LE(<value> <expected>)valueValue to testexpectedExpected value
errorif the value is greater than expected
Verify if the value is greater than the expected.
ASSERT_GT(<value> <expected>)
EXPECT_GT(<value> <expected>)valueValue to testexpectedExpected value
errorif the value is not greater than expected
Verify if the value is greater than or equal to the expected.
ASSERT_GE(<value> <expected>)
EXPECT_GE(<value> <expected>)valueValue to testexpectedExpected value
errorif the value is less than expected
Verify if the length of the list is in relation to the given expected value.
ASSERT_LIST_LENGTH(<variable> <relation> <expected>)
EXPECT_LIST_LENGTH(<variable> <relation> <expected>)variableList variable to testrelationOne of:EQ,NE,LT,LE,GT,GEexpectedExpected list length
errorif the list length is not in relation to expected value
Verify if the value equals to the list made of the given item(s).
ASSERT_LIST_EQ(<value> [<item>...])
EXPECT_LIST_EQ(<value> [<item>...])valueValue to testitem...Expected content of the list
errorif the value differs from the given list of items
Verify if the value contains all the given item(s) without any particular order.
ASSERT_LIST_CONTAINS(<value> [<item>...])
EXPECT_LIST_CONTAINS(<value> [<item>...])valueValue to testitem...Expected content of the list
errorif the value does not contain at least one item
Define a mock function with the given name.
MOCK_FUNCTION(<name>)nameName of the mock to be created
fatal errorif mock function for the samenameis defined again
Verify if the mock has been called the expected number of times.
EXPECT_CALL_TIMES(<name> <expected>)nameMock function nameexpectedExpected number of mock function calls (no less than 0)
fatal errorif mock function is not defined or ifexpectedis less than 0non-fatal errorif mock function was not calledexpectednumber of times
Verify if the nth call of the mock function has been done with the given arguments.
EXPECT_CALL_WITH(<name> <nth> [<arg>...])nameMock function namenthNumber of the call of the mock function to verifyarg...List of expected arguments
fatal errorif mock function is not defined or ifnthis less than 1non-fatal errorif mock function has not been called or if has been called less thannthtimes or if expected arguments differ to actual