cppunit使用详解

第一步:如何安装 (我的运行环境: fc7 Linux, gcc4)
    cppunit 的安装是相当标准的linux的安装过程
    a. 下载cppunit的源文件
    b. 解压缩
    c. 编译安装程序
      $./configure --prefix=/data/soft/cppunit-1.12
      $make
      $make install
    这里 -prefix=/data/soft/cppunit-1.12 的意思是把安装的根目录设置为 /data/soft/cppunit-1.12
    安装完成以后头文件存储在/data/soft/cppunit-1.12/include,库文件存储在/data/soft/cppunit-1.12/lib。
    因为不是安装在默认的位置所以在编译和连结的时候要指定路径。
    例如:g++ -g -L/data/soft/cppunit-1.12/lib -lcppunit -ldl -I/data/soft/cppunit-1.12/include Main.C
注意这里的几个 -l选项, 尤其是 -ldl 选项。

第二步: 下面我介绍一下个人认为比较实用的测试程序的结构。
    这个测试类从CppUnit::TestFixture派生,并且由下面的部分组成:
    a. setUp() 方法
       在这个方法里实现一些准备工作,例如生成一些被测类的实列
       setUp(){
             m_vertex = new Vertex( 'V' );
       }
    b. tearDown() 方法
       在这个方法里实现扫尾的工作,例如释放内存
       tearDown(){
          //一些在setUp方法中申请的内存的清理工作
          delete m_vertex;
       }
    c. 测试方法的方法
       例如,在被测类里有一个方法叫做:bool operator==(MyComplex &a), 我们
    要写一个名字叫作test_Equality的方法来测试。
      void GraphTest::testConstructor()
 {
     CPPUNIT_ASSERT( m_vertex->label == 'V' );
     CPPUNIT_ASSERT( m_vertex->wasVisited == true );
     CPPUNIT_ASSERT( m_vertex->isInTreeVerts == true );
 }
    CPPUNIT_ASSERT用来判断里面的表达是是否为真。
    d. 把几个测试方法“打包”为一个suite。
      CppUnit::TestSuite *suite= new CppUnit::TestSuite();
     suite->addTest(new CppUnit::TestCaller<GraphTest> ("testConstructor", &GraphTest::testConstructor));
    测试类就是由这些方法组成。

e. 运行测试用例
 CppUnit::TextUi::TestRunner runner;
    runner.addTest( suite ); //指定运行TestSuite
 //开始运行, 自动显示测试进度和测试结果
    runner.run( "", true );

下面通过完整的源代码展现cppunit的使用方法。


// file1 : dijkstra.h 该头文件中含有下面代码,现在我们要使用cppunit对构造函数进行测试

#ifndef DIJKSTRA_H
#define DIJKSTRA_H
struct Vertex
{
public:
    char label; // label (e.g. 'A')

bool wasVisited;
    bool isInTreeVerts;
    
    Vertex( char lab ) // constructor

{
        label = lab;
    }
}; // end struct Vertex

#endif

// file2 : GraphTest.h

#include "dijkstra.h"

#include "cppunit/TestFixture.h"

class GraphTest : public CppUnit::TestFixture {
protected:
    Vertex * m_vertex;
public:
    GraphTest() {}

// 初始化函数

void setUp ();
    // 清理函数

void tearDown();
    
    // 测试构造函数的测试函数

void testConstructor ();
    //还可以添加新的测试函数

};

// file3 : GraphTest.cpp

#include "GraphTest.h"
#include "cppunit/TestAssert.h"

void GraphTest::setUp()
{
    m_vertex = new Vertex( 'V' );
}

void GraphTest::tearDown()
{
    delete m_vertex ;
}

void GraphTest::testConstructor()
{
    CPPUNIT_ASSERT( m_vertex->label == 'V' );
    CPPUNIT_ASSERT( m_vertex->wasVisited == true );
    CPPUNIT_ASSERT( m_vertex->isInTreeVerts == true );
}

// file4: main.cpp

#include "GraphTest.h"
#include "cppunit/ui/text/TestRunner.h"
#include "cppunit/TestCaller.h"
#include "cppunit/TestSuite.h"
int main()
{
    CppUnit::TextUi::TestRunner runner;
    CppUnit::TestSuite *suite= new CppUnit::TestSuite();

// 添加一个测试用例

suite->addTest(new CppUnit::TestCaller<GraphTest> ("testConstructor", &GraphTest::testConstructor));

runner.addTest( suite ); //指定运行TestSuite

//开始运行, 自动显示测试进度和测试结果

runner.run( "", true ); }

好了。都准备好了,编译:

