Using Google Test for C-Code and mocking C-Modules

I currently face a situation where I have to use Google Test for C-Code. So I am not really able to use gmock to mock C-modules. What I want to do instead is use the CMake build system to choose mocked modules when needed. I want to accomplish that if possible by telling the build system to first search for includes/header files in lets say "path/to/mock/include" and if the regarding header file cant be found there I want it to choose the header file from the "normal" production code path like "path/to/production/include". That I want to do for every target I define. A target will be the test suite for a specific C-module. Normally that should be possible like this: Why do I want to mock? Some of the modules my module under test includes are hardware dependent.
target_include_directories(TARGET "path/to/mock/include")
target_include_directories(TARGET "path/to/production/include")
target_include_directories(TARGET "path/to/mock/include")
target_include_directories(TARGET "path/to/production/include")
You first show cmake the path to the header files of the mocked modules and then to production includes. And that order should also be the order in which the header files are searched for. But somehow I cant get this to work. It seems like the build system ignores the mocked modules and only wants to use the "real" ones. Do you all have some hints about what could be going wrong? Am I missing something? Is there an other/better way to achieve this? Thank you all in advance! Flo
7 Replies
ZacckOsiemo
ZacckOsiemo4mo ago
@Undefined can this be done with Cmake?
Undefined
Undefined4mo ago
@Flo you can use ordering And it should be target_include_directories rather than target_link_directories So it will be
target_include_directories(TARGET "main_dir_path")
target_include_directories(TARGET "secondary_dir_path")
target_include_directories(TARGET "main_dir_path")
target_include_directories(TARGET "secondary_dir_path")
Flo
FloOP4mo ago
@Undefined Sorry for the confusion but I am already using "target_include_directories()". And yes I think so too that the ordering should be sufficient. Seems like I have another Problem I havnt identified yet.
Undefined
Undefined4mo ago
what is the problem can you please show me the error?
Flo
FloOP4mo ago
Its on the notebook from work and also confidential so I cant really show the real code sorry. But I will get back to you if I will set up a similar environment on my private machine. Hope to get some time for it this weekend.
Undefined
Undefined4mo ago
or don't need the error message can you please tell what is the error about? You first show cmake the path to the header files of the mocked modules and then to production includes. And that order should also be the order in which the header files are searched for. But somehow I cant get this to work. It seems like the build system ignores the mocked modules and only wants to use the "real" ones. Do you all have some hints about what could be going wrong? Am I missing something? Is there an other/better way to achieve this? if i consider this as your problem then i can show you this
option(PRODUCTION "Mock Build" OFF)
if(NOT PRODUCTION)
target_include_directories(TARGET "path/to/mock/include")
else()
target_include_directories(TARGET "path/to/production/include")
endif()
option(PRODUCTION "Mock Build" OFF)
if(NOT PRODUCTION)
target_include_directories(TARGET "path/to/mock/include")
else()
target_include_directories(TARGET "path/to/production/include")
endif()
then build with cmake -DPRODUCTION=ON/OFF "build dir path" @Flo Hope this helps
Flo
FloOP4mo ago
Thanks for the answer! The problem with this solution would be that I want to mix mocked modules and real modules. Only use mocks where hardware dependencies are present. Otherwise Im finde with using the real modules. Also I dont want to change production code so i cant wrap the local #inlude inside the modul under tests C-File with:
#if defined(PRODUCTION)
#include "module.c"
#else
#include "mocked_module.c"
#endif
#if defined(PRODUCTION)
#include "module.c"
#else
#include "mocked_module.c"
#endif
Want results from more Discord servers?
Add your server