在 Eclipse 中使用 googletest:如何?

发布于 2024-09-28 06:57:44 字数 98 浏览 6 评论 0 原文

我已经下载了 google test,但现在我不知道如何将其链接到 eclipse 中的项目。 我应该将其添加为源文件夹吗?应该将其作为 g++ 包含库包含吗?那我该如何运行测试呢?

I've downloaded google test, but now I've no idea on how to link it to my project in eclipse.
Should I add it as a source folder? Should include it as g++ included library? And how can I run test then?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

谁与争疯 2024-10-05 06:57:44

使用里加的出色答案,以下是我如何得到它的摘要工作:

  1. 在 Eclipse 中创建了一个新的 C++ 项目(我选择了“可执行文件”>“空项目”)
  2. 下载了 googletest 1.5.0,解压并运行 ./scripts/fuse_gtest_files.py 。 /contrib
  3. 回到 Eclipse,从 Release 构建配置中排除 contrib 目录,并添加 /contrib到包含目录(奇怪,我知道)
  4. 添加了一个 src 目录并添加了一个名为 Foo 的类(请参阅下面的 Foo.h 内容>--我暂时将 Foo.cpp 留空)
  5. 在 Eclipse 中添加了 test 目录,将其从发布构建配置中排除,添加了 ;/contrib 到包含目录,并添加新的源文件 FooTest.cppAllTests.cpp (内容见下文)
  6. 构建并运行项目!

Foo.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();
}

下面是详细步骤:

  1. 在Eclipse中,打开File菜单,选择New > C++ 项目
  2. 项目类型:可执行文件 > 空项目
  3. 工具链:Linux GCC
  4. 单击完成
  5. 打开终端并cd /tmp
  6. wget http:// /googletest.googlecode.com/files/gtest-1.5.0.tar.bz2
  7. cd gtest-1.5.0/
  8. ./scripts/fuse_gtest_files.py 。 /contrib
  9. 返回 Eclipse,右键单击 Project Explorer 窗格中的项目文件夹,然后选择 Refresh
  10. 在 Project Explorer 窗格中,右键单击contrib 文件夹中,选择从构建中排除...*,仅取消选中 **Release 框,然后单击确定
  11. 右键单击​​ < code>contrib 文件夹并选择属性 > C/C++ 构建 > 设置 > 工具设置选项卡> GCC C++ 编译器 > 目录
  12. 单击添加...按钮,然后单击工作区...按钮,然后选择 /contrib 并单击确定添加目录
  13. 再次单击确定接受对构建设置的更改
  14. 在 Project Explorer 窗格中右键单击项目并选择新建> 文件夹,输入 src 作为其名称,然后单击确定
  15. 右键单击​​项目资源管理器中的 src 文件夹窗格并选择新建> Class,将其命名为Foo,然后单击OKFoo.h的内容见上文; Foo.cpp 可以保留原样)
  16. 在 Project Explorer 窗格中右键单击该项目,然后选择 New > 文件夹,输入test作为其名称,然后单击确定
  17. 按照上述步骤添加/contrib< /code> 和 /src 作为 test 目录的包含目录
  18. 右键单击 test 文件夹,然后选择 > 源文件AllTests.cpp添加到test文件夹,然后重复相同的步骤添加FooTest.cpp (内容见上文)
  19. 右键单击FooTest.cpp并选择从构建中排除...,单击全选按钮,然后确定
  20. 右键单击​​“项目资源管理器”窗格中的项目,然后选择属性 > C/C++ 构建 > 设置 > 工具设置选项卡> GCC C++ 链接器 > Libraries,然后点击Add...按钮,输入pthread(googletest需要),点击OK即可添加库,然后再次确定接受更改
  21. Ctrl-b构建项目
  22. Ctrl-F11运行项目
  23. Victory !

