Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to setup googletest on Linux in the year 2012?

Tags:

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?

like image 622
TheOneTeam Avatar asked Jan 05 '12 08:01

TheOneTeam


People also ask

How do I check my version of Google?

Installation Queries: ... --version the version of the Google Test installation Version Queries: --min-version=VERSION return 0 if the version is at least VERSION --exact-version=VERSION return 0 if the version is exactly VERSION --max-version=VERSION return 0 if the version is at most VERSION ...

How do I install Google Windows test?

Add a Google Test project in Visual Studio 2022In Solution Explorer, right-click on the solution node and choose Add > New Project. Set Language to C++ and type test in the search box. From the results list, choose Google Test Project. Give the test project a name and choose OK.

What is Gtest?

Google Test (also known as gtest) is a unit testing library for the C++ programming language, based on the xUnit architecture. The library is released under the BSD 3-clause license.


2 Answers

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.

like image 122
James C Avatar answered Oct 06 '22 08:10

James C


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.

like image 21
Darren Burgess Avatar answered Oct 06 '22 07:10

Darren Burgess