Professional Documents
Culture Documents
Framework
Abner Huang
July. 2015
Test!? We do it every day
void Test__calc_isect() {
int x1, y1, x2, y2;
int x3, y3, x4, y4;
int x, y;
x1 = 0; y1 = INT_MIN; x2 = 2; y2 = INT_MAX;
x3 = 20; y3 = INT_MAX; x4 = 22; y4 = INT_MIN;
calc_isect(&x, &y, x1, y1, x2, y2, x3, y3, x4, y4, 0);
assert(x == 11);
assert(y == INT_MAX);
}
void main(void)
{
Test__calc_isect1();
Test__calc_isect2();
Test__calc_isect3();
Test__calc_isect();
Test__calc_isect4();
Test__calc_isect5();
Test__calc_isect6();
}
> ./test.out
test.cpp:100:Test__calc_isect(): Assertion `x == 11' failed.
void Test__calc_isect() {
int x1, y1, x2, y2;
int x3, y3, x4, y4;
int x, y;
x1 = 0; y1 = INT_MIN; x2 = 2; y2 = INT_MAX;
x3 = 20; y3 = INT_MAX; x4 = 22; y4 = INT_MIN;
calc_isect(&x, &y, x1, y1, x2, y2, x3, y3, x4, y4, 0);
… …
}
TEST(GEODeathTest, CalcIsect) {
ASSERT_DEATH(CalcIsect(NULL, NULL, 0,0,0,0,0,0,0,0,0),
".*invalid");
}
Adding Traces to Assertions
path/to/foo_test.cc:11: Failure
void Sub1(int n) {
Value of: Bar(n)
EXPECT_EQ(1, Bar(n));
Expected: 1
EXPECT_EQ(2, Bar(n + 1)); Actual: 2
} Trace:
path/to/foo_test.cc:17: A
TEST(FooTest, Bar) {
path/to/foo_test.cc:12: Failure
{ Value of: Bar(n + 1)
SCOPED_TRACE("A"); Expected: 2
Sub1(1);
}
Sub1(9);
}
XML outputs
<testsuites name="AllTests" tests="3" failures="
1" errors="0" time="35" ...>
<testsuite name="test_case_name" tests="2"
failures="1" errors="0" time="15"...>
<testcase name="test_name" status="run"
time="7"...>
<failure message="..."/>
<failure message="..."/>
</testcase>
</testsuite>
</testsuites>
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();
}
::testing::InitGoogleTest(&argc, argv);
int result = RUN_ALL_TESTS(); Initialize gtest and runs all tests.
return result;
}
Test types of Gtest
• TEST() SetUp()
• TEST_F() -- Fixture
TearDown() TEST/TEST_F/TEST_P
x1 = 0; y1 = INT_MIN; x2 = 2; y2 = INT_MAX;
x3 = 20; y3 = INT_MAX; x4 = 22; y4 = INT_MIN;
calc_isect(&x, &y, x1, y1, x2, y2, x3, y3, x4, y4, 0);
assert(x == 11);
assert(y == INT_MAX);
}
Test Fixtures
TEST_F(GEO, CalcIsect) {
x1 = 0; y1 = INT_MIN; x2 = 2; y2 = INT_MAX;
x3 = 20; y3 = INT_MAX; x4 = 22; y4 = INT_MIN;
calc_isect(&x, &y, x1, y1, x2, y2, x3, y3, x4, y4, 0);
assert(x == 11);
assert(y == INT_MAX);
}
TEST_P() -- Value-parameterized fixture
Range(begin, end[, Yields values {begin, begin+step, begin+step+step, ...}. The values do
step]) not include end. step defaults to 1.
Values(v1, v2, ..., vN) Yields values {v1, v2, ..., vN}.
Combine(g1, g2, ..., Yields all combinations (the Cartesian product for the math savvy) of
gN) the values generated by the N generators. This is only available if your
system provides the <tr1/tuple> header.
class GEO : public ::testing::TestWithParam<int> {
protected:
virtual void SetUp() {}
void ZapMemoryPool() {}
virtual void TearDown() {}
int x1, y1, x2, y2, x3, y3, x4, y4, x, y;
};
TEST_P(GEO, CalcIsect) {
double angle = PI/180 * 15 * GetParam();
x1 = -100; y1 = 0; x2 = 100; y2 = 0;
x3 = -50; y3 = 0; x4 = 50; y4 = 0;
rotate(&x3, &y4, angle);
rotate(&x4, &y4, angle);
calc_isect(&x, &y, x1, y1, x2, y2, x3, y3, x4, y4, 0);
assert(x == 0 && y == 0);
}
[----------] 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)
Organization of Tests
Environment::
SetUpTestCase()
SetUp()
SetUp()
TearDown() TEST/TEST_F/TEST_P
Environment::
TearDownTestCase()
TearDown()
How to install gtest
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/
How to use google test
• E.g.
/path/to/foo/
– exec
– gtest
– include
– lib
– bar <<< Library under test, contains production code
– published
– test <<< Directory contains test code
test> make
Some points to keep in mind