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?
Using Riga's excellent answer, here is a summary of how I got it to work:
./scripts/fuse_gtest_files.py . <project-dir>/contrib
contrib
directory from the Release build configuration, and added <project-dir>/contrib
to the include directories (odd, I know)src
directory and added a class named Foo
(see below for the contents of Foo.h
--I left Foo.cpp
empty for now)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)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:
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
folder, select Exclude from build...*, untick only the **Release box, and click OKcontrib
folder and select Properties > C/C++ Build > Settings > Tool Settings tab > GCC C++ Compiler > Directories<project-name>/contrib
and click OK to add the directorysrc
as its name, and click OKsrc
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)test
as its name, and click OK<project-name>/contrib
and <project-name>/src
as include directories to the test
directorytest
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)FooTest.cpp
and select Exclude from build..., click the Select All button, then OK