[英]using googletest in eclipse: how?
我已经下载了 google test,但现在我不知道如何将它链接到我在 eclipse 中的项目。 我应该将它添加为源文件夹吗? 应该将它包含为 g++ 包含的库吗? 那么我该如何运行测试呢?
使用里加的优秀答案,这里是我如何让它工作的总结:
./scripts/fuse_gtest_files.py . <project-dir>/contrib
./scripts/fuse_gtest_files.py . <project-dir>/contrib
contrib
目录,并将<project-dir>/contrib
到包含目录(奇怪,我知道)src
目录并添加了一个名为Foo
的类( Foo.h
的内容见下文——我Foo.cpp
留空)test
目录,将其从发布构建配置中排除,将<project-dir>/contrib
到包含目录,并添加了新的源文件FooTest.cpp
和AllTests.cpp
(内容见下文)福.h:
#ifndef FOO_H_
#define FOO_H_
class Foo {
public:
virtual ~Foo();
Foo();
bool foo(void) { return true; }
};
#endif /* FOO_H_ */
FooTest.cpp:
#include "gtest/gtest.h"
#include "Foo.h"
namespace {
class FooTest : public ::testing::Test {
protected:
Foo foo;
};
TEST_F(FooTest, Foo) {
ASSERT_TRUE(foo.foo());
}
}
AllTests.cpp:
#include "gtest/gtest.h"
#include "FooTest.cpp"
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
以下是详细步骤:
cd /tmp
wget http://googletest.googlecode.com/files/gtest-1.5.0.tar.bz2
cd gtest-1.5.0/
./scripts/fuse_gtest_files.py . <project-dir>/contrib
contrib
文件夹,选择Exclude from build...*,仅取消勾选**Release框,然后单击OKcontrib
文件夹并选择Properties > C/C++ Build > Settings > Tool Settings选项卡 > GCC C++ Compiler > Directories<project-name>/contrib
并单击OK添加目录src
作为其名称,然后单击OKsrc
文件夹,然后选择New > Class ,将其命名为Foo
,然后单击OK (有关Foo.h
内容,请参见上文; Foo.cpp
可以保持原样)test
作为其名称,然后单击OK<project-name>/contrib
和<project-name>/src
作为包含目录添加到test
目录test
文件夹,然后选择New > Source File将AllTests.cpp
添加到test
文件夹,然后重复相同的步骤添加FooTest.cpp
(内容见上文)FooTest.cpp
并选择Exclude from build... ,单击Select All按钮,然后单击 OK步骤 1 安装 Eclipse
如果机器上尚未安装 Eclipse,请从 Eclipse 下载页面 ( http://www.eclipse.org/downloads/ ) 获取最新版本的 Eclipse IDE for C/C++ Developers。
如果 Eclipse 已安装但仅适用于 Java,请按照以下说明下载 C++ 插件。
一种。 打开Eclipse,点击Help->Install New Software
湾在 Work with: 框中,输入http://download.eclipse.org/tools/cdt/releases/kepler 。 片刻之后,名称框将出现。 选择以下组件:
C。 单击下一步,同意这些声明,然后单击完成。
第 2 步下载 Cygwin
通过单击 Cygwin 安装页面 ( http://www.cygwin.com/install.html ) 上的 setup-x86_64.exe 链接安装 Cygwin。 运行后,按默认值单击 Next,直到进入 Select Packages 窗口。
您将需要搜索并安装两个软件包:gcc 和 make。
第一个搜索词是 gcc。 搜索 gcc,然后打开 Devel 文件夹。 通过单击“跳过”一词标记以下要安装的软件包(然后它会更改为内部版本号,可能比此处描述的要高):gcc-core、gcc-g++ 和 libgcc1。
第二个搜索词是 make。 在这里,我们只需要 Devel 包 make。
选择这些后,单击下一步进行安装。
步骤 3 下载并构建 Google Test 项目
从https://code.google.com/p/googletest/downloads/list下载最新版本的 GoogleTest,并将 zip 文件内容解压到一个公共目录中。 重要的是所有用户都能够访问此目录。
(注:以下命令使用make
- GoogleTest的最后一个版本,它使用make
是https://github.com/google/googletest/releases/tag/release-1.8.1对于GoogleTest使用的未来版本。 cmake
来代替。 )
要构建 Google 测试项目:
cd c:/<<yourpath>>/gtest-1.7.0/make/
make
ar -rv libgtest.a gtest-all.o
步骤 4 将 Cygwin bin 目录添加到计算机的 PATH 变量中
按照此页面上适用于您的 Windows 版本的说明进行操作: http : //www.java.com/en/download/help/path.xml ,将 Cygwins bin 目录添加到计算机的 PATH 环境变量中。 (通常通过将 ;C:\\cygwin64\\bin 添加到当前值的末尾)。
步骤 5 创建一个使用 GoogleTest 的新项目
启动 Eclipse 并选择 File->New->C++ Project。 输入以下值并单击完成。
在 Project Explorer 中,右键单击项目名称并选择 Properties。 在 C/C++ Build 下,将 Builder 类型更改为 Internal Builder。
在 C/C++ Build 下,选择 Settings,然后单击 Cygwin C++ Compiler 下的 Includes 文件夹。 单击顶部框中的添加按钮,然后浏览到 GoogleTest 包含文件夹。
最后,在 Cygwin C++ Linker 文件夹下,选择 Miscellaneous,然后单击 Other objects 下的 Add 图标。 找到您在第 3 步中重新构建的 libgtest.a 文件(它应该位于解压缩的 gtest 文件夹的 make 目录中)。
就是这样! 现在您可以尝试一下了。
步骤 6 编写一些使用 GoogleTest 的代码
将下面的代码复制并粘贴到相应的文件中:
计数器.h
class Counter {
private:
int mCounter;
public:
Counter() : mCounter(0) {}
int Increment();
};
计数器.cpp
#include <stdio.h>
#include "Counter.h"
int Counter::Increment() {
return mCounter++;
}
Counter_Tests.cpp
#include "gtest/gtest.h"
#include "Counter.h"
TEST(Counter, Increment) {
Counter c;
EXPECT_EQ(0, c.Increment());
EXPECT_EQ(1, c.Increment());
EXPECT_EQ(2, c.Increment());
}
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
在项目菜单中选择全部构建。 现在,要连接 GoogleTest 单元测试框架,请从“运行”菜单中选择“运行配置”。 从该对话框中,选择 C/C++ 单元并单击新建按钮。
在C/C++ Application下应该自动填写这个项目名称,如果没有点击Search Project选择这个项目。 接下来,单击 C/C++ 测试选项卡。 在 Tests Runner 下拉列表中,选择 Google Tests Runner,然后单击 Run 观看魔术!
下面是结果的快照。 编写更多代码/测试后,您可以单击以红色突出显示的按钮以快速重新编译并重新运行所有测试。
您不应将其添加到源文件夹中,而应创建单独的文件夹。 这是为了避免您的生产代码依赖于测试项目。 像这样做
../ #your project folder
Makefile
src/
module1 #some module
module2 #another module
build #tmp for build
dist #binaries
contrib/
gtest
...
test/ #your test project folder
Makefile
src/
module1 #correspondent to main project's one
module2 #correspondent to main project's one
build
dist
...
我通常使用 google test 作为两个文件,这很方便。 使用 gtest 发行版中的scripts/fuse_gtest_files.py
来提取它们。 只有两个文件,您可以将它们的编译包含在您的测试项目编译中,并具有简单的项目结构。
在您的测试项目中指定包含目录../contrib:../src:src
。 因此,您可以包含这样的标题
测试/src/module1/class1Test.h:
#include "gtest/gtest.h"
#include "module1/class1.h"
// test class1 here
// ...
测试/src/mainTest.cpp:
#include "gtest/gtest.h"
#include "module1/class1Test.h"
#include "module2/class2Test.h"
// include other tests you have
// ...
int main(int argc, char** argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
这是我针对 Eclipse 4.3 和 CDT 8.2 的解决方案,我觉得这比上面描述的要容易一些。
下载 gtest 并按照 readme.txt 中的描述进行安装(在 linux 中使用 cmake 和 make)
转到“YourProject-> Properties-> C/C++ Build-> Settings-> GCC C++ Compiler-> Includes-> Include paths”并在gtest中添加include文件夹
转到“YourProject-> Properties-> C/C++ Build-> Settings-> GCC C++ Linker-> Libraries”,添加gtest文件夹作为搜索路径并添加库“gtest”和“pthread”
(4.如果你在同一个项目中有测试作为源排除发布版本中的测试)
转到“运行-> 运行配置...”并创建一个新的 C/C++ 单元运行配置
在主选项卡中将项目设置为您的项目并将 C/C++ 应用程序设置为您的应用程序。 在 C/C++ 测试选项卡中将测试运行程序设置为 Google 测试运行程序。
(7. eclipse gui 中可能会出现错误通知,如果是这种情况,重新索引项目可能会有所帮助)
我已经提供了您的解决方案,并且运行良好。 我可以说对于编译 gtest 在 README 中不是很清楚。 文本。
我已经通过 cygwin 控制台运行了 /make 目录中的 makefile。 就我而言,编译器建议我不要找到 pthread 库。 所以我修改了这条线
CXXFLAGS += -g -Wall -Wextra -pthread
并将其更改为
CXXFLAGS += -g -Wall -Wextra -lpthread
我得到的输出是gtest_main.a
。 然后我将此文件重命名为 libgtest.a 并将其复制到C:\\cygwin\\lib directory
。
然后我将我的 eclipse 项目配置为使用 cygwin 并按照您的说法添加了 gtest 和 pthread ......并且它有效!
我希望它可以帮助某人
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.