cppunit

上一篇 / 下一篇  2013-12-23 14:33:48 / 个人分类:cppunit

1. 编译cppunit相关及配置vc环境

1.1分别使用debug和release模式编译cppunit,cppunit_dll,TestRunner三个工程,

1.2配置vc环境:  工具菜单->选项->项目和解决方案->vc++目录, 将..\cppunit-1.12.1\include加入到VC的包含文件目录中. 将..\cppunit-1.12.1\lib 加入到vc的库目录中. 然后将 ..\cppunit-1.12.1\lib 路径加入到系统路径中(因为我们的测试程序需要cppunit的dll文件,所以要dll所在路径加入到系统路径(path)变量中, 或者将这些文件拷贝到系统目录中也可以).

2 选择控制台方式搭建第一个工程
由于在CppUnit下, 可以选择控制台方式和UI方式两种表现方案,区别在于前者没有GUI,后者有GUI。我们先选择使用控制台方式的测试环境。
新建c++工程“Win32控制台应用程序”并配置这个工程
 2.1首先,在工程中打开RTTI开关,具体位置在:工程属性->C/C++->语言,启动运行时类型信息选择“是”。
 2.2其次,由于CppUnit所用的动态运行期库均为多线程动态库,因此你的单元测试程序也得使用相应设置,否则会发生冲突。于是我们在工程属性->C/C++->代码生成,运行时库选择:针对debug和release分别设置为“多线程调试DLL”和“多线程DLL”,这里我们就选择前者好了
 2.3然后,link正确的lib,工程属性->链接器->输入,附加依赖项加入“cppunitd.lib”
 2.4最后加入以下文件并编译运行:
math.h
/// math.h
// A TestFixture subclass.
#include "cppunit/extensions/HelperMacros.h"
classMathTest: public CppUnit::TestFixture {
  // 声明一个TestSuite
  CPPUNIT_TEST_SUITE(MathTest);
  // 添加测试用例到TestSuite, 定义新的测试用例需要在这儿声明一下
  CPPUNIT_TEST( testAdd );
  // TestSuite声明完成
  CPPUNIT_TEST_SUITE_END();
  // 其余不变
protected:
  int m_value1, m_value2;
  
public:
  MathTest() {}
  
  // 初始化函数
  void setUp ();
  // 清理函数
  void tearDown();
  
  // 测试加法的测试函数
  void testAdd ();
  // 可以添加新的测试函数
};

math.cpp
/// math.cpp
#include "stdafx.h"
#include "math.h"
// 把这个TestSuite注册到名字为"alltest"的TestSuite中, 如果没有定义会自动定义
// 也可以CPPUNIT_TEST_SUITE_REGISTRATION( MathTest );注册到全局的一个未命名的TestSuite中.
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(MathTest, "alltest" );
// 下面不变
void MathTest::setUp()
{
     m_value1 = 2;
     m_value2 = 2;
}
void MathTest::tearDown()
{
}
void MathTest::testAdd()
{
     int result = m_value1 + m_value2;
     CPPUNIT_ASSERT( result == 4 );
}
<入口文件>.cpp
#include "stdafx.h"
#include "StartCppunit.h"
#include <cppunit/ui/mfc/TestRunner.h>
#include <cppunit/ui/text/TestRunner.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
// 唯一的应用程序对象
//CWinApp theApp;
using namespace std;
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
  CppUnit::TextUi::TestRunner runner;
  
  // 从注册的TestSuite中获取特定的TestSuite, 没有参数获取未命名的TestSuite.
  CppUnit::TestFactoryRegistry &registry = 
      CppUnit::TestFactoryRegistry::getRegistry("alltest");
  // 添加这个TestSuite到TestRunner中
  runner.addTest( registry.makeTest() );
  // 运行测试
  runner.run();
return 0;
}
运行结果是:
                 OK (1 tests)

3 选择MFC GUI方式搭建第二个工程
新建c++工程“MFC应用程序”并配置这个工程,我起得工程名字:MFC
首先,在工程中打开RTTI开关:工程属性->C/C++->语言,启动运行时类型信息选择“是”。
3.1 其次,由于CppUnit所用的动态运行期库均为多线程动态库,因此你的单元测试程序也得使用相应设置,否则会发生冲突。于是我们在工程属性->C/C++->代码生成,运行时库选择:针对debug和release分别设置为“多线程调试DLL”和“多线程DLL”,这里我们就选择前者好了
3.2 然后,link正确的lib,工程属性->链接器->输入,附加依赖项加入“cppunitd.lib testrunnerd.lib”
3.3 配置字符:工程属性->配置属性->常规,字符集选择“选择多字节字符”(否则会报debug assertion failed错误)

3.4 添加testrunnerd.dll到工程的debug目录
3.5 最后加入以下文件并编译运行:
math.h和math.cpp跟第一个工程一样,<入口文件>MFC.cpp加入
#include <cppunit/ui/mfc/TestRunner.h>
#include <cppunit/extensions/TestFactoryRegistry.h>

BOOL CMFCApp::InitInstance()方法修改成//这个函数在CMFCAPP.CPP文件中


BOOL CMFCApp::InitInstance(){
CppUnit::MfcUi::TestRunner runner;
  
// 从注册的TestSuite中获取特定的TestSuite, 没有参数获取未命名的TestSuite.
CppUnit::TestFactoryRegistry &registry = 
    CppUnit::TestFactoryRegistry::getRegistry("alltest");
 // 添加这个TestSuite到TestRunner中
runner.addTest( registry.makeTest() );
// 运行测试
runner.run();
    return true;
}
运行结果可以看到cppunit的GUI界面,Browse内可以看到加入的用例

TAG:

 

评分:0

我来说两句

Open Toolbar