It’s always good to unit test your C++ code so that you can find problems early. In this tutorial, I will demonstrate how you can run unit testing by using of Google C++ Testing Framework using the Microsoft Visual Studio. GTest is easy to learn and master. Visit the following website to find out more about GTest Google C++ Testing Framework
Note: I am using VS2010 C++ Express Edition in this tutorial.
Compile the GTest framework
- Download GTest framework from http://code.google.com/p/googletest/downloads/list. You will find the ZIP file listed in the download page. The version that i am using in this tutorial is gtest-1.6.0.
- Unzip the file to C:\gtest-1.6.0\
- Browse to msvc\ folder. All files are set to read-only by default. Uncheck read-only attribute in the properties setting for all files so that it allow to write.
- Open the gtest-md.sln file using visual studio.
- Compile the solution in release mode.
Note: Debug build will generate gtestd.lib; Release build will generate gtest.lib - Create a lib folder in C:\gtest-1.6.0\
- Copy gtest.lib from msvc\gtest-md\Release\ to C:\gtest-1.6.0\lib\
Unit Test with GTest in the C++ Example
- Create a new C++ win32 console application. I called this project GTestSample.
- Right click the GTestSample project and select properties.
- Enter the Gtest include folder path to C/C++::General::Additional Include Directories field.
- Enter the Gtest lib folder path to Linker::General::Additional Library Directories field.
- We now start the coding part by insert the following header to the cpp file.
1#include <gtest/gtest.h>
- Let’s write the first function and its test cases. It’s a multiply function that each given number will return by multiply of 3.
1
2
3
4
5
6
7
8
9
10
11
12int MultiplyThree(int num)
{
return num * 3;
}
TEST(MultiplyThreeTest, InputNumber) {
EXPECT_EQ(6, MultiplyThree(2)); // Expect equal to 6
EXPECT_EQ(9, MultiplyThree(3)); // Expect equal to 9
EXPECT_EQ(12, MultiplyThree(4)); // Expect equal to 12
EXPECT_NE(20, MultiplyThree(5)); // Expect not equal to 20
EXPECT_NE(25, MultiplyThree(6)); // Expect not equal to 25
} - Our next function and its test cases will compare the char string.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26int CompareChar(char* ch1, char* ch2)
{
if (ch1 == NULL || ch2 == NULL)
return 88;
return strcmp(ch1, ch2);
}
TEST(CompareCharTest, InputChar) {
// Expect equal
EXPECT_EQ(0, CompareChar("hello", "hello"));
// Expect not equal
EXPECT_NE(0, CompareChar("hello", "world"));
// Expect greater than. "world" is grater than "hello"
EXPECT_GT(0, CompareChar("hello", "world"));
// Expect less than. "hello" is less than "world"
EXPECT_LT(0, CompareChar("world", "hello"));
// Expect equal.
// Either char string appear to be NULL, return 88
EXPECT_EQ(88, CompareChar(NULL, "world"));
} - Let’s create a class now and test it with GTest. A sample class that compare its value.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24class SampleClass
{
public:
SampleClass(int value)
{
m_value = value;
}
int CompareValue(int val)
{
return (m_value == val);
}
private:
int m_value;
};
TEST(SampleClassTest, InputNumber) {
SampleClass sample(10);
EXPECT_EQ(1, sample.CompareValue(10)); // Expect equal
EXPECT_EQ(0, sample.CompareValue(8)); // Expect equal
EXPECT_NE(1, sample.CompareValue(8)); // Expect not equal
} - Let’s make sure that we run all the test cases in the main function.
1
2
3
4
5
6
7int _tmain(int argc, _TCHAR* argv[])
{
::testing::InitGoogleTest(&argc, argv);
int i = RUN_ALL_TESTS();
getchar();
return i;
}
Note: It’s important that you need to make sure that you build with the correct GTest library. In this tutorial, our GTest library was built in release mode, so we will compile the C++ example in release mode too.
Compile and run the C++ program now and you should observe the following output:
Download source code: GTestSample.zip
You missed a step in the configuration of the VS Project (and I spent a long time finding it):
1) Add gtest.lib to Linker -> Input -> Additional Dependencies
Anyway, your post was quite helpful