代码单元测试:gtest
生活随笔
收集整理的這篇文章主要介紹了
代码单元测试: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的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 小猪短租平台(短租平台小猪进军乡村民宿市
- 下一篇: 代码覆盖率测试工具:gcov和lcov的