GoogleTest 的简单使用
2021年10月4日 · 613 字 · 2 分钟
The reliability and robustness of SQLite is achieved in part by thorough and careful testing.
As of version 3.33.0 (2020-08-14), the SQLite library consists of approximately 143.4 KSLOC of C code. (KSLOC means thousands of “Source Lines Of Code” or, in other words, lines of code excluding blank lines and comments.) By comparison, the project has 640 times as much test code and test scripts - 91911.0 KSLOC.
开发过程中都没怎么写过 测试代码/测试用例
, 软件测试 相关课程大学里面有上过一门,但毕业后,相关知识已经毫无保留地还给老师了。没有测试代码,测试都是自己和 QA 人肉测试。正常流程过一边,异常 case 测几组,打完收工。现在产品迭代也快,线上出 bug 了,更新即可。有时候,速度比质量更重要,特别是在抢占市场的情况下。
如果在时间允许的情况下,代码质量还是非常非常重要的。编写 测试用例
实现代码测试,也是保证质量的重要方法。项目规模越大,测试代码的优势就越明显。
编写测试用例除了保证代码质量,还可以解决一些开发中常见的麻烦:
- 想测试某个函数,总是在
main
函数里调用一下。main
函数是用来干这个的!! :( main
函数中测试完某个函数后,需要把相关测试代码注释或删除,不方便下次重复测试!! :(- ……
各编程语言都有各自的测试框架,Java 有 JTest等,JS 有 Jest等 …… C++ 也有测试框架, Google 整了一套 GoogleTest
。
GoogleTest is Google’s C++ testing and mocking framework.
GoogleTest 官方页面 有很好的说明介绍。针对 CMake Build System
,我对照页面 Quickstart: Building with CMake 自己实践了一把。正常运行如下图:
GoogleTest 官方也给出了很多的 Sample。
我自己把 Samples
抄了 9/10,边抄边理解,争取早日抄完。抄写地址在这。抄写过程中,如果遇到问题,我会记录到这儿。
loading...
REF
1. GoogleTest
2. GoogleTest CMake
3. How SQLite Is Tested
4. Googletest Samples