cppunit使用详解
来源:互联网 发布:qq windows rt 版本 编辑:程序博客网 时间:2024/06/08 03:18
寄语:本人只做了windows的,这篇linux的没做,只使用了他的例子,不错
原文地址:http://blog.chinaunix.net/uid-20384806-id-1954332.html
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 ("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使用详解
- CppUnit使用
- 使用CppUnit进行单元测试
- CppUnit的使用步骤
- CppUnit的使用步骤
- CppUnit的使用步骤
- CppUnit使用简介
- 使用CppUnit进行单元测试
- CppUnit使用备忘录
- cppunit 的使用
- eclipse 中使用cppunit
- CppUnit使用简介
- cppunit vs2005 使用
- CppUnit 安装和使用
- CppUnit基础使用
- CPPUNIT使用学习手册
- 驱动程序中使用CPPUnit
- 使用cppunit编译单元测试
- 这是什么心理,不买苹果6s只要买6
- S控制滚动条的位置
- php+mysql+jquery 简易的检索自动补全提示
- testNg执行顺序
- Sublime Text保存文件时自动去掉行末空格
- cppunit使用详解
- C#中的WebBrowser控件的使用
- ruby在MAC上的安装环境
- 基于华为java编程规范的checkstyle.xml以及格式化模版,注释模版
- 世上哪来那么多的无心插柳
- 收藏一个安卓常见问题问答
- poj 3463 Sightseeing(dij求最短路+次短路(比最短路长1)的总走法)
- jar包和war包
- 纯CSS自适应图片大小