C++ 全栈知识体系C++ 全栈知识体系
✿导航
  • 基础
  • 函数
  • 知识点
  • IO框架
  • 新版本特性
  • 数据库原理
  • SQL语言
  • SQL - MySQL
  • NoSQL - Redis
  • NoSQL - ElasticSearch
  • 算法基础
  • 常见算法
  • 领域算法
  • 分布式算法
  • 数据结构与算法
  • 计算机网络
  • 操作系统
  • 计算机组成
  • 开发
  • 测试
  • 架构基础
  • 分布式系统
  • 微服务
  • 中间件
  • 概念
  • 理论
  • 架构设计原则
  • 设计模式
  • 协议
  • 技术选型
  • 编码规范
  • 流水线构建 - CI/CD
  • 知识点 - Linux
  • 网站 - Nginx
  • 容器化 - Docker
  • 容器编排 - Kubernetes
  • 服务网格 - Service Mesh Istio
  • 常用快捷键 - Shortcut
  • 工具使用 - Tools
  • 开源项目
  • 学习项目
  • 个人项目
  • 项目开发
  • 项目Idea
  • 并发
  • 部署
  • 分布式
  • 知识
  • 问题
  • 编程语言与技术
  • 系统与架构
  • 软件开发实践
  • 数据处理与应用设计
  • 个人
  • 产品
  • 团队
  • 知识体系
  • Vue
关于
✿导航
  • 基础
  • 函数
  • 知识点
  • IO框架
  • 新版本特性
  • 数据库原理
  • SQL语言
  • SQL - MySQL
  • NoSQL - Redis
  • NoSQL - ElasticSearch
  • 算法基础
  • 常见算法
  • 领域算法
  • 分布式算法
  • 数据结构与算法
  • 计算机网络
  • 操作系统
  • 计算机组成
  • 开发
  • 测试
  • 架构基础
  • 分布式系统
  • 微服务
  • 中间件
  • 概念
  • 理论
  • 架构设计原则
  • 设计模式
  • 协议
  • 技术选型
  • 编码规范
  • 流水线构建 - CI/CD
  • 知识点 - Linux
  • 网站 - Nginx
  • 容器化 - Docker
  • 容器编排 - Kubernetes
  • 服务网格 - Service Mesh Istio
  • 常用快捷键 - Shortcut
  • 工具使用 - Tools
  • 开源项目
  • 学习项目
  • 个人项目
  • 项目开发
  • 项目Idea
  • 并发
  • 部署
  • 分布式
  • 知识
  • 问题
  • 编程语言与技术
  • 系统与架构
  • 软件开发实践
  • 数据处理与应用设计
  • 个人
  • 产品
  • 团队
  • 知识体系
  • Vue
关于
  • 开发

    • 常用开发库

      • 常用开发库 - 概述
      • 常用开发库 - Cppcheck
      • 常用开发库 - Cppunit
      • 常用开发库 - Drogon
      • 常用开发库 - FastCGI
      • 常用开发库 - Googletest
      • 常用开发库 - Hiredis
      • 常用开发库 - Jsoncpp
      • 常用开发库 - Log4cpp
      • 常用开发库 - Poco
      • 常用开发库 - Protobuf
      • 常用开发库 - Tinyxml
      • 常用开发库 - Yamlcpp
      • 常用开发库 - ZeroMQ
  • 测试

    • 测试理论

      • 测试理论 - 软件测试的发展
      • 测试理论 - 软件开发过程模型
      • 测试理论 - 测试模型
      • 测试理论 - 软件测试分类
      • 测试理论 - 基本原则及流程
    • 测试用例

      • 测试用例 - 概念
      • 测试用例 - 等价类划分法
      • 测试用例 - 边界值测试法
      • 测试用例 - 判定表法
      • 测试用例 - 因果图法
      • 测试用例 - 正交法
      • 测试用例 - 场景法
      • 测试用例 - 功能图法
    • 测试工具

      • 测试工具 - Postman使用
      • 测试工具 - RobotFramework使用

常用开发库 - googletest

  • 官网
  • googletest使用

概述

GoogleTest is Google’s C++ testing and mocking framework. This user’s guide has the following contents:

  • GoogleTest Primer - Teaches you how to write simple tests using GoogleTest. Read this first if you are new to GoogleTest.
  • GoogleTest Advanced - Read this when you’ve finished the Primer and want to utilize GoogleTest to its full potential.
  • GoogleTest Samples - Describes some GoogleTest samples.
  • GoogleTest FAQ - Have a question? Want some tips? Check here first.
  • Mocking for Dummies - Teaches you how to create mock objects and use them in tests.
  • Mocking Cookbook - Includes tips and approaches to common mocking use cases.
  • Mocking Cheat Sheet - A handy reference for matchers, actions, invariants, and more.
  • Mocking FAQ - Contains answers to some mocking-specific questions.

编写用例

ASSERT_EQ: 出错程序终止 EXPECT_EQ: 出错程序继续运行

ASSERT_EQ(x.size(), y.size()) << "Vectors x and y are of unequal length";

for (int i = 0; i < x.size(); ++i) {
  EXPECT_EQ(x[i], y[i]) << "Vectors x and y differ at index " << i;
}

简单测试

Use the TEST():

TEST(TestSuiteName, TestName) {
  ... test body ...
}

对应同一个函数的测试,一个使用相同的TestSuiteName

// Tests factorial of 0.
TEST(FactorialTest, HandlesZeroInput) {
  EXPECT_EQ(Factorial(0), 1);
}

// Tests factorial of positive numbers.
TEST(FactorialTest, HandlesPositiveInput) {
  EXPECT_EQ(Factorial(1), 1);
  EXPECT_EQ(Factorial(2), 2);
  EXPECT_EQ(Factorial(3), 6);
  EXPECT_EQ(Factorial(8), 40320);
}

测试夹具

当使用两个或多个程序操作相似的数据时,可以使用测试夹具。

TEST_F(TestFixtureClassName, TestName) {
  ... test body ...
}

示例:

class QueueTest : public ::testing::Test {
 protected:
  void SetUp() override {
     // q0_ remains empty
     q1_.Enqueue(1);
     q2_.Enqueue(2);
     q2_.Enqueue(3);
  }

  // void TearDown() override {}

  Queue<int> q0_;
  Queue<int> q1_;
  Queue<int> q2_;
};
TEST_F(QueueTest, IsEmptyInitially) {
  EXPECT_EQ(q0_.size(), 0);
}

TEST_F(QueueTest, DequeueWorks) {
  int* n = q0_.Dequeue();
  EXPECT_EQ(n, nullptr);

  n = q1_.Dequeue();
  ASSERT_NE(n, nullptr);
  EXPECT_EQ(*n, 1);
  EXPECT_EQ(q1_.size(), 0);
  delete n;

  n = q2_.Dequeue();
  ASSERT_NE(n, nullptr);
  EXPECT_EQ(*n, 2);
  EXPECT_EQ(q2_.size(), 1);
  delete n;
}

examples下的demo使用

路径: build_lib\examples\examples\3partlib\googletest

代码

// 校验当前用户成功
TEST_F(UserControllerCurrentTest, current_user_check_success)
{
    // 前面获取过程忽略
    EXPECT_EQ("admin", "admin") << "check current user fail.";
}

// 校验当前用户失败
TEST_F(UserControllerCurrentTest, current_user_check_fail)
{
    // 前面获取过程忽略
    EXPECT_EQ("admin", "klcadmin") << "check current user fail.";
}

执行结果:

[root@5d4b980baaa2 build]# ./googletest/googletest_test
[==========] Running 2 tests from 1 test suite.
[----------] Global test environment set-up.
[----------] 2 tests from UserControllerCurrentTest
[ RUN      ] UserControllerCurrentTest.current_user_check_success
[       OK ] UserControllerCurrentTest.current_user_check_success (0 ms)
[ RUN      ] UserControllerCurrentTest.current_user_check_fail
/home/stibel/examples/examples/3partlib/googletest/source/UserCurrent.cpp:16: Failure
Expected equality of these values:
  "admin"
    Which is: 0x403d60
  "klcadmin"
    Which is: 0x403d5d
check current user fail.
[  FAILED  ] UserControllerCurrentTest.current_user_check_fail (0 ms)
[----------] 2 tests from UserControllerCurrentTest (0 ms total)

[----------] Global test environment tear-down
[==========] 2 tests from 1 test suite ran. (1 ms total)
[  PASSED  ] 1 test.
[  FAILED  ] 1 test, listed below:
[  FAILED  ] UserControllerCurrentTest.current_user_check_fail

 1 FAILED TEST

Last Updated:
Contributors: klc407073648
Prev
常用开发库 - FastCGI
Next
常用开发库 - Hiredis