Zep*_*ock 5 c c++ automake googletest
我正在尝试使用 google test 测试我的 C 库,但在使用框架模拟函数时遇到问题fff.h。这是我的文件结构:
.\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 Makefile.am\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 configure.ac\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 include\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 Makefile.am\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 public_header.h\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 src\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 libmylib\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 Makefile.am\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 private_functions.c\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 private_functions.h\n\xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 libmylib.la\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 libmylib.c\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 test\n \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 libmylib_test\n \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 Makefile.am\n \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 fff.h\n \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 test.cc\nRun Code Online (Sandbox Code Playgroud)\n\n我想从标头模拟一个函数,该函数在使用框架private_functions.h的函数中使用。public_header.hfff.h
public_function()\n{\n private_function(); //This function is the one I want to mock.\n}\nRun Code Online (Sandbox Code Playgroud)\n\n我的测试如下所示:
\n\n#include "gtest/gtest.h"\n#include "public_header.h"\n#include "fff.h"\n\nextern "C" {\n #include "private_functions.h"\n}\n\nDEFINE_FFF_GLOBALS;\n\nFAKE_VALUE_FUNC(int, function, char *, char *);\n\nclass libtest : public testing::Test\n{\npublic: \n virtual void SetUp()\n {\n RESET_FAKE(function);\n }\n\n virtual void TearDown()\n {\n }\n};\n\nTEST_F(libtest, test_fff)\n{\n public_function("val1", "val2");\n EXPECT_EQ(function_fake.call_count, 1);\n}\n...\nRun Code Online (Sandbox Code Playgroud)\n\n当我运行 make 时,它说private_function()已定义多次。
我的test/libmylib_test/MakeFile.am看起来像这样:
LIBSRC = $(top_srcdir)/src/libstorage\n\ncheck_PROGRAMS = libmylib_test\nlibstorage_test_SOURCES = test.cc\nlibstorage_test_CFLAGS = $(AM_CFLAGS)\nlibstorage_test_CXXFLAGS = -I$(top_srcdir)/include -I$(LIBSRC) -std=c++11 $(AM_CPPFLAGS)\nlibstorage_test_LDFLAGS = $(AM_LDFLAGS) -static -pthread\nlibstorage_test_LDADD = $(top_srcdir)/src/libmylib/libmylib.la\nRun Code Online (Sandbox Code Playgroud)\n
我有一个解决方案,但我不确定它是否是最好的。
当我编译我的库时,我想测试它.o为我拥有的每个.c文件生成文件。因为我只想测试,所以libmylib.c我只链接libmylib_la-libmylib.o而不是.la文件。
生成的.o文件为src/libmylib/:
libmylib_la-libmylib.o
libmylib_la-private_functions.o
Run Code Online (Sandbox Code Playgroud)
我的更新MakeFile.am现在./test/libmylib_test/看起来像这样:
LIBSRC = $(top_srcdir)/src/libstorage
check_PROGRAMS = libmylib_test
libstorage_test_SOURCES = test.cc
libstorage_test_CFLAGS = $(AM_CFLAGS)
libstorage_test_CXXFLAGS = -I$(top_srcdir)/include -I$(LIBSRC) -std=c++11 $(AM_CPPFLAGS)
libstorage_test_LDFLAGS = $(AM_LDFLAGS) -static -pthread
libstorage_test_LDADD = $(top_srcdir)/src/libmylib/libmylib_la-libmylib.o
Run Code Online (Sandbox Code Playgroud)
因为我在其中还有另一个功能private_functions.h我也必须模拟该函数,因为它没有在其他地方定义。
在这些更新之后,测试编译没有问题并且运行良好。
测试文件现在看起来像这样:
#include "gtest/gtest.h"
#include "public_header.h"
#include "fff.h"
extern "C" {
#include "private_functions.h"
}
DEFINE_FFF_GLOBALS;
FAKE_VALUE_FUNC(int, function, char *, char *);
FAKE_VOID_FUNC(other_func, char *);
...
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4508 次 |
| 最近记录: |