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测试框架:

[cpp] view plaincopy
  1. #include <iostream>
  2. #include "gtest.h"
  3. GTEST_API_ int main(int argc, char **argv) {
  4. std::cout << "Running main() from gtest_main.cc\n";
  5. testing::InitGoogleTest(&argc, argv);
  6. return RUN_ALL_TESTS();
  7. }

4、将自己测试的文件及相应的头文件包含到工程中,例如自己建立一个选择法排序程序

[cpp] view plaincopy
  1. #include "Sort.h"
  2. inline void exchange(int *x,int *y)
  3. {
  4. int t = *x;
  5. *x = *y;
  6. *y = t;
  7. }
  8. void SelectionSort(int *pArray,int cnt)
  9. {
  10. if(cnt <= 1)
  11. return;
  12. int i = 0;
  13. int j = 0;
  14. for(i=0;i<cnt;i++)
  15. {
  16. for(j=i+1;j<cnt;j++)
  17. {
  18. if(pArray[i] > pArray[j])
  19. exchange(pArray+i,pArray+j);
  20. }
  21. }
  22. }

5、建立测试文件,比如叫做SortTest.cpp,建立测试类,并声明需测试的函数如下:

[cpp] view plaincopy
  1. #include "stdio.h"
  2. #include "gtest.h"
  3. #include "Sort.h"
  4. using testing::Test;
  5. class Test_Sort : public Test
  6. {
  7. void SetUp()
  8. {
  9. }
  10. void TearDown()
  11. {
  12. }
  13. };

一个Test_Sort可以添加多个测试用例。其中Setup函数是每个测试用例执行前都要执行的函数,可以用于统一的初始化。TearDown相反是最后调用的函数,可用户Setup中资源的释放。

本例中不使用。

6、添加自己的测试用例:

[cpp] view plaincopy
  1. TEST_F(Test_Sort,SelectionSortTest)
  2. {
  3. const unsigned int cnt = 10;
  4. int test1[] = {10,9,8,7,6,5,4,3,2,1};
  5. int stest1[] = {1,2,3,4,5,6,7,8,9,10};
  6. int i = 0;
  7. printf("SelectionSort -- TestCase:");
  8. SelectionSort(test1,cnt);
  9. for(i=0;i<cnt;i++)
  10. {
  11. ASSERT_EQ(test1[i],stest1[i]);
  12. }
  13. printf("----OK\n");
  14. }

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等多罢了。

为了方便测试排序类算法,我将排序的用例写在了一个函数中,对调试有所不便,但避免了每个排序函数都写相同的用例:

源码如下:

