GoogleTest Simple UnitTest Code


1. Install GoogleTest

  • Test Environment : Ubuntu 16.04 (Dockerized)
# apt-get update
# apt-get upgrade
# apt-get install gcc g++ make cmake libgtest-dev
# cd /usr/src/gtest
# cmake CMakeLists.txt
# make
# cp *.a /usr/lib

2. Syntax of Google Test (Simplified Manual)

Test body of Google Test

  • Basic
TEST(TestName, TestCaseName){
	... Test Assertions ...
}

Basic Assertions

Fatal assertion Nonfatal assertion Verifies
ASSERT_TRUE(condition); EXPECT_TRUE(condition); condition is true
ASSERT_FALSE(condition); EXPECT_FALSE(condition); condition is false
  • Binary Comparison
Fatal assertion Nonfatal assertion Verifies
ASSERT_EQ(val1, val2); EXPECT_EQ(val1, val2); val1 == val2
ASSERT_NE(val1, val2); EXPECT_NE(val1, val2); val1 != val2
ASSERT_LT(val1, val2); EXPECT_LT(val1, val2); val1 < val2
ASSERT_LE(val1, val2); EXPECT_LE(val1, val2); val1 <= val2
ASSERT_GT(val1, val2); EXPECT_GT(val1, val2); val1 > val2
ASSERT_GE(val1, val2); EXPECT_GE(val1, val2); val1 >= val2
  • String Comparison
Fatal assertion Nonfatal assertion Verifies
ASSERT_STREQ(str1, str2); EXPECT_STREQ(str1, str2); the two C strings have the same content
ASSERT_STRNE(str1, str2; EXPECT_STRNE(str1, str2); the two C strings have different content
ASSERT_STRCASEEQ(str1, str2); EXPECT_STRCASEEQ(str1, str2); the two C strings have the same content, ignoring case
ASSERT_STRCASENE(str1, str2); EXPECT_STRCASENE(str1, str2); the two C strings have different content, ignoring case

3. Snap Shot

  • Succeed

Succeed

  • Failed

Failed

4. cpplint.py

# python cpplint.py ./project1/priority_queue.cc
./project1/priority_queue.cc:0:  No copyright message found.  You should have a line: "Copyright [year] <Copyright Owner>"  [legal/copyright] [5]
./project1/priority_queue.cc:1:  Include the directory when naming .h files  [build/include] [4]
./project1/priority_queue.cc:4:  Missing space before {  [whitespace/braces] [5]
./project1/priority_queue.cc:8:  Missing space before {  [whitespace/braces] [5]
./project1/priority_queue.cc:12:  Missing space before {  [whitespace/braces] [5]
./project1/priority_queue.cc:17:  Missing space before {  [whitespace/braces] [5]
./project1/priority_queue.cc:22:  Missing space before {  [whitespace/braces] [5]
./project1/priority_queue.cc:27:  Missing space before {  [whitespace/braces] [5]
./project1/priority_queue.cc:32:  Missing space before {  [whitespace/braces] [5]
Done processing ./project1/priority_queue.cc
Total errors found: 9

Reference