Click here to Skip to main content
11,495,371 members (64,486 online)
Click here to Skip to main content

Tagged as

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

, 26 Aug 2014 CPOL 11.8K 20
Rate this:
Please Sign up or sign in to vote.
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.

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.

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”,

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

And then click “next”

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.

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.

 

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

2. adding source code.

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

//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”.

//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”

//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”

// 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”

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.

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.

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.

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)

Share

About the Author

Yu222

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

Comments and Discussions

 
QuestionImage rendering Pin
Rage28-Aug-14 6:36
professionalRage28-Aug-14 6:36 
AnswerRe: Image rendering Pin
Yu22228-Aug-14 7:09
memberYu22228-Aug-14 7:09 
AnswerRe: Image rendering Pin
Christian Amado12-Sep-14 4:29
professionalChristian Amado12-Sep-14 4:29 
GeneralHow to verify a range of data? Pin
Paul M Watt28-Aug-14 5:41
mentorPaul M Watt28-Aug-14 5:41 
GeneralRe: How to verify a range of data? Pin
Yu22228-Aug-14 7:21
memberYu22228-Aug-14 7:21 
SuggestionAlternative setup for Google Test (gtest) [modified] Pin
DiegoRL26-Aug-14 23:08
memberDiegoRL26-Aug-14 23:08 
GeneralRe: Alternative setup for Google Test (gtest) Pin
Nelek27-Aug-14 15:08
memberNelek27-Aug-14 15:08 
GeneralRe: Alternative setup for Google Test (gtest) Pin
Yu22227-Aug-14 21:41
memberYu22227-Aug-14 21:41 
GeneralRe: Alternative setup for Google Test (gtest) [modified] Pin
Nelek28-Aug-14 1:33
memberNelek28-Aug-14 1:33 
GeneralRe: Alternative setup for Google Test (gtest) Pin
DiegoRL28-Aug-14 0:13
memberDiegoRL28-Aug-14 0:13 
GeneralRe: Alternative setup for Google Test (gtest) Pin
Nelek28-Aug-14 1:37
memberNelek28-Aug-14 1:37 
GeneralRe: Alternative setup for Google Test (gtest) Pin
DiegoRL28-Aug-14 3:07
memberDiegoRL28-Aug-14 3:07 
GeneralRe: Alternative setup for Google Test (gtest) Pin
Nelek28-Aug-14 6:49
memberNelek28-Aug-14 6:49 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150520.1 | Last Updated 27 Aug 2014
Article Copyright 2014 by Yu222
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid