1

I'm integrating CMocka into my C library project using FetchContent in CMake.

cmake_minimum_required(VERSION 3.31)
project(mylib C)

set(CMAKE_C_STANDARD 11)

add_library(mylib STATIC src/library.c)
target_include_directories(mylib PUBLIC include)

# Download CMocka
include(FetchContent)
FetchContent_Declare(
        cmocka
        GIT_REPOSITORY https://git.cryptomilk.org/projects/cmocka.git
        GIT_TAG        cmocka-1.1.5
)
set(CMOCKA_TESTS OFF CACHE BOOL "Disable CMocka self-tests")
FetchContent_MakeAvailable(cmocka)
add_library(cmocka::cmocka ALIAS cmocka)

# Add test
enable_testing()

add_executable(mylib_test test/mylib_test.c)
target_link_libraries(mylib_test PRIVATE mylib cmocka::cmocka)

add_test(NAME MyLibTests COMMAND mylib_test)

My test executable is working fine, but when I run ctest, I see extra tests like:

Start 1: MyLibTests
Start 2: simple_test
Start 3: allocate_module_test
Start 4: assert_macro_test
Start 5: assert_module_test

The additional ones (simple_test, assert_module_test, etc.) are not from my code — they appear to be from CMocka itself

Question

How do I prevent CMocka’s own internal tests from being built and registered with CTest when using FetchContent_MakeAvailable?

What I’ve tried:

I tried to switch them off as well without success.

set(CMOCKA_TESTS OFF CACHE BOOL "Disable CMocka self-tests")

Goal:

Only my own tests should appear in ctest, not CMocka’s internal ones.

1

1 Answer 1

0

Instead of the following line on your CMakeLists.txt :

set(CMOCKA_TESTS OFF CACHE BOOL "Disable CMocka self-tests")

You need to use this one :

set(WITH_EXAMPLES OFF CACHE BOOL "")

CMake options defined for cmocka are on the file DefineOptions.cmake (like this for the version 1.1.5) :

option(WITH_STATIC_LIB "Build with a static library" OFF)
option(WITH_CMOCKERY_SUPPORT "Install a cmockery header" OFF)
option(WITH_EXAMPLES "Build examples" ON)
option(UNIT_TESTING "Build with unit testing" OFF)
option(PICKY_DEVELOPER "Build with picky developer flags" OFF)

if (WITH_STATIC_LIB)
    set(BUILD_STATIC_LIB ON)
endif (WITH_STATIC_LIB)

if (UNIT_TESTING)
  set(BUILD_STATIC_LIB ON)
endif (UNIT_TESTING)

So, if you don't defined WITH_EXAMPLES, the default value is to ON.

You can also passed the argument on the command line like this :

cmake -B build -DWITH_EXAMPLES=OFF

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.