[cpp] view plaincopy
  1. #include "stdio.h"
  2. #include "gtest.h"
  3. #include "Common.h"
  4. #include "Sort.h"
  5. using testing::Test;
  6. class Test_Sort : public Test
  7. {
  8. void SetUp()
  9. {
  10. }
  11. void TearDown()
  12. {
  13. }
  14. };
  15. bool checkSorted(int *pArray,int cnt)
  16. {
  17. if(cnt <= 0)
  18. return true;
  19. int i = 0;
  20. for(i=0;i<cnt-1;i++)
  21. if(pArray[i+1] < pArray[i])
  22. return false;
  23. return true;
  24. }
  25. bool cmpArray(int *pArray,int *pSorted,int cnt)
  26. {
  27. if(cnt <= 0)
  28. return true;
  29. int i = 0;
  30. for(i=0;i<cnt;i++)
  31. if(pArray[i] != pSorted[i])
  32. return false;
  33. return true;
  34. }
  35. void PrintArray(int *pArray,int cnt)
  36. {
  37. printf("\n");
  38. int i = 0;
  39. for(i=0;i<cnt;i++)
  40. printf("%d,",pArray[i]);
  41. printf("\n");
  42. }
  43. void SortTest(SK_SORT pFunc,const char *strFuncName)
  44. {
  45. static const int bigArray = 1024;
  46. static const int testCnt = 9;
  47. static int test1[] = {1,2,3,4,5,6,7,8,9,10};
  48. static int stest1[] = {1,2,3,4,5,6,7,8,9,10};
  49. static int test2[] = {10,9,8,7,6,5,4,3,2,1};
  50. static int stest2[] = {1,2,3,4,5,6,7,8,9,10};
  51. static int test3[] = {1};
  52. static int stest3[] = {1};
  53. static int test4[] = {1,1,1,1,1};
  54. static int stest4[] = {1,1,1,1,1};
  55. static int test5[] = {1,2,3,2,1};
  56. static int stest5[] = {1,1,2,2,3};
  57. static int test6[] = {3,1,4,6,5,9,7,5,1};
  58. static int stest6[] = {1,1,3,4,5,5,6,7,9};
  59. static int test7[] = {4,2,7,4,5,6,9,4,3,1,5,7,9,32,4,5,7,8};
  60. static int stest7[] = {1,2,3,4,4,4,4,5,5,5,6,7,7,7,8,9,9,32};
  61. static int test8[bigArray];
  62. static int stest8[bigArray];
  63. static int test9[bigArray];
  64. static int stest9[bigArray];
  65. static bool binit = false;
  66. int i = 0;
  67. if(!binit)
  68. {
  69. binit = true;
  70. for(i=0;i<bigArray;i++)
  71. {
  72. test8[i] = bigArray-i;
  73. stest8[i] = i+1;
  74. }
  75. for(i=0;i<bigArray;i++)
  76. {
  77. test9[i] = get_random(0,32767);
  78. stest9[i] = test9[i];
  79. }
  80. SelectionSort(stest9,bigArray);
  81. }
  82. struct tagTestCase
  83. {
  84. int *pArray;
  85. int *pSorted;
  86. int cnt;
  87. bool bPrint;
  88. }tests[testCnt] = {{test1,stest1,10,false},
  89. {test2,stest2,10,false},
  90. {test3,stest3,1,false},
  91. {test4,stest4,5,false},
  92. {test5,stest5,5,false},
  93. {test6,stest6,9,false},
  94. {test7,stest7,18,false},
  95. {test8,stest8,bigArray,false},
  96. {test9,stest9,bigArray,false},};
  97. printf("%s -- TestCase:",strFuncName);
  98. for(i=0;i<testCnt;i++)
  99. {
  100. printf("%d..",i);
  101. if(tests[i].bPrint)
  102. PrintArray(tests[i].pArray,tests[i].cnt);
  103. pFunc(tests[i].pArray,tests[i].cnt);
  104. if(tests[i].bPrint)
  105. PrintArray(tests[i].pArray,tests[i].cnt);
  106. ASSERT_TRUE(cmpArray(tests[i].pArray,tests[i].pSorted,tests[i].cnt));
  107. }
  108. printf("----OK\n");
  109. }
  110. TEST_F(Test_Sort,SelectionSort)
  111. {
  112. SortTest(SelectionSort,"SelectionSort");
  113. }
  114. 下节开始算法导论的学习,其中排序算法的测试用例就用上面。