[root@zieckey cppunit]# g++ GraphTest.cpp main.cpp -lcppunit -I /data/soft/cppunit-1.12/include/ -L /data/soft/cppunit-1.12/lib
/data/soft/cppunit-1.12/lib/libcppunit.so: undefined reference to `dlsym'
/data/soft/cppunit-1.12/lib/libcppunit.so: undefined reference to `dlopen'
/data/soft/cppunit-1.12/lib/libcppunit.so: undefined reference to `dlclose

这个错误是由于没有找到 dlsym 等函数的链接库,制定下他们的连接库:

[root@zieckey cppunit]# g++ GraphTest.cpp main.cpp -lcppunit -I /data/soft/cppunit-1.12/include/ -L /data/soft/cppunit-1.12/lib -ldl

编译成功,运行:

[root@zieckey cppunit]# ./a.out
.F

!!!
Test Results:
Run: 1 Failures: 1 Errors: 0

1) test: testConstructor (F) line: 17 GraphTest.cpp
assertion failed
- Expression: m_vertex->wasVisited == true

<RETURN> to continue

发现一个错误,构造函数没有按照我们想象的对成员变量 wasVisited 初始为 true
从而发现一个错误,修改 Vertex 的构造如下:

Vertex( char lab ) // constructor

{
        label = lab;
        wasVisited = true;
        isInTreeVerts = true;
    }

再次编译,运行,通过测试。

cppunit使用详解的更多相关文章

  1. Junit使用详解

    http://junit.org/上详细介绍了Junit.JUnit is a simple framework to write repeatable tests. It is an instanc ...

  2. Linq之旅:Linq入门详解(Linq to Objects)

    示例代码下载:Linq之旅:Linq入门详解(Linq to Objects) 本博文详细介绍 .NET 3.5 中引入的重要功能:Language Integrated Query(LINQ,语言集 ...

  3. 架构设计:远程调用服务架构设计及zookeeper技术详解(下篇)

    一.下篇开头的废话 终于开写下篇了,这也是我写远程调用框架的第三篇文章,前两篇都被博客园作为[编辑推荐]的文章,很兴奋哦,嘿嘿~~~~,本人是个很臭美的人,一定得要截图为证: 今天是2014年的第一天 ...

  4. EntityFramework Core 1.1 Add、Attach、Update、Remove方法如何高效使用详解

    前言 我比较喜欢安静,大概和我喜欢研究和琢磨技术原因相关吧,刚好到了元旦节,这几天可以好好学习下EF Core,同时在项目当中用到EF Core,借此机会给予比较深入的理解,这里我们只讲解和EF 6. ...

  5. Java 字符串格式化详解

    Java 字符串格式化详解 版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 文中如有纰漏,欢迎大家留言指出. 在 Java 的 String 类中,可以使用 format() 方法 ...

  6. Android Notification 详解(一)——基本操作

    Android Notification 详解(一)--基本操作 版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 源码:AndroidDemo/Notification 文中如有纰 ...

  7. Android Notification 详解——基本操作

    Android Notification 详解 版权声明:本文为博主原创文章,未经博主允许不得转载. 前几天项目中有用到 Android 通知相关的内容,索性把 Android Notificatio ...

  8. Git初探--笔记整理和Git命令详解

    几个重要的概念 首先先明确几个概念: WorkPlace : 工作区 Index: 暂存区 Repository: 本地仓库/版本库 Remote: 远程仓库 当在Remote(如Github)上面c ...

  9. Drawable实战解析:Android XML shape 标签使用详解(apk瘦身,减少内存好帮手)

    Android XML shape 标签使用详解   一个android开发者肯定懂得使用 xml 定义一个 Drawable,比如定义一个 rect 或者 circle 作为一个 View 的背景. ...

随机推荐

  1. B. Quasi Binary

    开启博客园 记录codeforces程序 这个题目很简单 一个数能被最少的只包含0 ,1的数字和 如:9 = 1+1+1+1+1+1+1+1+1 10 =10 12 =11+ 1 求得是最少个数的整数 ...

  2. C语言标准

    1,K&R C 1978年, Dennis Ritchie和Brian Wilson Kernighan合作出版了<The C Programming Language>的第一版. ...

  3. Android 核心分析 之七Service深入分析

    Service深入分析 上一章我们分析了Android IPC架构,知道了Android服务构建的一些基本理念和原理,本章我们将深入分析Android的服务.Android体系架构中三种意义上服务: ...

  4. 关于html5与jsp页面同样的html代码展示的页面效果不一样的问题

    原文:关于html5与jsp页面同样的html代码展示的页面效果不一样的问题 html5默认的声明为 <!DOCTYPE html> jsp默认的声明头部为 <%@ page con ...

  5. SQL SERVER ->> Wait Stats

    等待状态是隔离SQL SERVER性能问题的一个重要手段,从实例级别入手.可以大概看出SQL SERVER在过去某段时间内是否处于某种资源的吃紧状态.不过取决于你是否有建立有效的性能数据收集机制.sy ...

  6. Spring Boot MyBatis 通用Mapper插件集成

    Mybatis在使用过程中需要三个东西,每张表对应一个XXMapper.java接口文件,每张表对应一个XXMapper.xml文件,每张表对应一个Entity的Java文件.   其中XXMappe ...

  7. OpenGL基础知识

    基本概念 透视(Perspective)变换(Transformation)投影矩阵(Projection Matrix):用于将3D坐标转换为2D屏幕坐标光栅化(Rasterization): 实际 ...

  8. 脉络清晰的BP神经网络讲解,赞

    学习是神经网络一种最重要也最令人注目的特点.在神经网络的发展进程中,学习算法的研究有着十分重要的地位.目前,人们所提出的神经网络模型都是和学习算 法相应的.所以,有时人们并不去祈求对模型和算法进行严格 ...

  9. dojo 资源库

    文档 :http://wenku.baidu.com/link?url=Nnek_tAjIC-Q3t3e9zHQmsh4LhU3f0ncC1QH8WD_U9-I8-fJ7mMisscFpgfuS8nU ...

  10. HttpClient通过GET和POST获取网页内容

    中国银行支付网关---银行回调的接口 最简单的HTTP客户端,用来演示通过GET或者POST方式访问某个页面 /** * 中国银行支付网关---银行回调的接口 * @svncode svn://10. ...