欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

代码单元测试:gtest

发布时间:2023/12/15 编程问答 34 豆豆
生活随笔 收集整理的这篇文章主要介绍了 代码单元测试:gtest 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

Gtest是Google公司发布的一款非常优秀的开源C/C++单元测试框架,已被应用于多个开源项目及Google内部项目中,知名的例子包括ChromeWeb浏览器、LLVM编译器架构、ProtocolBuffers数据交换格式及工具等。在我们开发规范的代码时候,要想办法构造简单的测试用例进行调试,因此针对gtest中的三种事件机制进行简单的分析。

Features

  • An xUnit test framework.
  • Test discovery.
  • A rich set of assertions.
  • User-defined assertions.
  • Death tests.
  • Fatal and non-fatal failures.
  • Value-parameterized tests.
  • Type-parameterized tests.
  • Various options for running the tests.
  • XML test report generation.

Platforms

Google test has been used on a variety of platforms:

  • Linux
  • Mac OS X
  • Windows
  • Cygwin
  • MinGW
  • Windows Mobile
  • Symbian
  • PlatformIO

 

github:https://github.com/google/googletest

doc:https://github.com/google/googletest/blob/master/googletest/docs/primer.m

参考:

  • http://www.cnblogs.com/coderzh/archive/2009/04/06/1426755.html
  • https://www.cnblogs.com/jycboy/p/6057677.html
  • https://blog.csdn.net/vanturman/article/details/80930159

 

 

1. 安装

  • git clone https://github.com/google/googletest.git
  • cd googletest/
  • mkdir build
  • cd build/
  • cmake ..
  • make
  • sudo make install

 

2. 测试

2.1 代码test.c

#include <iostream> #include <gtest/gtest.h>int add(int a, int b) {return a + b; }TEST(testCase, test0) {EXPECT_EQ(add(2, 3), 5); }int main(int argc, char **argv) {testing::InitGoogleTest(&argc, argv);return RUN_ALL_TESTS(); }

 

2.2 编译

g++ test.cc -lgtest -lpthread -std=c++11

Note: -lpthread需要放在-lgtest后,否则编译会出错

 

2.3 测试

执行:a.out

baoli@ubuntu:~/tools/gtest/mytest$ ./a.out [==========] Running 1 test from 1 test suite. [----------] Global test environment set-up. [----------] 1 test from testCase [ RUN ] testCase.test0 [ OK ] testCase.test0 (0 ms) [----------] 1 test from testCase (0 ms total)[----------] Global test environment tear-down [==========] 1 test from 1 test suite ran. (0 ms total) [ PASSED ] 1 test.

 

总结

以上是生活随笔为你收集整理的代码单元测试:gtest的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。