Using Riga's excellent answer, here is a summary of how I got it to work:

  1. Created a new C++ project in Eclipse (I chose Executable > Empty Project)
  2. Downloaded googletest 1.5.0, untarred, and ran ./scripts/fuse_gtest_files.py . <project-dir>/contrib
  3. Back in Eclipse, excluded the contrib directory from the Release build configuration, and added <project-dir>/contrib to the include directories (odd, I know)
  4. Added a src directory and added a class named Foo (see below for the contents of Foo.h--I left Foo.cpp empty for now)
  5. Added a test directory in Eclipse, excluded it from the Release build configuration, added <project-dir>/contrib to the include directories, and added new source files FooTest.cpp and AllTests.cpp (see below for contents)
  6. Built and ran the project!

Foo.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();
}

Here are the detailed steps:

  1. In Eclipse, open the File menu and select New > C++ Project
  2. Project Type: Executable > Empty Project
  3. Toolchain: Linux GCC
  4. Click Finish
  5. Open a terminal and cd /tmp
  6. wget http://googletest.googlecode.com/files/gtest-1.5.0.tar.bz2
  7. cd gtest-1.5.0/
  8. ./scripts/fuse_gtest_files.py . <project-dir>/contrib
  9. Back in Eclipse, right-click on the project folder in the Project Explorer pane, then select Refresh
  10. In the Project Explorer pane, right-click on the contrib folder, select Exclude from build...*, untick only the **Release box, and click OK
  11. Right-click on the contrib folder and select Properties > C/C++ Build > Settings > Tool Settings tab > GCC C++ Compiler > Directories
  12. Click on the Add... button, then the Workspace... button, then select <project-name>/contrib and click OK to add the directory
  13. Click OK once more to accept your changes to the build settings
  14. Right-click on the project in the Project Explorer pane and select New > Folder, enter src as its name, and click OK
  15. Right-click on the src folder in the Project Explorer pane and select New > Class, name it Foo, then click OK (see above for contents of Foo.h; Foo.cpp can be left as is)
  16. Right-click on the project in the Project Explorer pane and select New > Folder, enter test as its name, and click OK
  17. Follow the steps above to add <project-name>/contrib and <project-name>/src as include directories to the test directory
  18. Right-click on the test folder, then select New > Source File to add AllTests.cpp to the test folder, then repeat the same steps to add FooTest.cpp (see above for contents)
  19. Right-click on FooTest.cpp and select Exclude from build..., click the Select All button, then OK
  20. Right-click on the project in the Project Explorer pane, and select Properties > C/C++ Build > Settings > Tool Settings tab > GCC C++ Linker > Libraries, then click the Add... button, enter pthread (required by googletest), click OK to add the library, then OK once more to accept the changes
  21. Hit Ctrl-b to build the project
  22. Hit Ctrl-F11 to run the project
  23. Victory!
送舟行 2024-10-05 06:57:44

第 1 步安装 Eclipse