测试框架之GTest相关推荐

  1. 项目gtest测试框架 - GoogleTest(十)

    精简版本的C++单元测试框架 ,通过编写这个简单的测试框架,将有助于我们理解gtest. 1. 目录 类型 文件 说明 文件 ./CMakeLists.txt 整体项目工程文件 目录 ./debian ...

  2. gtest测试框架使用详解_测试框架TestNG使用介绍

    近期接触到了一个比较全面的基于Java的接口自动化测试框架,作为一名Java小白,所以打算研究一下,顺带学习学习Java,该测试框架的逻辑控制层使用的HttpClient + TestNG. 在本期中 ...

  3. gtest测试框架使用详解_【python】新手小白必看,教你如何使用全功能Python测试框架 - python秋枫...

    大家好,我是在升职加薪道路上越奋斗头发越少的阿茅. 今天来跟想入门还徘徊在门外的小白们聊一聊 1.安装和简单使用 2.配置文件 3.断言 一. 第1步 (安装和简单使用) pytest是一个非常成熟的 ...

  4. 下一代CC++测试框架TestNG++入门指导【转】

    原文:http://www.cnblogs.com/sinojelly/archive/2010/05/22/1741646.html   xUnit框架改变了单元测试的历史,一时间,很多语言的多种单 ...

  5. 单元测试 : Googel test测试框架

    1.什么是gtest gtest是一个跨平台的(Liunx.Mac OS X.Windows .Cygwin .Windows CE and Symbian ) C++单元测试框架,由google公司 ...

  6. 实现Google测试框架及输出彩色的文字

    1.使用 Google 测试框架 git clone https://gitee.com/maureen-liu/googletest.git cd googletest mkdir mybuild ...

  7. C++船长免费课程 Google测试框架实现

    一:预备知识 1. cout的本质:cout直接输出对象虚重载左移运算符,返回值和传参为左值引用和const引用,友元函数才能访问对象 代码规范其实是不允许直接using namespace的应该具体 ...

  8. Google Test测试框架使用(Linux平台)

    文章目录 一.googleTest测试框架的基本介绍 1.基本概念 2.断言 3.基本断言判断 4.二元比较运算符 5.字符串比较 二.实际搭建google test测试框架 1.准备gtest框架 ...

  9. 软件自动测试框架,软件自动化测试框架的研究和实现

    摘要: 软件自动化测试是软件工程领域的一项重要课题.随着软件工程理论的不断发展,软件自动化测试在理论上也不断达到新的高度.目前最为成熟的软件自动化测试技术是使用自动测试框架来指导自动化测试的实现.迄今 ...

最新文章

  1. BZOJ2584 : [Wc2012]memory
  2. 嵌入式Linux教程一:安装Ubuntu并进行基本配置、交叉编译环境和Minicom
  3. 如何重新安装TCP/IP协议
  4. 人工智能时代的数据中心该怎么建?腾讯给出了自己的答案
  5. mysql卸载安装视频_MYSQL安装与卸载(一)
  6. 什么是CSS?你真的理解?
  7. bash初识,shell的基础语法及基本特性
  8. 聚焦openEuler Summit,解锁云原生、开源等领域的实践干货
  9. POJ 1947 Rebuilding Roads
  10. Unity 性能优化经验整理
  11. 改变风格(css)的四种方法
  12. Jinja2 入门教程、基本概念、简单使用及使用 Jinja2 生成 H3C 交换机配置举例
  13. Win7添加打印机local port端口拒绝访问的解决方法
  14. 【手绘集】我的手绘集
  15. Ruoyi的功能简单介绍
  16. 从零搭建一个自动化运维体系
  17. TLC2543驱动程序
  18. 十六进制转字符串,字符串转十六进制,相互转换
  19. 潜在语义索引(LSI)
  20. Linux将与Windows争夺主流地位

热门文章

  1. mime类型是什么 node_Node.js - 文件系统获取文件类型
  2. 奇迹觉醒qq服务器比微信少,十年内最大的奇迹!功能比QQ还少的微信为什么能成功?...
  3. 是vans_硬核复刻,就服VANS棋盘格
  4. [渝粤教育] 广东-国家-开放大学 21秋期末考试社会学概论10082k1
  5. [渝粤教育] 西南科技大学 施工组织 在线考试复习资料
  6. 【渝粤题库】陕西师范大学202801 中国古代文学(五) 作业
  7. 【渝粤题库】广东开放大学企业标准化 形成性考核
  8. java判断是否第一次出现_利用java判断字符首次出现的位置,java替换最后一个特定字符...
  9. c语言 freopen txt_C语言:freopen函数
  10. php ord函数 中文,php ord 函数与中文乱码解决方法_PHP教程