This document provides an introduction to using the Google Test framework for unit testing C++ code. It begins with an example of a simple test for a function called calc_isect. It then demonstrates how to add assertions to tests, use test fixtures to reduce duplicated setup code, and generate parameterized tests. The document also covers best practices for test organization, installing and using Google Test, and some key features like XML output and selecting subsets of tests. Overall, the document serves as a tutorial for getting started with the Google Test framework for writing and running unit tests in C++ projects.
16. ● Fatal and non-fatal assertions
○ EXPECT_EQ(x, 10);EXPECT_FLOAT_EQ(x,10.0)
○ ASSERT_EQ(x, 10);ASSERT_DOUBLE_EQ(x,10.0)
● Easy assertions informative messages:
assert(x == 10);
EXPECT_EQ(x, 10) << “vector (” << old_x <<
”,” << old_y << ”) moved to (” << x << ”,” <<
y << ”)n”;
17. > ./test.out
[ RUN ] MYTEST.TestFoo
test.cpp:37: Failure
Value of: x
Actual: 11
Expected: 10
vector (10, 77) move to (11, 77)
[ FAILED ] MYTEST.TestFoo (0 ms)
18. DEATH TEST
calc_isect(int *x, int *y, int x1, int y1, int x2, int y2,
int x3, int y3, int x4, int y4, int tolerance)
{
if (x==NULL || y==NULL)
{
cerr << “Input is invalid.n”;
exit(1);
}
… …
}
TEST(GEODeathTest, CalcIsect) {
ASSERT_DEATH(CalcIsect(NULL, NULL, 0,0,0,0,0,0,0,0,0),
".*invalid");
}
21. Prepare your test case
• test.cpp contains main() in test directory
#include "_test_util.h"
static void
infrastructure_init(void)
{
vmemUseMalloc(1);
vmemInit_m();
vmemSetIncrementValue(VMEM_MEGABYTE_MINUS);
hoVMsetDefaultPoolLockingState(0);
vth_init();
}
int main(int argc, char **argv) {
infrastructure_init(); // Write a function like this to initialize vmem, threading etc.
::testing::InitGoogleTest(&argc, argv);
int result = RUN_ALL_TESTS(); Initialize gtest and runs all tests.
return result;
}
24. Test Fixtures
• Sometimes you want to run several tests on similar data
structures (same setup before running test).
• In a fixture you define common setup/tear-down for
multiple tests
• Reduces amount of test code by avoiding repetition
26. TEST_P() -- Value-parameterized fixture
● generator based tests.
Range(begin, end[,
step])
Yields values {begin, begin+step, begin+step+step, ...}. The values do
not include end. step defaults to 1.
Values(v1, v2, ..., vN) Yields values {v1, v2, ..., vN}.
ValuesIn(container) and
ValuesIn(begin, end)
Yields values from a C-style array, an STL-style container, or an
iterator range [begin, end). container, begin, and end can be
expressions whose values are determined at run time.
Bool() Yields sequence {false, true}.
Combine(g1, g2, ...,
gN)
Yields all combinations (the Cartesian product for the math savvy) of
the values generated by the N generators. This is only available if your
system provides the <tr1/tuple> header.
28. [----------] 16 tests from AUTO_GEN_OH/GEO
[ RUN ] AUTO_GEN_OH/GEO.CalcIsect/1
[ OK ] AUTO_GEN_OH/GEO.CalcIsect/1 (0 ms)
[ RUN ] AUTO_GEN_OH/GEO.CalcIsect/2
[ OK ] AUTO_GEN_OH/GEO.CalcIsect/2 (0 ms)
[ RUN ] AUTO_GEN_OH/GEO.CalcIsect/3
[ OK ] AUTO_GEN_OH/GEO.CalcIsect/3 (0 ms)
[ RUN ] AUTO_GEN_OH/GEO.CalcIsect/4
[ OK ] AUTO_GEN_OH/GEO.CalcIsect/4 (0 ms)
[ RUN ] AUTO_GEN_OH/GEO.CalcIsect/5
[ OK ] AUTO_GEN_OH/GEO.CalcIsect/5 (0 ms)
[ RUN ] AUTO_GEN_OH/GEO.CalcIsect/6
[ OK ] AUTO_GEN_OH/GEO.CalcIsect/6 (0 ms)
[ RUN ] AUTO_GEN_OH/GEO.CalcIsect/7
[ OK ] AUTO_GEN_OH/GEO.CalcIsect/7 (0 ms)
[ RUN ] AUTO_GEN_OH/GEO.CalcIsect/8
[ OK ] AUTO_GEN_OH/GEO.CalcIsect/8 (0 ms)
[ RUN ] AUTO_GEN_OH/GEO.CalcIsect/9
[ OK ] AUTO_GEN_OH/GEO.CalcIsect/9 (0 ms)
[ RUN ] AUTO_GEN_OH/GEO.CalcIsect/10
[ OK ] AUTO_GEN_OH/GEO.CalcIsect/10 (0 ms)
[ RUN ] AUTO_GEN_OH/GEO.CalcIsect/11
[ OK ] AUTO_GEN_OH/GEO.CalcIsect/11 (0 ms)
[----------] 11 tests from AUTO_GEN_OH/GEO (0 ms total)
32. Easy Install
> wget http://googletest.googlecode.com/files/gtest-1.7.0.zip
> unzip gtest-1.7.0.zip
> cd gtest-1.7.0
> ./configure
> make
> cp -a include/gtest /usr/include
> sudo cp -a lib/.libs/* /usr/lib/
33. How to use google test
• You must check out gtest to use unit test feature. We do
not have the executable of gtest.
• E.g.
– /path/to/foo/
– build
– arch
– src
– exec
– gtest
– include
– lib
34. How to use google test (II)
• E.g.
/path/to/foo/
– exec
– gtest
– include
– lib
– bar <<< Library under test, contains production code
– published
– test <<< Directory contains test code
test> make
35. Some points to keep in mind
• gtest is useful for testing primitive functions in libraries
• Unit testing does not replace regression testing
• With gtest:
– You do not modify production code.
– You add test code that calls library functions.
– You check-in test code to repository
• Production code is not compiled/linked with gtest
• Using gtest creates a test binary, not icv binary
36. Coding Style and Rules
• Don’t mix TEST(), TEST_F(), TEST_P()
• Don’t use global variables, use test fixture to instead
• Gtest does not reuse the same test fixture object across
multiple tests
• Spell SetUp() correctly.
• Gtest use fork() to do death test, do not try not to free
memory in a death test.
• You can derive new class from your own class.
37. Coding Style and Rules
• Use Camel Case style to name test cases and suite
– E.g., TEST(Time, Flies_Like_An_Arrow) { ... }
and
TEST(Time_Flies, Like_An_Arrow) { … }
will have the same name
Time_Files_Like_An_Arrow_Test
• Current Naming Rule
– Test suite name is all uppercase
– Test case name is Camel Case style with underscore.
– E.g., TEST_F(SPACING_UTIL__PNT_EDGE, WhichHalfRes_SamePoints)
38. Key Features & Characters Boost Test Google Test UnitTest++
Free (Open Source) software Yes Yes Yes
Simple to use Yes
(5 out of 5)
Yes
(5 out of 5)
Yes
(4 out of 5)
Test cases grouping & organization Good Good Normal
Test case template support Yes Yes Yes
Test fixture (i.e. fixed state used as
a baseline for running test) support
Yes Yes Yes
Global test fixture Yes Yes No
Handle C++ exceptions Yes Yes Yes
Different types of assertions support Yes Yes No
Fatal and non-fatal assertions Yes Yes No
Support Regular Expression when
matching function output
Yes Yes No
Output in XML format Yes Yes No
39. Key Features & Characters Boost Test Google Test UnitTest++
Support Mock object (objects can
simulate the behavior of complex
objects)
No Yes
(work seamlessly with
Google Mock)
No
Extend features with event
mechanism
No Yes No
Run tests on distributed machines No Yes No
Thread safety when running tests No Yes No
Run subset of all defined test cases Yes
(only support
filtering by test
name)
Yes
(support multiple
filtering conditions)
No
• Karl Chen, C++ Unit Testing Frameworks Evaluation