[libubootenv,v2,6/8] tests: add unit test infrastructure based on cmocka
Commit Message
Add the BUILD_TESTS option, disabled by default, to build unit tests
based on cmocka and run them with ctest, and enable it in the CI.
Signed-off-by: Dario Binacchi <dario.binacchi@amarulasolutions.com>
---
Changes in v2:
- New patch
.gitlab-ci.yml | 4 +++-
CMakeLists.txt | 7 +++++++
tests/CMakeLists.txt | 11 +++++++++++
3 files changed, 21 insertions(+), 1 deletion(-)
create mode 100644 tests/CMakeLists.txt
--
2.43.0
@@ -18,11 +18,12 @@ ubuntu-jammy:
- apt-get install -y zlib1g-dev
- apt-get install -y curl
- apt-get install -y git
+ - apt-get install -y libcmocka-dev
script:
- if [ ! -d Debug ];then mkdir Debug;fi
- cd Debug
- - cmake -DCMAKE_BUILD_TYPE=Debug ..
+ - cmake -DCMAKE_BUILD_TYPE=Debug -DBUILD_TESTS=ON ..
- if [ $CI_COMMIT_BRANCH == "coverity_scan" ]; then
curl -o /tmp/cov-analysis-linux64.tgz https://scan.coverity.com/download/linux64 --form project=$COVERITY_SCAN_PROJECT_NAME --form token=$COVERITY_SCAN_TOKEN;
tar xfz /tmp/cov-analysis-linux64.tgz;
@@ -31,6 +32,7 @@ ubuntu-jammy:
curl https://scan.coverity.com/builds?project=$COVERITY_SCAN_PROJECT_NAME --form token=$COVERITY_SCAN_TOKEN --form email=$GITLAB_USER_EMAIL --form file=@cov-int.tar.gz --form version="`git describe --tags`" --form description="`git describe --tags` / $CI_COMMIT_TITLE / $CI_COMMIT_REF_NAME:$CI_PIPELINE_ID ";
fi
- make VERBOSE=1
+ - ctest --output-on-failure
reuse:
needs: []
@@ -32,6 +32,13 @@ set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu99")
include_directories ("${PROJECT_SOURCE_DIR}/src")
add_subdirectory (src)
+option(BUILD_TESTS "Build the unit tests" OFF)
+
+if(BUILD_TESTS)
+ enable_testing()
+ add_subdirectory(tests)
+endif(BUILD_TESTS)
+
# first we can indicate the documentation build as an option and set it to ON by default
option(BUILD_DOC "Build documentation" ON)
new file mode 100644
@@ -0,0 +1,11 @@
+# SPDX-FileCopyrightText: 2026 Amarula Solutions, Dario Binacchi <dario.binacchi@amarulasolutions.com>
+#
+# SPDX-License-Identifier: LGPL-2.1-or-later
+
+find_path(CMOCKA_INCLUDE_DIR cmocka.h)
+find_library(CMOCKA_LIBRARY cmocka)
+if(NOT CMOCKA_INCLUDE_DIR OR NOT CMOCKA_LIBRARY)
+ message(FATAL_ERROR "cmocka is required to build the tests")
+endif()
+
+include_directories(${CMOCKA_INCLUDE_DIR})