Click here to Skip to main content
15,885,546 members
Articles / Programming Languages / C++

C++ unit test start guide, how to set up Google Test (gtest) in Eclipse?

Rate me:
Please Sign up or sign in to vote.
4.52/5 (10 votes)
26 Aug 2014CPOL3 min read 109.1K   24   13
Running Google Test in Eclipse

Introduction

Software testing is a large and complex subject. Unit testing is the testing of an individual class in isolation from other classes. The goal of unit testing is to isolate each part of the program and show that the individual parts are correct.

Background

Googletest is one of the most popular C++ unit test frameworks. It works on a variety of platforms (Linux, Mac OS X, Windows, Cygwin, Windows CE, and Symbian). Based on the xUnit architecture. Supports automatic test discovery, a rich set of assertions, user-defined assertions, death tests, fatal and non-fatal failures, value- and type-parameterized tests, various options for running the tests, and XML test report generation.

In this article, I will give a step-by-step description on how to set up Googletest in Eclipse Juno.

Setup Procedures

Step 1: Download googletest.

Download googletest from:

https://code.google.com/p/googletest/downloads/list

The current version is 1.7.0.

Image 1

Step 2: Extract gtest-1.7.0.

Extract gtest-1.7.0, and open the folder gtest-1.7.0. The folder contains several files. “samples” gives ten examples of googletest; “src” has the source code.

Image 2

Step 3: Setting up working environment for googletest in Eclipse

Step 3.1 Create unit test project

Open eclipse, go to File->New->C++ Project

Type the name of your project in “Project name:” such as “unit_test”,

Image 3

and then click next, select “Debug” and “Release” in Select Configurations

Image 4

And then click “next”

Image 5

And click Finish, unit_test will appear in the left tab of “Project Explorer”.

Step 3.2 adding googletest library, source and test folder.

1. unt_test-> New -> Folder, create source code (code to be tested) folder “src”.

2. unt_test-> New -> Folder, create header file folder “include”.

3. unt_test-> New -> Folder, create folder “gtest_src” to store the gtest library.

4. unt_test-> New -> Folder, create folder “test” to store the test code.

Image 6

Step 3.3 adding googletest library, source and test code.

1. adding Googletest library to folder gtest_src.

The next thing we want to do is add google test library to the gtest_src folder by running the following command. This step basically copy gtest.h and gtest_all.cc to the folder of gtest_src/gtest.

Image 7

 

After running the above command in the terminal, the snapshot looks like this.

Image 8

2. adding source code.

Folder include->New-> Header File, and create header file “factorial.h”. The code is shown in the Figure.

Image 9

C++
//factorial.h
#ifndef FACTORIAL_H_
#define FACTORIAL_H_
int factorial(int n);
#endif /* FACTORIAL_H_ */ 

Next, folder src->New-> Source File, enter the source code file name, such as “factorial.cpp”.

Image 10

C++
//factorial.cpp       
int factorial(int n) {
        int result = 1;
        for (int i = 1; i <= n; i++) {
                result *= i;
         }
        return result;
}
3. Adding test code.

Folder test_code->New-> Source File, enter the test code file name,

Test code: “test_factorial.cpp”

Image 11

//test_factorial.cpp

# include "gtest/gtest.h"
# include "factorial.h"

TEST(IntegerFunctionTest, negative) {
        EXPECT_EQ(1, factorial(-5));
        EXPECT_EQ(1, factorial(-1));
        EXPECT_GT(factorial(-10), 0);
}

TEST(IntegerFunctionTest, DISABLED_zero) {
         EXPECT_EQ(1, factorial(0));
}

TEST(IntegerFunctionTest, postive) {
         EXPECT_EQ(1, factorial(1));
         EXPECT_EQ(2, factorial(2));
         EXPECT_EQ(6, factorial(3));
         EXPECT_EQ(40320, factorial(8));
}    

And “gtest_main.cpp”

Image 12

// gtest_main.cpp
#include <stdio.h>
#include "gtest/gtest.h"

GTEST_API_ int main(int argc, char **argv) {
  printf("Running main() from gtest_main.cc\n");
  testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}

Step 4: Setup the running environment

1. Project unit_test->properties -> C++ linker -> Libraries -> Libraries(-I) -> enter “pthread” and click “ok”

Image 13

2. folder test-> Properties -> C/C++ Build -> Setting -> Cross G++ Compiler -> includes -> Include paths (-I)

And then click “add..” -> “workspace..”, add folder “include” and “gtest_src” to the paths.

Image 14

3. folder gtest_src-> Properties -> C/C++ Build -> Setting -> Cross G++ Compiler -> includes -> Include paths (-I). And then click “add..” -> “workspace..”, add folder “gtest_src” to the path.

Image 15

After this, we have set up the running environment, and we can start unit test.

Step 5: running Google Test.

1. In eclipse, go to Project -> Build All or ctrl+B to build the project.

2. go to Run-> Run or ctrl+F11 to run the executable file.

Alternative, we can go to the terminal, go to the Debug folder and run the executable file unit_test. '

The output is shown below.

Image 16

History

1.0 Version, Aug. 26 2014.

1.1 Version, Sept. 9 2014. Change the method of import the gtest library

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
Software Developer in Java, C/C++.

Comments and Discussions

 
QuestionImage rendering Pin
Rage28-Aug-14 5:36
professionalRage28-Aug-14 5:36 
AnswerRe: Image rendering Pin
Yu22228-Aug-14 6:09
Yu22228-Aug-14 6:09 
AnswerRe: Image rendering Pin
Christian Amado12-Sep-14 3:29
professionalChristian Amado12-Sep-14 3:29 
GeneralHow to verify a range of data? Pin
Paul M Watt28-Aug-14 4:41
mentorPaul M Watt28-Aug-14 4:41 
GeneralRe: How to verify a range of data? Pin
Yu22228-Aug-14 6:21
Yu22228-Aug-14 6:21 
SuggestionAlternative setup for Google Test (gtest) Pin
DiegoRL26-Aug-14 22:08
DiegoRL26-Aug-14 22:08 
GeneralRe: Alternative setup for Google Test (gtest) Pin
Nelek27-Aug-14 14:08
protectorNelek27-Aug-14 14:08 
GeneralRe: Alternative setup for Google Test (gtest) Pin
Yu22227-Aug-14 20:41
Yu22227-Aug-14 20:41 
GeneralRe: Alternative setup for Google Test (gtest) Pin
Nelek28-Aug-14 0:33
protectorNelek28-Aug-14 0:33 
GeneralRe: Alternative setup for Google Test (gtest) Pin
DiegoRL27-Aug-14 23:13
DiegoRL27-Aug-14 23:13 
GeneralRe: Alternative setup for Google Test (gtest) Pin
Nelek28-Aug-14 0:37
protectorNelek28-Aug-14 0:37 
GeneralRe: Alternative setup for Google Test (gtest) Pin
DiegoRL28-Aug-14 2:07
DiegoRL28-Aug-14 2:07 
GeneralRe: Alternative setup for Google Test (gtest) Pin
Nelek28-Aug-14 5:49
protectorNelek28-Aug-14 5:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.