googletest 笔记

简介: googletest 笔记

什么是一个好的测试

1 测试应该是独立的和可重复的。调试一个由于其他测试而成功或 失败的测试是一件痛苦的事情。googletest 通过在不同的对象上 运行测试来隔离测试。当测试失败时,googletest 允许您单独运 行它以快速调试。

2 测试应该很好地“组织”,并反映出测试代码的结构。googletest 将相关测试分组到可以共享数据和子例程的测试套件中。这种通 用模式很容易识别,并使测试易于维护。当人们切换项目并开始 在新的代码库上工作时,这种一致性尤其有用。 3 测试应该是可移植的和可重用的。谷歌有许多与平台无关的代 码;它的测试也应该是平台中立的。googletest 可以在不同的操 作系统上工作,使用不同的编译器,所以 googletest 测试可以在 多种配置下工作。

4 当测试失败时,他们应该提供尽可能多的关于问题的信息。 googletest 不会在第一次测试失败时停止。相反,它只停止当前 的测试并继续下一个测试。还可以设置报告非致命失败的测试, 在此之后当前测试将继续进行。因此,您可以在一个运行-编辑编译周期中检测和修复多个错误。

5 测试框架应该将测试编写者从日常琐事中解放出来,让他们专注 于测试“内容”。googletest 自动跟踪所有定义的测试,并且不要 求用户为了运行它们而枚举它们

6 测试应该是快速的。使用 googletest,您可以在测试之间重用共 享资源,并且只需要为设置/拆除支付一次费用,而无需使测试 彼此依赖。

测试层次关系

环境准备

下载

git clone https://github.com/google/googletest.git # 或者 wget https://github.com/google/googletest/releases/tag/ release-1.11.0

安装

cd googletest cmake CMakeLists.txt make sudo make install

重要文件

googletest# 头文件 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 -lgtest -lgmock - lgmock_main -lpthread 否则 g++ sample.cc -o sample -lgtest -lpthread g++ sample.cc -o sample -lgtest -lgmock - lpthreadgooglemock # 头文件 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 )

二元比较

val1 = val2:

EXPECT_EQ( val1 , val2 )

ASSERT_EQ( val1 , val2 )

val1 != val2:

EXPECT_NE( val1 , val2 )

ASSERT_NE( val1 , val2 )

注意:比较空指针的时候;

使用 EXPECT_NE( ptr , nullptr) 而不是 EXPECT_NE( ptr , NULL)。

val1 < val2:

EXPECT_LT( val1 , val2 )

ASSERT_LT( val1 , val2 )

val1 <= val2: EXPECT_LE( val1 , val2 ) ASSERT_LE( val1 , val2 ) val1 > val2: EXPECT_GT( val1 , val2 ) ASSERT_GT( val1 , val2 ) 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

googletest samples 层次

函数测试以及类测试

#define TEST(test_suite_name,test_name)

test fixture(测试夹具)

用相同的数据配置来测试多个测试案例。

// 定义类型,继承自 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 T>
class TestFixtureSmpl : public testing::Test {
protected:
 void SetUp() {} // 测试夹具测试前调用的函数 -- 做初
始化的工作
    void TearDown() {} // 测试夹具测试后调用的函数 --
做清理的工作
};
// 枚举测试类型
typedef Types<Class1, Class2, class3, ...>
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<OnTheFlyPrimeTable,
PreCalculatedPrimeTable>
    PrimeTableImplementations;
// #define
INSTANTIATE_TYPED_TEST_SUITE_P(Prefix,SuiteName,T
ypes,__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 eachiteration of tests starts.
  virtual void OnEnvironmentsSetUpStart(const UnitTest& unit_test) = 0;
  // Fired after environment set-up for eachiteration 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;
};

内存泄露

怎么产生?1. 忘记释放了;2. 因为逻辑bug,跳过了释放流 程; new 是 c++ 中的操作符; 1. 调用 operator new 分配内存; 2. 调用构造函数在步骤 1 返回的内存地址生成类对象; 可以通过重载 new 来修改 1 的功能; delete 与 new 类似;只是是先调用析构函数,再释放内存;
// 重载操作符 new 和 delete,接着用类的静态成员来统计调
用 new 和 delete的次数
class CLeakMem {
public:
  // ...
  void* operator new(size_t allocation_size) {
    allocated_++;
    return malloc(allocation_size);
 }
  void operator delete(void* block, size_t /*
allocation_size */) {
    allocated_--;
    free(block);
 }
private:
  static int allocated_;
};
int CLeakMem::allocated_ = 0;
检测
class LeakChecker : public EmptyTestEventListener
{
private:
  // Called before a test starts.
  void OnTestStart(const TestInfo& /* test_info*/) override {
    initially_allocated_ = CLeakMem::allocated();
 }
  // Called after a test ends.
  void OnTestEnd(const TestInfo& /* test_info */) override {
    int difference = CLeakMem::allocated() - initially_allocated_;
    // You can generate a failure in any event handler except
    // OnTestPartResult. Just use an appropriate Google Test assertion to do
    // it.
    EXPECT_LE(difference, 0) << "Leaked " << difference << " unit(s) of class!";
  }
  int initially_allocated_;
};
目录
相关文章
|
8天前
|
测试技术
使用CLion创建Cmake项目,使用GoogleTest和GoogleMock对代码进行测试
使用CLion创建Cmake项目,使用GoogleTest和GoogleMock对代码进行测试
21 3
|
10月前
|
测试技术
[googletest] --- 简易使用教程
[googletest] --- 简易使用教程
271 0
|
6月前
|
并行计算 关系型数据库 MySQL
Cmake 使用备忘
Cmake 使用备忘
|
7月前
|
算法 Linux C++
[笔记]CMake学习
[笔记]CMake学习
又编译了一次doubango整个项目
又编译了一次doubango整个项目
39 0
CMake教程9:cocos2d-x的CMake逻辑分析
CMake教程9:cocos2d-x的CMake逻辑分析
204 0
|
开发工具 git
gitbook docs 序言
gitbook docs 序言
|
XML Ubuntu 测试技术
【GoogleTest】GoogleTest单元测试(1)快速上手
【GoogleTest】GoogleTest单元测试(1)快速上手
|
Ubuntu Windows
Jpeglib开发笔记(二):JpegLib编译(ubutnu)和Demo
Jpeglib开发笔记(二):JpegLib编译(ubutnu)和Demo
Jpeglib开发笔记(二):JpegLib编译(ubutnu)和Demo
|
并行计算 Ubuntu 编译器
现代cmake--阅读笔记
现代cmake--阅读笔记
261 0