Skip to content

Commit

Permalink
add gtest intro part, done
Browse files Browse the repository at this point in the history
  • Loading branch information
ffengc committed Jul 21, 2024
1 parent ad06902 commit 9070e64
Show file tree
Hide file tree
Showing 16 changed files with 312 additions and 8 deletions.
12 changes: 12 additions & 0 deletions HareMQ/demo/gtest/makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
.PHONY:all
all: test_assert test_global test_suit

test_assert:test_ASSERT.cc
g++ -o $@ $^ -std=c++11 -lgtest
test_global:test_GLOBAL.cc
g++ -o $@ $^ -std=c++11 -lgtest
test_suit:test_SUIT.cc
g++ -o $@ $^ -std=c++11 -lgtest
.PHONY:clean
clean:
rm -f test_assert test_global test_suit
30 changes: 30 additions & 0 deletions HareMQ/demo/gtest/test_ASSERT.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Write by Yufc
* See https://github.com/ffengc/HareMQ
* please cite my project link: https://github.com/ffengc/HareMQ when you use this code
*/

#include "../log.hpp"
#include <gtest/gtest.h>
#include <iostream>

/**
* 断言宏的使用:
* ASSERT_ 断言失败则推出
* EXPECT_ 断言失败则继续运行
* 注意:
* 断言宏,必须在单元测试宏函数中使用
*/

TEST(test1, MYTEST) {
int age = 20;
ASSERT_GT(age, 18);
std::cout << "OK" << std::endl;
}

int main(int argc, char** argv) {
testing::InitGoogleTest(&argc, argv);
auto res = RUN_ALL_TESTS();
LOG(INFO) << "res: " << res << std::endl;
return 0;
}
32 changes: 32 additions & 0 deletions HareMQ/demo/gtest/test_GLOBAL.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Write by Yufc
* See https://github.com/ffengc/HareMQ
* please cite my project link: https://github.com/ffengc/HareMQ when you use this code
*/

#include "../log.hpp"
#include <gtest/gtest.h>
#include <iostream>

class MyEnv : public testing::Environment {
public:
virtual void SetUp() override {
LOG(INFO) << "before testing, init ..." << std::endl;
}
virtual void TearDown() override {
LOG(INFO) << "after testing, clearing ..." << std::endl;
}
};
TEST(MyEnv, test1) {
LOG(INFO) << "unit test1" << std::endl;
}
TEST(MyEnv, test2) {
LOG(INFO) << "unit test2" << std::endl;
}
int main(int argc, char** argv) {
testing::AddGlobalTestEnvironment(new MyEnv);
testing::InitGoogleTest(&argc, argv);
auto res = RUN_ALL_TESTS();
LOG(INFO) << "res: " << res << std::endl;
return 0;
}
41 changes: 41 additions & 0 deletions HareMQ/demo/gtest/test_SUIT.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Write by Yufc
* See https://github.com/ffengc/HareMQ
* please cite my project link: https://github.com/ffengc/HareMQ when you use this code
*/


#include "../log.hpp"
#include <gtest/gtest.h>
#include <iostream>
#include <unordered_map>

class MyTest : public testing::Test {
public:
virtual void SetUp() override { }
virtual void TearDown() override { }
static void SetUpTestCase() {
LOG(INFO) << "before each suit test, init ..." << std::endl;
}
static void TearDownTestCase() {
LOG(INFO) << "after each suit test, clearing ..." << std::endl;
}

public:
std::unordered_map<std::string, std::string> __map;
};

TEST_F(MyTest, insert_test) {
__map.insert({ "hello", "nihao" });
__map.insert({ "bye", "zaijian" });
}
TEST_F(MyTest, size_test) {
ASSERT_EQ(__map.size(), 2);
}

int main(int argc, char** argv) {
testing::InitGoogleTest(&argc, argv);
auto res = RUN_ALL_TESTS();
LOG(INFO) << "res: " << res << std::endl;
return 0;
}
11 changes: 5 additions & 6 deletions HareMQ/demo/log.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@

/*
* Write by Yufc
* See https://github.com/ffengc/HareMQ
* please cite my project link: https://github.com/ffengc/HareMQ when you use this code
*/

#ifndef __YUFC_LOG__
#define __YUFC_LOG__
Expand All @@ -16,11 +20,6 @@ enum STATUES // 日志等级
FATAL
};

/*
* Write by Yufc
* See https://github.com/ffengc/boost-search-engine
* please cite my project link: https://github.com/ffengc/boost-search-engine when you use this code
*/


// 定义颜色代码
Expand Down
5 changes: 5 additions & 0 deletions HareMQ/demo/muduo/dict_client.cc
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/*
* Write by Yufc
* See https://github.com/ffengc/HareMQ
* please cite my project link: https://github.com/ffengc/HareMQ when you use this code
*/

#ifndef __YUFC_DEMO_DICT_CLIENT_USE_MUDUO__
#define __YUFC_DEMO_DICT_CLIENT_USE_MUDUO__
Expand Down
5 changes: 5 additions & 0 deletions HareMQ/demo/muduo/dict_server.cc
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/*
* Write by Yufc
* See https://github.com/ffengc/HareMQ
* please cite my project link: https://github.com/ffengc/HareMQ when you use this code
*/

#ifndef __YUFC_DEMO_DICT_SERVER_USE_MUDUO__
#define __YUFC_DEMO_DICT_SERVER_USE_MUDUO__
Expand Down
5 changes: 5 additions & 0 deletions HareMQ/demo/muduo/proto_client.cc
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/*
* Write by Yufc
* See https://github.com/ffengc/HareMQ
* please cite my project link: https://github.com/ffengc/HareMQ when you use this code
*/


#ifndef __YUFC_DEMO_PROTOC_CLIENT_USE_MUDUO__
Expand Down
5 changes: 5 additions & 0 deletions HareMQ/demo/muduo/proto_server.cc
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/*
* Write by Yufc
* See https://github.com/ffengc/HareMQ
* please cite my project link: https://github.com/ffengc/HareMQ when you use this code
*/


#ifndef __YUFC_DEMO_PROTOC_SERVER_USE_MUDUO__
Expand Down
5 changes: 5 additions & 0 deletions HareMQ/demo/protobuf/main.cc
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/*
* Write by Yufc
* See https://github.com/ffengc/HareMQ
* please cite my project link: https://github.com/ffengc/HareMQ when you use this code
*/

#include "contacts.pb.h"

Expand Down
7 changes: 5 additions & 2 deletions HareMQ/demo/sqlite/db.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
/**
* 封装sqlite常用方法
/*
* Write by Yufc
* See https://github.com/ffengc/HareMQ
* please cite my project link: https://github.com/ffengc/HareMQ when you use this code
*/


#ifndef __YUFC_SQLITE_HELPER__
#define __YUFC_SQLITE_HELPER__

Expand Down
5 changes: 5 additions & 0 deletions HareMQ/demo/sqlite/test_sql.cc
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/*
* Write by Yufc
* See https://github.com/ffengc/HareMQ
* please cite my project link: https://github.com/ffengc/HareMQ when you use this code
*/

#include "db.hpp"
#include <assert.h>
Expand Down
Binary file added docs/assets/17.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/assets/18.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
150 changes: 150 additions & 0 deletions docs/gtest.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
# GTest

