我将尝试将此作为一个纯粹的最小示例,以尽可能适用于尽可能多的人,并保护任何可能违反 NDA 的代码共享。希望这没问题!
我正在使用 CppUTest 和 CppUMock(使用 gcc/g++ 编译和使用 CMake 创建的 makefile)与 Gitlab 持续集成软件一起为未来的提交和软件发布创建单元测试环境。但是,我遇到了一些问题。假设我有以下文件夹设置(除了 /tests 文件夹的内容之外,我几乎没有更改的能力):
+-- src
+-- driver1.c
+-- driver2.c
+-- inc
+-- driver1.h
+-- driver2.h
+-- tests
+-- test_driver1.cpp
+-- test_driver2.cpp
+-- main.cpp
+-- cmakelists.txt
CMakeLists 文件将包含 inc 文件夹、src 文件夹的编译和测试文件夹的编译。但是,假设 driver2.c 依赖于 driver1.c 定义的方法。如果没有模拟设置,这很好,因为您可以正常测试调用驱动程序2 的方法的结果。但是,假设我想模拟 driver1 的 method1 函数,以便我可以检查 driver2 是否正确调用了 method1(使用 CppUMock)。这通常会很好,如果 driver1 没有被编译,但是在 test_driver2.cpp 文件中添加类似的东西:
void method1(int n) {
mock().actualCall("method1").withParameter("n", n);
}
将导致与 driver1.c 中的实际方法 1 发生冲突,并出现如下链接器错误:
CMakeFiles/Tests.dir/.../src/driver1.c:(.text+0x11d): multiple definition of 'method1'
CMakeFiles/Tests.dir/.../src/test_driver2.cpp:(.text+0x0): first defined here
根据评论者的要求,这是包含结构的样子:
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 在 driver1.h 中声明并在 driver1.c 中定义。我无法编辑这些文件。
我很高兴根据要求添加详细信息。
解决这个嘲笑问题的最佳方法是什么?