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

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:
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.

No comments:

Post a Comment

Genuine websites to earn money.

If you are interested in PTC sites then this article is for you. I have personally tried many of the sites and found that the best thing ...