git clone https://github.com/google/googletest.git
cd googletest
// 创建build目录
mkdir build
cd build
//编译安装
cmake ..
make
sudo make install
ls /usr/local/lib
// 存在以下文件则说明安装成功
libgmock.a
libgmock_main.a
libgtest.a
libgtest_main.a
# 头文件
gtest/gtest.h
# 不带 main 静态库
libgtest.a
# 带 main 静态库
libgtest_main.a
当不想写 main 函数的时候,可以直接引入 libgtest_main.a;
g++ sample.cc -o sample -lgtest -lgtest_main -lpthread
g++ sample.cc -o sample -lgmock -lgmock_main -lpthread
否则
g++ sample.cc -o sample -lgtest -lpthread
# 头文件
gmock/gmock.h
# 不带 main 静态库
libgmock.a
# 带 main 静态库
libgmock_main.a
断言成对出现,它们测试相同的东西,但对当前函数有不同的影响。 ASSERT_* 版本在失败时产生致命失败,并中止当前测试案例。 EXPECT_* 版本生成非致命失败,它不会中止当前函数。通常首选 EXPECT_* ,因为它们允许在测试中报告一个以上的失败。但是,如果在有问题的断言失败时继 续没有意义,则应该使用 ASSERT_* 。
所有断言宏都支持输出流,也就是当出现错误的时候,我们可以通过流输出更详细的信息;注意编 码问题,经流输出的信息会自动转换为 UTF-8;
EXPECT_TRUE(my_condition) << "My condition is not true";
有时候我们测试案例当中的条件太复杂,不能使用断言,那么自己写判断语句;自己返回 成功或 者失败;
SUCCEED() 或者 FAIL()
EXPECT_TRUE( condition )
ASSERT_TRUE( condition )
EXPECT_FALSE( condition )
ASSERT_FALSE( condition )
EXPECT_EQ( val1 , val2 )
ASSERT_EQ( val1 , val2 )
EXPECT_NE( val1 , val2 )
ASSERT_NE( val1 , val2 )
注意:比较空指针的时候;
使用 EXPECT_NE( ptr , nullptr) 而不是 EXPECT_NE( ptr , NULL) 。
EXPECT_LT( val1 , val2 )
ASSERT_LT( val1 , val2 )
EXPECT_LE( val1 , val2 )
ASSERT_LE( val1 , val2 )
EXPECT_GT( val1 , val2 )
ASSERT_GT( val1 , val2 )
EXPECT_GE( val1 , val2 )
ASSERT_GE( val1 , val2 )
谓词断言能比 EXPECT_TRUE 提供更详细的错误消息;
EXPECT_PRED1( pred , val1 ) \
EXPECT_PRED2( pred , val1 , val2 ) \
EXPECT_PRED3( pred , val1 , val2 , val3 ) \
EXPECT_PRED4( pred , val1 , val2 , val3 , val4 ) \
EXPECT_PRED5( pred , val1 , val2 , val3 , val4 , val5 )
ASSERT_PRED1( pred , val1 ) \
ASSERT_PRED2( pred , val1 , val2 ) \
ASSERT_PRED3( pred , val1 , val2 , val3 ) \
ASSERT_PRED4( pred , val1 , val2 , val3 , val4 ) \
ASSERT_PRED5( pred , val1 , val2 , val3 , val4 , val5 )
// Returns true if m and n have no common divisors except 1.
bool MutuallyPrime(int m, int n) { ... }
...
const int a = 3;
const int b = 4;
const int c = 10;
...
EXPECT_PRED2(MutuallyPrime, a, b); // Succeeds
EXPECT_PRED2(MutuallyPrime, b, c); // Fails
得到的错误信息:
MutuallyPrime(b, c) is false, where
b is 4
c is 10
#define TEST(test_suite_name,test_name)
用相同的数据配置来测试多个测试案例。
// 定义类型,继承自 testing::Test
class TestFixtureSmpl : public testing::Test {
protected:
void SetUp() {} // 测试夹具测试前调用的函数 -- 做初始化的工作
void TearDown() {} // 测试夹具测试后调用的函数 -- 做清理的工作
};
// 需要在 TEST_F 中书写测试用例
#define TEST_F(test_fixture,test_name)
// 如果需要复用测试夹具,只需要继承自 TestFixtureSmpl
class TestFixtureSmpl_v2 : public TestFixtureSmpl {
};
有时候相同的接口,有多个实现,下面是复用测试代码流程; 复用测试案例
using testing::Test;
using testing::Types;
// 先申明测试夹具
template
class TestFixtureSmpl : public testing::Test {
protected:
void SetUp() {} // 测试夹具测试前调用的函数 -- 做初始化的工作
void TearDown() {} // 测试夹具测试后调用的函数 -- 做清理的工作
};
// 枚举测试类型
typedef Types Implementations;
// #define TYPED_TEST_SUITE(CaseName,Types,__VA_ARGS__...)
// 注意 casename 一定要与测试夹具的名字一致
TYPED_TEST_SUITE(TestFixtureSmpl, Implementations);
// #define TYPED_TEST(CaseName,TestName)
// 开始测试, CaseName 要与 TYPED_TEST_SUITE 一致
TYPED_TEST(TestFixtureSmpl, TestName)
有时候你写了某个接口,期望其他人实现它,你可能想写一系列测试,确保其他人的实现满足你的 测试;
// 首先声明测试类型参数化(_P 是 parameterized or pattern)
// #define TYPED_TEST_SUITE_P(SuiteName)
TYPED_TEST_SUITE_P(TestFixtureSmpl);
// 书写测试, suiteName 与上面一致
// #define TYPED_TEST_P(SuiteName,TestName)
TYPED_TEST_P(TestFixtureSmpl,TestName)
// 枚举所有测试
// #define REGISTER_TYPED_TEST_SUITE_P(SuiteName,__VA_ARGS__...)
REGISTER_TYPED_TEST_SUITE_P(TestFixtureSmpl,
TestName1,TestName2,...)
// 上面定义的是抽象测试类型
// 其他人实现功能后,开始测试,假如实现了 OnTheFlyPrimeTable 和
PreCalculatedPrimeTable
typedef Types
PrimeTableImplementations;
// #define
INSTANTIATE_TYPED_TEST_SUITE_P(Prefix,SuiteName,Types,__VA_ARGS__...)
INSTANTIATE_TYPED_TEST_SUITE_P(
instance_name,
testcase,
typelist...)
可以通过 googletest 的事件机制,在测试前后进行埋点处理;
模板模式
// The interface for tracing execution of tests. The methods are organized
in
// the order the corresponding events are fired.
class TestEventListener {
public:
virtual ~TestEventListener() {}
// Fired before any test activity starts.
virtual void OnTestProgramStart(const UnitTest& unit_test) = 0;
// Fired before each iteration of tests starts. There may be more than
// one iteration if GTEST_FLAG(repeat) is set. iteration is the iteration
// index, starting from 0.
virtual void OnTestIterationStart(const UnitTest& unit_test,
int iteration) = 0;
// Fired before environment set-up for each iteration of tests starts.
virtual void OnEnvironmentsSetUpStart(const UnitTest& unit_test) = 0;
// Fired after environment set-up for each iteration of tests ends.
virtual void OnEnvironmentsSetUpEnd(const UnitTest& unit_test) = 0;
// Fired before the test suite starts.
virtual void OnTestSuiteStart(const TestSuite& /*test_suite*/) {}
// Legacy API is deprecated but still available
#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_
virtual void OnTestCaseStart(const TestCase& /*test_case*/) {}
#endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_
// Fired before the test starts.
virtual void OnTestStart(const TestInfo& test_info) = 0;
// Fired after a failed assertion or a SUCCEED() invocation.
// If you want to throw an exception from this function to skip to the
next
// TEST, it must be AssertionException defined above, or inherited from
it.
virtual void OnTestPartResult(const TestPartResult& test_part_result) = 0;
// Fired after the test ends.
virtual void OnTestEnd(const TestInfo& test_info) = 0;
// Fired after the test suite ends.
virtual void OnTestSuiteEnd(const TestSuite& /*test_suite*/) {}
// Legacy API is deprecated but still available
#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_
virtual void OnTestCaseEnd(const TestCase& /*test_case*/) {}
#endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_
// Fired before environment tear-down for each iteration of tests starts.
virtual void OnEnvironmentsTearDownStart(const UnitTest& unit_test) = 0;
// Fired after environment tear-down for each iteration of tests ends.
virtual void OnEnvironmentsTearDownEnd(const UnitTest& unit_test) = 0;
// Fired after each iteration of tests finishes.
virtual void OnTestIterationEnd(const UnitTest& unit_test,
int iteration) = 0;
// Fired after all test activities have ended.
virtual void OnTestProgramEnd(const UnitTest& unit_test) = 0;
};