How to setup googletest on Linux in the year 2012?

Kit Ho picture Kit Ho · Jan 5, 2012 · Viewed 17.8k times · Source

I am using Linux machine. I have download the googletest package from here

However, there is no installation guide or other blogs related on how to set it up properly The README file is no good that I can't understand what it is talking about?

Can anyone provide a simple example on how to test a simple function inside a .cc file with that gtest package?

Answer

James C picture James C · Dec 20, 2012

Here's what I did and you can adjust as necessary. I downloaded gtest-1.6.0.zip (from the releases page) on my Linux box into ~/Downloads which typed out fully is /home/me/Downloads/

Unzip the contents of gtest-1.6.0.zip into ~/Downloads/gtest-1.6.0/

cd /home/me/Downloads
unzip gtest-1.6.0.zip

Build the gtest library because it's something you need to "include" in your test executable. Compile the object file gtest-all.o:

g++ -Igtest-1.6.0/include -Igtest-1.6.0 -c gtest-1.6.0/src/gtest-all.cc

Then build the library archive libgtest.a:

ar -rv libgtest.a gtest-all.o

Now you can create your test.cc file in ~/Downloads. Here is an example test file that I used to make sure it compiles.

#include "gtest/gtest.h"

TEST(blahTest, blah1) {
    EXPECT_EQ(1, 1);
}

int main (int argc, char** argv) {
    ::testing::InitGoogleTest(&argc, argv);

    int returnValue;

    //Do whatever setup here you will need for your tests here
    //
    //

    returnValue =  RUN_ALL_TESTS();

    //Do Your teardown here if required
    //
    //

    return returnValue;
}

To compile your own test and run it:

g++ -I/home/me/Downloads/gtest-1.6.0/include -pthread test.cc libgtest.a -o test_executable

Then to execute it:

./test_executable

And it should run fine. Modify as necessary from there.