CppUTest Unit Testing Framework Multiple Exception Definition

I will try to make this a purely minimal example, to be applicable to as many people as possible, as well as to protect any kind of code sharing that may violate the NDA. Hope all is well!

I use CppUTest and CppUMock (compiling with gcc / g ++ and make files created with CMake) in combination with the Gitlab Continuous Integration software to create a single test environment for future commits and software releases. However, I ran into a problem. Let's say I have the following folder setting (I have the minimal ability to change it besides the contents of the / tests folder):

+-- src +-- driver1.c +-- driver2.c +-- inc +-- driver1.h +-- driver2.h +-- tests +-- test_driver1.cpp +-- test_driver2.cpp +-- main.cpp +-- cmakelists.txt 

The CMakeLists file will include the inc folder, the compilation of the src folder, and the compilation of the test folder. However, let's say that driver2.c relies on methods defined by driver1.c. This is normal if there is no mocking setup, because you can just check the results of calls using driver2 methods. However, say that I want to mock the driver1 method1 function so that I can correctly check the operator1 method of operator1 method (using CppUMock). This is usually normal if driver1 does not compile, but adds something like the file test_driver2.cpp:

 void method1(int n) { mock().actualCall("method1").withParameter("n", n); } 

There will be a collision with the actual method1 in driver 1.c with a linker error:

 CMakeFiles/Tests.dir/.../src/driver1.c:(.text+0x11d): multiple definition of 'method1' CMakeFiles/Tests.dir/.../src/test_driver2.cpp:(.text+0x0): first defined here 

As requested by the commentator, here is what the include structure is:

 driver1.c includes driver1.h (obviously) driver2.c includes driver2.h (obviously) driver2.h includes driver1.h (for calling method1) test cpp files include their respective .h files (test_driver1.cpp -> driver1.h and test_driver2.cpp -> driver2.h) 

method1 is declared in driver1.h and defined in driver1.c. I can not edit these files.

I am pleased to add data on request.

What is the best way to get around this mocking problem?

+6
source share
1 answer

If you want to make fun of method1 from driver1.h , just add the mocked definition to a separate mock_driver1.cpp file, and then to your CMakeLists.txt:

 add_executable(target1 test_driver1.cpp driver1.cpp) add_executable(target2 test_driver2.cpp driver2.cpp mock_driver1.cpp) 

Once you finish the mockery, replace the mock_driver1.cpp dependency with driver1.cpp .

All this assumes that for each test driver there is a separate executable file.

However, if you want to have one big main program in which all the drivers are connected to each other, then you cannot coexist between the real method1 and the funny method1 at the same time. To do this, I would recommend wrapping the tricked method1 in the mock namespace or something like this and calling only mock::test1 in test_driver2.cpp.

+3
source

All Articles