mercredi 31 août 2016

CppUTest Unit Testing Framework Multiple Definition Exception

I will try to make this a purely minimal example to be as applicable to as many people as possible as well as protect any sort of code sharing that might violate an NDA. Hope this is okay!

I am using CppUTest and CppUMock (compiling with gcc/g++ and makefiles created with CMake) in conjunction with Gitlab Continuous Integration software to create a unit testing environment for future commits and release of software. However, I have run into a bit of a problem. Let's say I have the following folder setup (that I have minimal ability to change, other than 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 contain including of the inc folder, compilation of the src folder, and compilation of the tests folder. However, let's say that driver2.c relies on methods defined by driver1.c. This is fine if there is no mocking setup because you can just test the results of calls to driver2's methods normally. However, say that I want to mock driver1's method1 function so that I can check that driver2 calls method1 correctly (using CppUMock). This would usually be fine, if driver1 wasn't being compiled, but adding something like so to the test_driver2.cpp file:

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

Will cause a collision with the actual method1 in driver1.c with a linker error like so:

CMakeFiles/http://ift.tt/2c0QURP: multiple definition of 'method1'
CMakeFiles/http://ift.tt/2c8Wij0: first defined here

I'm happy to add details as requested.

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

Aucun commentaire:

Enregistrer un commentaire