Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using googletest in eclipse: how?

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?

like image 553
user478310 Avatar asked Oct 17 '10 02:10

user478310


People also ask

Can Google Test be used for C?

I have used googletest extensively in the past to unit test C code and can recommend it. Show activity on this post. As all Google's C++ code, Google Test does not use exceptions, so exception safety flow won't be an issue.


2 Answers

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!
like image 70
Josh Glover Avatar answered Sep 29 '22 19:09

Josh Glover


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

like image 36
rundavidrun Avatar answered Sep 29 '22 19:09

rundavidrun