20
votes

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?

3

3 Answers

14
votes

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.

3
votes

These instructions get the testing framework working for the Debug configuration.

Get Google C++ Testing Framework

1.Download the latest gtest framework

2.Unzip to C:\gtest

Build the Framework Libraries

1.Open C:\gtest\msvc\gtest.sln in Visual Studio

2.Set Configuration to "Debug"

3.Build Solution

Create and Configure Your Test Project

1.Create a new solution and choose the template Visual C++ > Win32 > Win32 Console Application

2.Right click the newly created project and choose Properties

3.Change Configuration to Debug.

4.Configuration Properties > C/C++ > General > Additional Include Directories: Add C:\gtest\include

5.Configuration Properties > C/C++ > Code Generation > Runtime Library: If your code links to a runtime DLL, choose Multi-threaded Debug DLL (/MDd). If not, choose Multi-threaded Debug (/MTd).

6.Configuration Properties > Linker > General > Additional Library Directories: Add C:\gtest\msvc\gtest\Debug

7.Configuration Properties > Linker > Input > Additional Dependencies: Add gtestd.lib

Verifying Everything Works

1.Open the cpp in your Test Project containing the main() function.

2.Paste the following code:

#include "stdafx.h"
#include <iostream>

#include "gtest/gtest.h"

    TEST(sample_test_case, sample_test)
    {
        EXPECT_EQ(1, 1);
    }

    int main(int argc, char** argv) 
    { 
        testing::InitGoogleTest(&argc, argv); 
        RUN_ALL_TESTS(); 
        std::getchar(); // keep console window open until Return keystroke
    }

1.Debug > Start Debugging

If this works you should see the console window open with your test results.

1
votes

An addendum to James C's answer:

Note that building the library using gtest-1.6.0/src/gtest-all.cc will require you to provide with a main method yourself. If you want to avoid that altogether and use the default implementation of the main method provided by Googletest, build your library including gtest_main.cc.

That is:

g++ -Igtest-1.6.0/include -Igtest-1.6.0 -c gtest-1.6.0/src/gtest-all.cc gtest-1.6.0/src/gtest_main.cc
                                                                                       ^^^^^^^^^^^^^^
ar -rv libgtest_main.a gtest_main.o gtest-all.o
                       ^^^^^^^^^^^^

Also, keep in mind that implementing your own main method is not the recommended way of defining the SetUp and TearDown behaviours; you should be using fixtures instead. Check the Googletest documentation on the topic.