1安装
版本:1.1.2
2解压之后进入目录
3配置
./configure
[kiosk@foundation45 cpptest-1.1.2]$ ./configure
4编译
make
[kiosk@foundation45 cpptest-1.1.2]$ make
5安装
make install
[root@foundation45 cpptest-1.1.2]# make install
6添加共享库目录到共享库配置文件
[root@foundation45 cpptest-1.1.2]# echo /usr/local/lib >> /etc/ld.so.conf
[root@foundation45 cpptest-1.1.2]# ldconfig
1.引入测试的头文件:#include“cpptest.h”
2.继承Test::Suite编写套件类
3.给套件类添加测试成员函数
4.使用宏定义TEST_ADD注册测试成员函数
5编译执行
[kiosk@foundation45 leimoban]$ g++ array.cpp -o test -lcpptest
[kiosk@foundation45 leimoban]$ ./test >> test.html
案例:
#include"array.h"
#include"cpptest.h"
//继承套件类
class MyTest:public Test::Suite{
public:
//使用宏定义注册成员函数
MyTest(){
TEST_ADD(MyTest::test_case1);
TEST_ADD(MyTest::test_case2);
}
private:
//自己写测试的具体实现函数
void test_case1(){
//测试之前要实例化
Array array;
array.push_back(1);
array.push_back(2);
array.push_back(4);
//TEST_ASSERT是一个宏
TEST_ASSERT(2 == array.Get(1));
}
void test_case2(){
Array array;
TEST_ASSERT(array.Size() == 0);
}
};
int main ( )
{
MyTest test;
Test::HtmlOutput output;
test.run(output,true);
output.generate(std::cout, true, "MyTest");
return 0;
}
#ifndef _ARRAY_H_
#define _ARRAY_H_
#include<stdexcept>
#include<iostream>
//异常机制
//invalid_argument是一个类
using std::invalid_argument;
using std::out_of_range;
using namespace std;
class Array
{
public:
Array():count_(0),pData_(NULL){}
void push_back(int n)
{
if(0==count_){
pData_ = new int[1];
pData_[0] = n;
}else{
int* temp = new int[count_+1];
for(int i=0;i<count_;i++){
temp[i] = pData_[i];
}
temp[count_] = n;
delete [] pData_;
pData_ = temp;
}
count_++;
}
int Size(){
return count_;
}
int Get(int i)
{
//抛异常,跑异常的时候要抛出对象,不能抛一个没有实例化的类
if(i<0)throw invalid_argument("invalid argument");
if(i>count_)throw out_of_range("out_of_range");
return pData_[i];
}
private:
//数组的大小
int count_;
//数组的数据的头指针
int* pData_;
};
//#include "array.cpp"
#endif //
测试宏 | 含义 |
---|---|
TEST_FAIL (message) | 无条件地产生失败,并停止执行 |
TEST_ASSERT (expression) | 如果表达式的结果是false,打印一个错误 |
TEST_ASSERT_MSG (expression, message) | 与TEST_ASSERT (expression)相似,只是错误打印信息 |
TEST_THROWS (expression, exception) | 如果没有捕捉到异常,就触发断言 |
TEST_THROWS_MSG (expression, exception, message) | 与TEST_THROWS (expression, exception)相似,只是错误打印信息 |
exception是异常类型,不是异常对象。参数设置的时候是类型,抛的时候要抛一个对象。
格式 | 含义 |
---|---|
Test::TextOutput | 显示模式可以是详细或简洁。 |
Test::CompilerOutput | 编译器构建日志相似的方式生成输出。 |
Test::HtmlOutput | 生成 HTML 输出。 |