测试框架之GTest

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/351829.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

idea使用junit测试_在JUnit测试中使用Builder模式

idea使用junit测试这并不是要成为技术含量很高的职位。 这篇文章的目的是为您提供一些指导&#xff0c;以使您的JUnit测试生活更加轻松&#xff0c;使您能够在几分钟内编写复杂的测试场景&#xff0c;并获得具有高度可读性的测试。 单元测试中有两个主要部分&#xff0c;需要编…

LeetCode-best time to buy and sell stock 2 数组

1、问题&#xff1a; Say you have an array for which the i th element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock mu…

机器人酷跑闯关_机器人酷跑游戏下载-机器人酷跑手机版下载-地之图下载

机器人酷跑是一款机器人跑酷为游戏题材的动作冒险类游戏。在游戏中你将变成一个小机器人&#xff0c;你需要在各个平台上不断的进行跳跃&#xff0c;躲避各种障碍物以及陷阱&#xff0c;让你可以安全的到达终点。感兴趣的话就赶紧下载游戏玩玩吧&#xff01;机器人酷跑游戏简介…

Visual Studio属性配置中使用宏

在学习C语言的时候&#xff0c;我们曾经遇到过一个宏的概念。宏的作用机理本质上是宏的展开&#xff0c;C语言中的宏的用法也有很多种&#xff08;水其实很深...&#xff09;&#xff0c;不过从感觉上来讲&#xff0c;人们大致上会在以下的场景中&#xff0c;利用宏来解决一些窘…

CodeForces - 500A-New Year Transportation(模拟)

New Year is coming in Line World! In this world, there are n cells numbered by integers from 1 to n, as a 1  n board. People live in cells. However, it was hard to move between distinct cells, because of the difficulty of escaping the cell. People wanted…

LeetCode-best time to buy and sell stock 1 数组

1.问题 Say you have an array for which the i th element is the price of a given stock on day i. If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit. …

linq 清除一条数据中的某个字段值_B端通用批量数据导入方案设计

点击上方蓝色字体&#xff0c;关注我B端通用批量数据导入方案设计文 | 3548字估计阅读 | 9分钟引题B端产品经常遇到大量数据录入的需求。如春季招聘完成后&#xff0c;给新招的120个员工建立员工档案&#xff0c;并创建员工帐号。如果逐条将大量的数据录入系统&#xff0c;将花…

Google C++单元测试框架(Gtest)系列教程之一——入门

引言 本文将先介绍单元测试的相关概念&#xff0c;然后引入Google的开源C单元测试框架Gtest&#xff0c;最后通过编译、运行Gtest自带的一个测试样例&#xff0c;介绍如何在Unix/Linux下使用Gtest。 单元测试 说到单元测试&#xff0c;大家应该不会陌生。作为软件开发过程…

luoguP3185 [HNOI2007]分裂游戏 枚举 + 博弈论

每个位置的瓶子中的每个石子是一个独立的游戏 只要计算出他们的\(sg\)值即可 至于方案数&#xff0c;反正不多\(n^3\)暴力枚举即可 反正怎么暴力都能过啊 复杂度\(O(Tn^3)\) #include <cstdio> #include <cstring> #include <iostream> #include <algorit…

opencv图像前景目标提取

1、功能 论文图片处理需要用到简单的前景目标提取&#xff0c;这里采用opencv的grabCut函数&#xff1b; 前期可以采用selectROI获取矩形框&#xff08;需要添加contrib库&#xff09;&#xff0c;也可以手动设定rect&#xff1b; 添加了一个图片批处理操作&#xff0c;glob函数…

内存结构 堆 栈 全局区 常量区 代码区

转载&#xff1a;https://blog.csdn.net/levy1021/article/details/45419381

批量提交 kafka_Kafka精华问答|kafka的使用场景是什么?

戳蓝字“CSDN云计算”关注我们哦&#xff01;Kafka是由Apache软件基金会开发的一个开源流处理平台&#xff0c;由Scala和Java编写。作为一种高吞吐量的分布式发布订阅消息系统&#xff0c;有着诸多特性。今天&#xff0c;就让我们一起来看看关于它的精华问答吧&#xff01;1Q&a…

Gtest在vs 2010上的配置

找了好多文章&#xff0c;发现这篇是讲得最简便、正确的&#xff01; 以下为复制&粘贴的&#xff1a; [cpp] view plaincopyVS2010 gtest简易配置 使用一个简单的控制台来演示。 1.下载google test http://code.google.com/p/googletest/downloads/detail?nameg…

Python集合list,tuple,dict,set

Python四中集合list&#xff0c;tuple&#xff0c;dict&#xff0c;set list(有数组越界问题) 创建list&#xff1a;L [Michael, 100, True] 访问list&#xff1a;L[0] 倒序访问&#xff1a;L[-1] 添加新元素&#xff1a;L.append(paul)或者L.insert(0,paul) 删除元素与&#…

jmeter性能分析_使用JMeter和Yourkit进行REST / HTTP服务的性能分析

jmeter性能分析我的上一篇文章描述了如何使用JMeter完成异步REST / HTTP服务的压力测试或负载测试。 但是&#xff0c;运行这样的测试通常表明被测系统不能很好地应对增加的负载。 现在的问题是如何找到瓶颈&#xff1f; 深入研究代码以检测可疑部分可能是一种替代方法。 但是…

JAVA错误:无法从静态上下文中引用非静态变量 this

新学习&#xff1a;构造方法的重载&#xff0c;给成员变量赋值 错误代码&#xff1a; class Student {public static void main(String[] args) {Person p new Person();p.setAge(24);p.setName("杨洋");p.show();System.out.println("Hello World!");}…

datetime建立索引有用吗_超全的数据库建表、SQL、索引规范

背景因为工作岗位的原因&#xff0c;负责制定了关于后端组数据库的规约规范&#xff0c;作为所有产品线的规范&#xff0c;历经几版的修改&#xff0c;最终形成下边的文本&#xff0c;规范在整个后端执行也有大半年的时间&#xff0c;对于整个团队在开发阶段就减少不恰当的建表…

linux找不到动态链接库 .so文件的解决方法

如果使用自己手动生成的动态链接库.so文件&#xff0c;但是这个.so文件&#xff0c;没有加入库文件搜索路劲中&#xff0c;程序运行时可能会出现找不到动态链接库的情形。 可以通过ldd命名来查看可执行文件依赖的动态链接库&#xff0c;如下(其中D为可执行程序)&#xff1a; 其…

mui的学习图片预览

css部分 <link href"./css/mui.min.css" rel"stylesheet"/> <style> html,body {margin: 0px;padding: 0px;background-color: white;} .mui-preview-image.mui-fullscreen { position: fixed;z-index: 20; background-co…

Kafka Connect在MapR上

在本周的白板演练中&#xff0c;MapR的高级产品营销经理Ankur Desai描述了Apache Kafka Connect和REST API如何简化和提高在处理来自包括旧数据库或数据仓库在内的各种数据源的流数据时的敏捷性。 他还解释了使用MapR Streams与Kafka进行数据传输时此体系结构的差异。 其他资源…