如果计算机上尚未安装 Eclipse,请从 Eclipse 下载页面 (http://www.eclipse.org/downloads/)。

如果已安装 Eclipse 但仅适用于 Java,请按照以下说明下载 C++ 插件。

一个。打开 Eclipse 并单击 Help->Install New Software

在此处输入图像描述

b.在“使用:”框中,输入 http://download.eclipse.org/tools /cdt/releases/开普勒。片刻之后,名称框将会填充。选择以下组件:

  • CDT 主要功能 -> C/C++开发工具
  • CDT主要特点-> C/C++ 开发工具 SDK
  • CDT 可选功能 -> C/C++ 单元测试支持
  • CDT 可选功能 -> C/C++ 单元测试支持源码
  • CDT 可选功能 -> C/C++ Visual C++ 支持

在此处输入图像描述

c.单击“下一步”,同意这些声明,然后单击“完成”。

第 2 步下载 Cygwin

通过单击 Cygwin 安装页面上的 setup-x86_64.exe 链接来安装 Cygwin (http://www.cygwin.com/install.html)。运行后,按默认值单击“下一步”,直到进入“选择包”窗口。

在此处输入图像描述

您需要搜索并安装两个软件包:gcc 和 make。

第一个搜索词是 gcc。搜索 gcc,然后打开 Devel 文件夹。通过单击“跳过”一词标记要安装的以下软件包(然后它将更改为内部版本号,该版本号可能高于此处描述的版本号):gcc-core、gcc-g++ 和 libgcc1。

在此处输入图像描述

第二个搜索词是 make。在这里,我们只需要 Devel 包 make。

在此处输入图像描述

选择这些后,单击“下一步”进行安装。

第 3 步下载并构建 Google Test 项目

https://code.google.com/p/googletest/downloads/list,并将 zip 文件内容提取到公共目录中。所有用户都能够访问此目录非常重要。

(注意:以下命令使用 make ——使用 make 的 GoogleTest 的最后一个版本是 https://github.com/google/googletest/releases/tag/release-1.8.1。对于 GoogleTest 的未来修订版,请使用 < code>cmake。)

要构建 Google Test 项目:

  1. 打开 Cygwin(找到 Cygwin 的安装目录并双击
    在 Cygwin.bat 上)。
  2. 将当前工作目录更改为解压后的目录
    GoogleTest make 目录:cd c:/<>/gtest-1.7.0/make/
  3. 构建项目:make
  4. 创建一个存档库这
    gtest-all.o 文件: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。输入以下值并单击完成。

在此处输入图像描述

在“项目浏览”中,右键单击项目名称并选择“属性”。在 C/C++ Build 下,将 Builder 类型更改为 Internal Builder。

在此处输入图像描述

在 C/C++ Build 下,选择 Settings,然后单击 Cygwin C++ Compiler 下的 Includes 文件夹。单击顶部框中的添加按钮,然后浏览到 GoogleTest 包含文件夹。

在此处输入图像描述

最后,在 Cygwin C++ Linker 文件夹下,选择 Miscellaneous,然后单击其他对象下的 Add 图标。找到您在步骤 3 中构建的 libgtest.a 文件(它应该位于解压缩的 gtest 文件夹的 make 目录中)。

在此处输入图像描述

就是这样!现在您已准备好尝试一下。

第 6 步编写一些使用 GoogleTest 的代码

  • 通过单击“文件”->“新建”->“源文件夹”添加源文件夹。叫它
    源代码。
  • 通过右键单击 src 文件夹并选择“新建”->“头文件”来添加头文件。将此文件命名为 Counter.h。
  • 右键单击 src 文件夹并选择“新建”->“源文件”来添加源文件。称呼
    这个文件Counter.cpp。
  • 添加另一个源文件并将其命名为 Counter_Tests.cpp。

将以下代码复制并粘贴到相应的文件中:

Counter.h

class Counter { 
private: 
      int mCounter;
public:    
      Counter() : mCounter(0) {}  
      int Increment();    
};

Counter.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 下自动填写此项目名称,如果没有请单击“搜索项目”进行选择这个项目。接下来,单击“C/C++ 测试”选项卡。在 Tests Runner 下拉列表中,选择 Google Tests Runner,然后单击 Run 观看魔术!

在此处输入图像描述

下面是结果的快照。编写更多代码/测试后,您可以单击以红色突出显示的按钮来快速重新编译并重新运行所有测试。

在此处输入图像描述

Step 1 Install Eclipse

If Eclipse is not already installed on the machine, then get the latest version of the Eclipse IDE for C/C++ Developers from the Eclipse downloads page (http://www.eclipse.org/downloads/).

If Eclipse is already installed but only for Java, download the C++ plug-in following these instructions.

a. Open Eclipse and click on Help->Install New Software

enter image description here

b. In the Work with: box, type in http://download.eclipse.org/tools/cdt/releases/kepler. After a few moments, the Name box will populate. Select the following components:

  • CDT Main Features -> C/C++ Development Tools
  • CDT Main Features -> C/C++ Development Tools SDK
  • CDT Optional Features -> C/C++ Unit Testing Support
  • CDT Optional Features -> C/C++ Unit Testing Support Source
  • CDT Optional Features -> C/C++ Visual C++ Support

enter image description here

c. Click on Next, agree to the statements, and then Finish.

Step 2 Download Cygwin

Install Cygwin by clicking on the setup-x86_64.exe link on the Cygwin install page (http://www.cygwin.com/install.html). After running, click Next through the defaults until you get to the Select Packages window.

enter image description here

You will need to search for and install two packages: gcc and make.

The first search term is gcc. Search for gcc and then open the Devel folder. Mark the following packages for install by clicking on the word Skip (it will then change to the build number, which may by higher than the one depicted here): gcc-core, gcc-g++, and libgcc1.

enter image description here

The second search term is make. Here, we will only need the Devel package make.

enter image description here

Once these have been selected, click Next to install.

Step 3 Download and build Google Test project

Download the latest release of GoogleTest from https://code.google.com/p/googletest/downloads/list, and extract the zip file contents into a common directory. It is important that all users are able to access this directory.

(Note: the following commands use make -- the last revision of GoogleTest that uses make is https://github.com/google/googletest/releases/tag/release-1.8.1. For future revisions of GoogleTest use cmake instead.)

To build the Google Test project:

  1. Open Cygwin (find the install directory for Cygwin and double-click
    on Cygwin.bat).
  2. Change the current working directory to the unzipped
    GoogleTest make directory: cd c:/<<yourpath>>/gtest-1.7.0/make/
  3. Build the project: make
  4. Create an archived library out of the
    gtest-all.o file: ar -rv libgtest.a gtest-all.o

Step 4 Add the Cygwin bin directory to the computers PATH variable

Follow the instructions on this page for your version of Windows: http://www.java.com/en/download/help/path.xml, to add Cygwins bin directory to the computers PATH environment variable. (typically by adding ;C:\cygwin64\bin to the end of the current value).

Step 5 Create a new project that uses GoogleTest

Start Eclipse and select File->New->C++ Project. Enter the values below and click Finish.

enter image description here

In the Project Explore, right-click the name of the project and select Properties. Under C/C++ Build, change the Builder type to Internal Builder.

enter image description here

Under C/C++ Build, select Settings, then click on the Includes folder under Cygwin C++ Compiler. Click on the Add button in the top box and then browse to the GoogleTest include folder.

enter image description here

Lastly, under the Cygwin C++ Linker folder, select Miscellaneous and then click the Add icon under Other objects. Find the libgtest.a file that you built back in step 3 (it should be in the make directory of the unzipped gtest folder).

enter image description here

Thats it! Now you're ready to try it out.

Step 6 Write some code that uses GoogleTest

  • Add a source folder by clicking File->New->Source folder. Call it
    src.
  • Add a header file by right-clicking on the src folder and select New->Header File. Call this file Counter.h.
  • Add a source file by right-clicking on the src folder and select New->Source File. Call
    this file Counter.cpp.
  • Add another source file and call it Counter_Tests.cpp.

Copy and paste the code below into the appropriate files:

Counter.h

class Counter { 
private: 
      int mCounter;
public:    
      Counter() : mCounter(0) {}  
      int Increment();    
};

Counter.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();
}

In the Project menu select Build All. Now, to connect up the GoogleTest unit testing framework, select Run Configurations from the Run menu. From this dialog, select C/C++ Unit and click the New button.

enter image description here

It should fill in this project name automatically under C/C++ Application, if not click on Search Project to select this project. Next, click on the C/C++ Testing tab. In the Tests Runner drop-down, choose Google Tests Runner, and then click Run to watch the magic!

enter image description here

Below is a snapshot of the result. After writing more code/tests, you can click on the button highlighted in red to quickly recompile and re-run all of the tests.

enter image description here

君勿笑 2024-10-05 06:57:44

您不应将其添加到源文件夹中,而应创建单独的文件夹。这是为了避免生产代码对测试项目的依赖。这样做

../ #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
因此,您可以包含像这样的标头

test/src/module1/class1Test.h:

#include "gtest/gtest.h"
#include "module1/class1.h"

// test class1 here
// ...

test/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();
}

You should not add it to your source folder, make separate folder instead. This is for avoidance of dependency of your production code from testing project. Do it like this

../ #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
  ...

I usually use google test as two files, this is very handy. Use scripts/fuse_gtest_files.py from gtest distribution to extract them. Having only two files you can include their compilation in your test project compilation and have simple project structure.

In your test projectspecify include directories ../contrib:../src:src.
Thus you can include headers like this

test/src/module1/class1Test.h:

#include "gtest/gtest.h"
#include "module1/class1.h"

// test class1 here
// ...

test/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();
}
罪#恶を代价 2024-10-05 06:57:44

这是我针对 Eclipse 4.3 和 CDT 8.2 的解决方案
我觉得这比上面描述的要容易一些。

  1. 下载 gtest 并按照 readme.txt 中的说明进行安装(在 linux 中使用 cmake 和 make)

  2. 转到“YourProject-> Properties-> C/C++ Build-> Settings-> GCC C++ Compiler-> Includes-> Include paths”并在 gtest 中添加 include 文件夹

  3. 到“YourProject->属性” > C/C++ Build->GCC C++ Libraries”,添加 gtest 文件夹作为搜索路径并添加库“gtest”和“pthread”

”与源相同的项目从发布版本中排除测试)

  1. 转到“运行 -> 运行配置...”并创建新的 C/C++ 单元运行配置

  2. 在主选项卡中将项目设置为您的项目,将 C/C++ 应用程序设置为您的应用程序。在 C/C++ 测试选项卡中将测试运行程序设置为 Google Test Runner。

在主选项卡中将项目设置为您的项目, 在这种情况下重新索引该项目可能会有所帮助)

Here is my solution for Eclipse 4.3 and CDT 8.2
I felt this was somewhat easier then described above.

  1. Download gtest and install it as described in the readme.txt (using cmake and make in linux)

  2. Go to "YourProject-> Properties-> C/C++ Build-> Settings-> GCC C++ Compiler-> Includes-> Include paths" and add the include folder in gtest

  3. Go to "YourProject-> Properties-> C/C++ Build-> Settings-> GCC C++ Linker-> Libraries", add the gtest folder as search path and add libraries "gtest" and "pthread"

(4. If you have tests in the same project as sources exclude tests from release build)

  1. Go to "Run-> Run Configurations..." and Create a new C/C++ Unit run configuration

  2. Set project to your project and C/C++ Application to your Application in main tab. Set Tests Runner to Google Test Runner in C/C++ Testing tab.

(7. Error notifications may stick around in the eclipse gui, if this is the case re-indexing the project might help)

疯到世界奔溃 2024-10-05 06:57:44

我已经提交了您的解决方案并且运行良好。我可以说,对于编译 gtest 在 README 中不是很清楚。 TXT。

我已通过 cygwin 控制台运行 /make 目录中的 makefile。
就我而言,编译器建议我找不到 pthread 库。所以我修改了行

CXXFLAGS += -g -Wall -Wextra -pthread

并将其更改为

CXXFLAGS += -g -Wall -Wextra -lpthread

我得到的输出是gtest_main.a。然后我将此文件重命名为 libgtest.a 并将其复制到 C:\cygwin\lib 目录。

然后我将我的 eclipse 项目配置为使用 cygwin 并添加了 gtest 和 pthread 正如你所说...并且它有效!

我希望它可以帮助别人

I've tray your solution and it runs well. I can say that for compile gtest is not very clear in the README. txt.

I've run the makefile in the /make directory through a cygwin console.
In my case the compiler advise me that don't findt the pthread library. So I modified the line

CXXFLAGS += -g -Wall -Wextra -pthread

and changed it to

CXXFLAGS += -g -Wall -Wextra -lpthread

The output I get is gtest_main.a. Then I have rename this file into libgtest.a and copy it to C:\cygwin\lib directory.

Then i've configure my eclipse project to use cygwin and added gtest and pthread as you say... and it works!

I hope it can help someone

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文