随着工作时间越久,慢慢的发现单元测试的必要性。
特别是在要实现某项复杂的功能时,对一些组合前的小功能进行测试是很方便的,即时在组合时出了问题,也容易通过测试用例查找。
1.googletest环境搭建
首先从这里下载源文件:
http://code.google.com/p/googletest/downloads/list
我下载的是gtest-1.6.0.zip
$unzip gtest-1.6.0.zip
$cd gtest-1.6.0
编译方法其实很容易,看下README就知道了
g++ -I${GTEST_DIR}/include -I${GTEST_DIR} -c ${GTEST_DIR}/src/gtest-all.cc
ar -rv libgtest.a gtest-all.o
因为我们当前在gtest文件夹下,因此在终端输入
$ g++ -I./include -I./ -c ./src/gtest-all.cc
$ ar -rv libgtest.a gtest-all.o
libgtest.a静态库,以及include下的文件就是我们需要的了。
不妨验证一下:
cd make;make;./sample1_unittest
看到输出:
Running main() from gtest_main.cc
[==========] Running 6 tests from 2 test cases.
[----------] Global test environment set-up.
[----------] 3 tests from FactorialTest
[ RUN ] FactorialTest.Negative
[ OK ] FactorialTest.Negative (0 ms)
[ RUN ] FactorialTest.Zero
[ OK ] FactorialTest.Zero (0 ms)
[ RUN ] FactorialTest.Positive
[ OK ] FactorialTest.Positive (0 ms)
[----------] 3 tests from FactorialTest (2 ms total)
[----------] 3 tests from IsPrimeTest
[ RUN ] IsPrimeTest.Negative
[ OK ] IsPrimeTest.Negative (0 ms)
[ RUN ] IsPrimeTest.Trivial
[ OK ] IsPrimeTest.Trivial (0 ms)
[ RUN ] IsPrimeTest.Positive
[ OK ] IsPrimeTest.Positive (0 ms)
[----------] 3 tests from IsPrimeTest (2 ms total)
[----------] Global test environment tear-down
[==========] 6 tests from 2 test cases ran. (7 ms total)
[ PASSED ] 6 tests.
就ok了
2.自己书写测试用例
贴下我自己的代码,本身用例的目的是为了从当前文件夹下按索引取得某个文件名
/* * ===================================================================================== * Filename: test.cpp * Description: test for gtest * * Version: 1.0 * Created: 08/11/2012 02:48:24 PM * * Author: zhy (), [email protected] * ===================================================================================== */ #include <stdio.h> #include <string> #include <gtest/gtest.h> class Neo { public: Neo(); ~Neo(); std::string GetFileOfIndex(int); private: bool Init(); bool UnInit(); private: FILE* mpFile; }; Neo::Neo() : mpFile(NULL) { Init(); } bool Neo::Init() { if (!UnInit()) return false; mpFile = popen("ls","r"); if (mpFile == NULL) { printf("open error\n"); return false; } return true; } Neo::~Neo() { UnInit(); } bool Neo::UnInit() { if (mpFile) { if (pclose(mpFile) == -1) { printf("close error\n"); return false; } } return true; } std::string Neo::GetFileOfIndex(int index) { char filename[256] = {0}; if (mpFile==NULL) if (!Init()) return std::string(); for ( int i=0; i<index; ++i) { fgets(filename, 255, mpFile); } fgets(filename, 255, mpFile); return filename; } /*TEST,在gtest定义的宏,表示这是一个测试用例 */ TEST(Neo, GetFileByIndex) { Neo neo; printf("%s", neo.GetFileOfIndex(2).c_str()); } int main(int argc, char* argv[]) { /*初始化,照抄即可 */ testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); } 拷贝之前生成的libgtest.a到当前文件夹下
编译并运行
$ g++ -o test test.cpp -L. -lgtest -lpthread
$ ./test
[==========] Running 1 test from 1 test case.
[----------] Global test environment set-up.
[----------] 1 test from Neo
[ RUN ] Neo.GetFileByIndex
main.c
[ OK ] Neo.GetFileByIndex (32 ms)
[----------] 1 test from Neo (34 ms total)
[----------] Global test environment tear-down
[==========] 1 test from 1 test case ran. (36 ms total)
[ PASSED ] 1 test.
我的第一感觉是将TEST的内容放到main函数里不是一样么?一样有测试的作用。
没错,的确是这样的?是么?不是么?
首先,这是一个测试,直接放到main里一样起到了测试的作用,所以是的。
其次,googletest远不止这么简单,所以不是。。。。。。
单从第一印象上来讲,当工程变大,测试用例越来越多时,这样的结果无疑是更清晰一目了然的。
至于googletest又有哪些地方又更加方便测试呢,我想必须要您自己去探索了。找一个网上的小例子,比较简单,用到了断言相关的宏,先贴一下看一下:
简介:
gtest中,断言的宏可以理解为分为两类,一类是ASSERT系列,一类是EXPECT系列。一个直观的解释就是:
1. ASSERT_* 系列的断言,当检查点失败时,退出当前函数(注意:并非退出当前案例)。
2. EXPECT_* 系列的断言,当检查点失败时,继续往下执行。
在这里你可以找到更详细的定义:http://code.google.com/p/googletest/wiki/Primer
/* * ===================================================================================== * Filename: main.c * Description: gtest notes * * Version: 1.0 * Created: 07/31/2012 02:19:16 PM * * Author: zhy (), [email protected] * ===================================================================================== */ #include <gtest/gtest.h> int Factorial(int n) { if (n==2) return 100; return n<=0 ? 1 : n*Factorial(n-1); } TEST(TestFactorial, ZeroInput) { EXPECT_EQ(1, Factorial(0)); } TEST(TestFactorial, OtherInput) { EXPECT_EQ(1, Factorial(1)); EXPECT_EQ(2, Factorial(2)); EXPECT_EQ(6, Factorial(3)); EXPECT_EQ(40320, Factorial(8)); } int main(int argc, char* argv[]) { testing::InitGoogleTest(&argc, argv); RUN_ALL_TESTS(); return 0; }
运行结果: