CPPUnit用法简介
来源:互联网 发布:solidworks软件画足球 编辑:程序博客网 时间:2024/05/17 23:02
CPPUnit是一个开源的C++单元测试工具.实际上是一些dll,你要写单元测试时调用它的一些类就行.跟我们平时使用一般的dll文件类似.
下面的详细的使用步骤,参考了别人两篇博客写出来的.
把cppunit源文件编译成dll文件
1.)先去网上找个cppunit的源文件来,我找到的是cppunit-1.12.1.tar.gz,解压缩后所有的源文件都在目录cppunit-1.12.1下.
找到源文件后自然是要先把它编译成dll,在目录cppunit-1.12.1\src下面有一个叫CppUnitLibraries.dsw的文件,用Visual studio 2008打开它.然后会弹出个对话框,大概是说这是以前老版本的VS创建的项目,不用管点按钮yes to all.
2.)把项目cppunit设为启动项,此时你一编译会看到一堆错.有两个地方要改
一是把TesrRunner中MsDevCallerListCtrl.cpp文件里面的#import "libid:80cc9f66-e7d8-4ddd-85b6-d9e6cd0e93e2" version("7.0") lcid("0") 中的7.0改成8.0
二是把项目DSPlugIn删除掉.
再次编译就成功了.参考博客:http://blog.csdn.net/forestcell/article/details/2391105
编译成功后我们在cppunit-1.12.1\lib目录下会看到cppunit_dll.lib和cppunit_dll.dll,后面会用到这两个东东.
怎么使用cppunit生成的dll
接下来要看下怎么使用上面生成的dll,我们知道平时使用dll时需要用到三个东西: .h头文件 , .lib库文件, .dll文件
dll文件引用
新建一个win32 console项目.取名CPPUnitTest.把cppunit_dll.dll拷贝到目录CPPUnitTest\Debug下.
头文件与库文件引用
头文件和库文件实际上也可以考过来,但还是从之前的源文件目录中直接引用比较好.点击VS菜单中的Tools->Projects and Solutions->VC++ directories,然后在右上角的下拉列表中先选Include Files,点下面的第二个按钮添加一个文件目录cppunit-1.12.1\include
再在下拉列表中选Library File,添加目录cppunit-1.12.1\lib
测试代码
上面 的dll都已引用好,现在就可以写代码了.代码来源博客:http://www.51testing.com/html/99/n-854399.html
#include <iostream>
#include "stdafx.h"
#include "cppunit/TestCase.h"
#include "cppunit/TestResult.h"
#include "cppunit/TestResultCollector.h"
#include "cppunit/TestCaller.h"
#include "cppunit/TextOutputter.h"
using namespace std;
//被测试类,简单计算器
class SimpleCalculator {
public:
int add(int a, int b) {
return a + b;
}
int sub(int a, int b) {
return a - b;
}
int mul(int a, int b) {
return a * b;
}
int div(int a, int b) {
return a / b;
}
};
//测试类,继承TestCase类
class SimpleCalculatorTest : public CPPUNIT_NS::TestCase {
public:
//测试开始前初始化
virtual void setUp() {
pSc = new SimpleCalculator();
}
//测试结束后资源清理
virtual void tearDown() {
delete pSc;
}
//测试方法
void testAdd() {
CPPUNIT_ASSERT_EQUAL(pSc->add(5,6), 11);
}
void testDiv() {
CPPUNIT_ASSERT_EQUAL(pSc->div(10,1), 0);
}
private:
SimpleCalculator *pSc;
};
int _tmain(int argc, _TCHAR* argv[])
{
CPPUNIT_NS::TestResult tr;
CPPUNIT_NS::TestResultCollector resultCollector;
tr.addListener(&resultCollector);
//testAdd
CPPUNIT_NS::TestCaller<SimpleCalculatorTest> tcAdd("testAdd", &SimpleCalculatorTest::testAdd);
tcAdd.run(&tr);
//testDiv
CPPUNIT_NS::TestCaller<SimpleCalculatorTest> tcDiv("testDiv", &SimpleCalculatorTest::testDiv);
tcDiv.run(&tr);
CPPUNIT_NS::TextOutputter output(&resultCollector, std::cout);
output.write();
return 0;
}
- CPPUnit用法简介
- CppUnit使用简介
- CppUnit使用简介
- C++ - CppUnit 使用简介
- error 2005 Cppunit 用法
- cppunit在mfc中的用法
- cppunit
- cppunit
- CPPUnit
- cppunit
- Cppunit
- CppUnit
- cppunit
- C++单元测试工具CppUnit使用简介 【转载】
- C++单元测试工具CppUnit使用简介
- C++单元测试工具CppUnit使用简介
- C++单元测试工具CppUnit使用简介
- CppUnit代码简介 - 第一部分,核心类 (转)
- 命令模式lua实现
- poj1273
- Hadoop2.2.0版本多节点集群安装及测试
- ARM架构kprobe应用及实现分析(9.0 arch_prepare_kprobe平台相关注册)
- Product_catModel.class.php
- CPPUnit用法简介
- SelfformModel.class.php
- 【javaweb源码】JSP+Servlet+MySQL实现注册功能
- About Maze Problem
- matlab简易画板
- linux 地下安装jboss并设置自动启动
- Qt下结构体中的成员使用问题
- 性能测试流程-各阶段的工作
- android图片裁剪