gtest是google的一个C/C++测试框架,由C++实现,可在http://code.google.com/p/googletest/下载其源码及库文件。
gtest用法和cppunit用法差不多,个人比较习惯gtest,使用比cppunit方便些。主要通过宏TEST_F定义测试用例,通过EXPECT_系列和ASSERT_系列宏进行检测。
1、源码编译:
下载gtest,解压后,在gtest/msvc下有VC工程文件gtest.sln,用VS2005打开后生成库文件,生成的文件在gtest/msvc/gtest/debug或gtest/msvc/gtest/release下。我们需要的是gtest.lib或gtestd.lib静态库文件。
2、建立一个测试工程,将gtest/include添加到头文件路径中。include文件夹可拷贝到工程目录下。将gtest.lib添加到包含库中。
3、建立主函数文件,例如main.cpp,主要用于初始化及启动gtest测试框架:
#include <iostream> #include "gtest.h" GTEST_API_ int main(int argc, char **argv) { std::cout << "Running main() from gtest_main.cc\n"; testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); }4、将自己测试的文件及相应的头文件包含到工程中,例如自己建立一个选择法排序程序
#include "Sort.h" inline void exchange(int *x,int *y) { int t = *x; *x = *y; *y = t; } void SelectionSort(int *pArray,int cnt) { if(cnt <= 1) return; int i = 0; int j = 0; for(i=0;i<cnt;i++) { for(j=i+1;j<cnt;j++) { if(pArray[i] > pArray[j]) exchange(pArray+i,pArray+j); } } }5、建立测试文件,比如叫做SortTest.cpp,建立测试类,并声明需测试的函数如下:
#include "stdio.h" #include "gtest.h" #include "Sort.h" using testing::Test; class Test_Sort : public Test { void SetUp() { } void TearDown() { } };一个Test_Sort可以添加多个测试用例。其中Setup函数是每个测试用例执行前都要执行的函数,可以用于统一的初始化。TearDown相反是最后调用的函数,可用户Setup中资源的释放。
本例中不使用。
6、添加自己的测试用例:
TEST_F(Test_Sort,SelectionSortTest) { const unsigned int cnt = 10; int test1[] = {10,9,8,7,6,5,4,3,2,1}; int stest1[] = {1,2,3,4,5,6,7,8,9,10}; int i = 0; printf("SelectionSort -- TestCase:"); SelectionSort(test1,cnt); for(i=0;i<cnt;i++) { ASSERT_EQ(test1[i],stest1[i]); } printf("----OK\n"); }7、运行后有如下提示:
可以看到执行成功。
8、如果失败,提示如下:
可以看到错误的位置,及错误的值、期望值。
最简单的gtest使用工程就这样完成了。
常用的判断宏主要有:
ASSERT_TRUE(A):判断A是否true,如果为false则assert,不继续执行。
ASSERT_EQ(A,B):判断AB是否相等,如果不相等则提示,不继续执行。
EXPECT_TRUE(A):判断A是否true,如果为false则提示,继续执行。
EXPECT_EQ(A,B):判断AB是否相等,如果不相等则提示,继续执行。
一般也就用这么4个。其他的比如比较可以转化为ASSERT_TRUE,但这种情况下,用例失败时的提示信息不如ASSERT_LT等多罢了。
为了方便测试排序类算法,我将排序的用例写在了一个函数中,对调试有所不便,但避免了每个排序函数都写相同的用例:
源码如下:
#include "stdio.h" #include "gtest.h" #include "Common.h" #include "Sort.h" using testing::Test; class Test_Sort : public Test { void SetUp() { } void TearDown() { } }; bool checkSorted(int *pArray,int cnt) { if(cnt <= 0) return true; int i = 0; for(i=0;i<cnt-1;i++) if(pArray[i+1] < pArray[i]) return false; return true; } bool cmpArray(int *pArray,int *pSorted,int cnt) { if(cnt <= 0) return true; int i = 0; for(i=0;i<cnt;i++) if(pArray[i] != pSorted[i]) return false; return true; } void PrintArray(int *pArray,int cnt) { printf("\n"); int i = 0; for(i=0;i<cnt;i++) printf("%d,",pArray[i]); printf("\n"); } void SortTest(SK_SORT pFunc,const char *strFuncName) { static const int bigArray = 1024; static const int testCnt = 9; static int test1[] = {1,2,3,4,5,6,7,8,9,10}; static int stest1[] = {1,2,3,4,5,6,7,8,9,10}; static int test2[] = {10,9,8,7,6,5,4,3,2,1}; static int stest2[] = {1,2,3,4,5,6,7,8,9,10}; static int test3[] = {1}; static int stest3[] = {1}; static int test4[] = {1,1,1,1,1}; static int stest4[] = {1,1,1,1,1}; static int test5[] = {1,2,3,2,1}; static int stest5[] = {1,1,2,2,3}; static int test6[] = {3,1,4,6,5,9,7,5,1}; static int stest6[] = {1,1,3,4,5,5,6,7,9}; static int test7[] = {4,2,7,4,5,6,9,4,3,1,5,7,9,32,4,5,7,8}; static int stest7[] = {1,2,3,4,4,4,4,5,5,5,6,7,7,7,8,9,9,32}; static int test8[bigArray]; static int stest8[bigArray]; static int test9[bigArray]; static int stest9[bigArray]; static bool binit = false; int i = 0; if(!binit) { binit = true; for(i=0;i<bigArray;i++) { test8[i] = bigArray-i; stest8[i] = i+1; } for(i=0;i<bigArray;i++) { test9[i] = get_random(0,32767); stest9[i] = test9[i]; } SelectionSort(stest9,bigArray); } struct tagTestCase { int *pArray; int *pSorted; int cnt; bool bPrint; }tests[testCnt] = {{test1,stest1,10,false}, {test2,stest2,10,false}, {test3,stest3,1,false}, {test4,stest4,5,false}, {test5,stest5,5,false}, {test6,stest6,9,false}, {test7,stest7,18,false}, {test8,stest8,bigArray,false}, {test9,stest9,bigArray,false},}; printf("%s -- TestCase:",strFuncName); for(i=0;i<testCnt;i++) { printf("%d..",i); if(tests[i].bPrint) PrintArray(tests[i].pArray,tests[i].cnt); pFunc(tests[i].pArray,tests[i].cnt); if(tests[i].bPrint) PrintArray(tests[i].pArray,tests[i].cnt); ASSERT_TRUE(cmpArray(tests[i].pArray,tests[i].pSorted,tests[i].cnt)); } printf("----OK\n"); } TEST_F(Test_Sort,SelectionSort) { SortTest(SelectionSort,"SelectionSort"); } 下节开始算法导论的学习,其中排序算法的测试用例就用上面。