- [GTest](#gtest)
- [GTest是什么](#gtest是什么)
- [我们需要学习的GTest功能](#我们需要学习的gtest功能)
- [宏断言](#宏断言)
- [事件机制](#事件机制)
- [全局测试套件](#全局测试套件)
- [独立测试套件](#独立测试套件)

## GTest是什么

GTest是一个跨平台的 C++单元测试框架,由google公司发布。gtest是为了在不同平台上为编写C++单元测试而生成的。它提供了丰富的断言、致命和非致命判断、参数化等等。

## 我们需要学习的GTest功能

1. 简单的宏断言机制
2. 事件机制(全局测试,单独用例测试)

## 宏断言

GTest中断言的宏可以分为两类:
- `ASSERT_`系列:如果当前检测失败则推出当前函数
- `EXPECT_`系列: 如果当前检测失败则继续向下执行

```cpp
// bool值检查
ASSERT_TRUE(res) // 期待res是true
ASSERT_FALSE(res) // 期待res是false
//数值型数据检查
ASSERT_EQ(arg1, arg2) // arg1 == arg2 返回 true
ASSERT_NE(arg1, arg2) // arg1 != arg2 返回 true
ASSERT_LT(arg1, arg2) // arg1 < arg2 返回 true
ASSERT_GT(arg1, arg2) // arg1 > arg2 返回 true
ASSERT_LE(arg1, arg2) // arg1 <= arg2 返回 true
ASSERT_GE(arg1, arg2) // arg1 >= arg2 返回 true
```
```cpp
TEST(test1, MYTEST) {
}
```
所有的测试都要放在一个单元测试下才行的。宏的第一个参数表示“测试套件名称”,第二个参数表示“测试名称”。

```cpp
testing::InitGoogleTest(&argc, argv);
RUN_ALL_TESTS();
```
通过这种方式来初始化和运行所有的测试。
## 事件机制
测试中,可以有多个测试套件(包含有一组单元测试)
测试套件: 可以理解成一个测试环境,可以在单元测试之前进行测试环境初始化,测试完毕后进行测试环境清理
- 全局测试套件:在整体的测试中,只会初始化一次环境,在所有测试用例完毕后,才会清理环境
- 用例测试套件:在每次的单元测试中,都会重新初始化测试环境,完毕后清理环境
### 全局测试套件
```cpp
#include "../log.hpp"
#include <gtest/gtest.h>
#include <iostream>
class MyEnv : public testing::Environment {
public:
virtual void SetUp() override {
LOG(INFO) << "before testing, init ..." << std::endl;
}
virtual void TearDown() override {
LOG(INFO) << "after testing, clearing ..." << std::endl;
}
};
TEST(MyEnv, test1) {
LOG(INFO) << "unit test1" << std::endl;
}
TEST(MyEnv, test2) {
LOG(INFO) << "unit test2" << std::endl;
}
int main(int argc, char** argv) {
testing::AddGlobalTestEnvironment(new MyEnv);
testing::InitGoogleTest(&argc, argv);
auto res = RUN_ALL_TESTS();
LOG(INFO) << "res: " << res << std::endl;
return 0;
}
```

观察每条打印信息的位置,观察全局套件是何时初始化和何时清理的。

![](./assets/17.png)


### 独立测试套件

- 测试环境类中,可以定义成员变量,成员变量是独立的,是与当前测试套件相关的单元测试才能访问的。
- 单元测试宏名称变成了`TEST_F()`,而且这个宏函数的第一个参数(总测试名称),一定要和套件环境类名一致。(在单元测试宏函数中,能够直接访问类成员变量)

```cpp
#include "../log.hpp"
#include <gtest/gtest.h>
#include <iostream>
#include <unordered_map>

class MyTest : public testing::Test {
public:
static void SetUpTestCase() {
LOG(INFO) << "before each suit test, init ..." << std::endl;
}
static void TearDownTestCase() {
LOG(INFO) << "after each suit test, clearing ..." << std::endl;
}
virtual void SetUp() override { }
virtual void TearDown() override { }
public:
std::unordered_map<std::string, std::string> __map;
};

TEST_F(MyTest, insert_test) {
__map.insert({ "hello", "nihao" });
__map.insert({ "bye", "zaijian" });
}
TEST_F(MyTest, size_test) {
ASSERT_EQ(__map.size(), 2);
}

int main(int argc, char** argv) {
testing::InitGoogleTest(&argc, argv);
auto res = RUN_ALL_TESTS();
LOG(INFO) << "res: " << res << std::endl;
return 0;
}
```
此时运行可以发现,是通过不了测试的,因为 `__map` 是每个测试单元独立的。所以`insert_test`里添加的数据不会出现在`size_test`中。
![](./assets/18.png)
如果想让一开始就有两个数据,可以在:
```cpp
virtual void SetUp() override { }
virtual void TearDown() override { }
```

进行全部的初始化,就是公共的初始化。
7 changes: 7 additions & 0 deletions docs/sqlite.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# SQLite3

- [SQLite3](#sqlite3)
- [什么是SQLite](#什么是sqlite)
- [为什么需要用SQLite](#为什么需要用sqlite)
- [官方文档](#官方文档)
- [封装Helper](#封装helper)
- [进行一些实验](#进行一些实验)

## 什么是SQLite

SQLite是一个进程内的轻量级数据库,它实现了自给自足的、无服务器的、零配置的、事务性的 SQL数据库引擎。它是一个零配置的数据库,这意味着与其他数据库不一样,我们不需要在系统中配置。像其他数据库,SQLite引擎不是一个独立的进程,可以按应用程序需求进行静态或动态连接,SQLite直接访问其存储文件。
Expand Down

0 comments on commit 9070e64

Please sign in to comment.