CppUnit小结

  rel="File-List" href="file:///C:%5CDOCUME%7E1%5CADMINI%7E1%5CLOCALS%7E1%5CTemp%5Cmsohtml1%5C01%5Cclip_filelist.xml"> rel="Edit-Time-Data" href="file:///C:%5CDOCUME%7E1%5CADMINI%7E1%5CLOCALS%7E1%5CTemp%5Cmsohtml1%5C01%5Cclip_editdata.mso">

1、安装配置

1.1 下载CppUnit

       网址:http://sourceforge.net/projects/cppunit

1.2 编译、安装CppUnit

       将下载的cppunit-1.12.1.tar.gz解压,得到cppunit-1.12.1文件夹。在cppunit-1.12.1/src找到并打开CppUnitLibraries.dsw。选择vs2005菜单中的Build->Batch Build, 选中所有项目,点击build按钮。编译成功,会在cppunit-1.12.1/lib下生成所有库文件。

1.3 设置vs2005

       打开’Tools’->’Options…’->’Projects and Solutions’->’VC++ Directories’

’Include files’加入:cppunit-1.12.1/include的路径

’Library files’加入:cppunit-1.12.1/lib的路径

’Source files’加入:cppunit-1.12.1/src的路径

点击’Ok’保存。

1.4 设置测试代码工程

       VS2005中打开你写得测试程序,工程右击,选择’properties’, ’Configuration Properties’->’C/C++’->’Code Generation’->’Runtime Library’, 对于release版,选择'Multithreaded DLL',对于Debug版,选择'Debug Multithreaded DLL'

       ’Configuration Properties’ -> ’C/C++’ -> ’Code Generation’ -> ’Language’ -> ’Enable Rn-Time Type Info’, 选择’yes’

       ’Configuration Properties’->’Linker’ -> ‘Input’ -> ‘Additional Dependencies’, 添入需要的lib文件cppunitX.lib debug模式为cppunitd.lib, release 模式为cppunit.lib )和testrunnerX.libdebug模式为testrunnerd.lib release 模式为testrunner.libdebug Unicode模式为testrunnerud.lib release Unicode模式为testrunneru.lib

       注:添加需要的lib文件也可以在代码中用#pragma comment(lib,”**.lib”)来添加。

1.5 添加dll路径

为使测试程序在运行时能找到CppUnit提供的dll,我们在环境变量中指出CppUnit提供的dll的路径:在我的电脑中,打开环境变量,编辑系统变量中的path变量,向其中添加CPPUNITHOME/lib,从新启动计算机,使设置生效。

注:也可以将需要的dll拷到测试程序的.exe所在目录下或者system32下,而不设环境变量。

2、例子

2.1 待被测试的类

myClass.h

  1. //myClass.h
  2. #ifndef MYCLASS_H
  3. #define MYCLASS_H
  4. class myClass
  5. {
  6. public:
  7.     int add(intint);
  8.     int sub(intint);
  9. };
  10. #endif

myClass.cpp

  1. //myClass.cpp
  2. #include "myClass.h"

  3. int myClass::add(int a, int b)
  4. {
  5.     return a+b;
  6. }

  7. int myClass::sub(int a, int b)
  8. {
  9.     return a-b;
  10. }


2.2 测试案例的类

CTestMyClass.h

  1. //CTestMyClass.h
  2. #ifndef CTESTMYCLASS_H
  3. #define CTESTMYCLASS_H
  4. #include "cppunit/TestCaller.h"
  5. #include "cppunit/extensions/HelperMacros.h"
  6. #include "cppunit/TestSuite.h"
  7. #pragma comment(lib,"cppunitd.lib")
  8. #pragma comment(lib,"testrunnerud.lib")
  9. class CTestMyClass:public CppUnit::TestFixture
  10. {
  11.     CPPUNIT_TEST_SUITE(CTestMyClass);
  12.     CPPUNIT_TEST(TestCase_1);
  13.     CPPUNIT_TEST(TestCase_2);
  14.     CPPUNIT_TEST(TestCase_3);
  15.     CPPUNIT_TEST_SUITE_END();
  16. public:
  17.     void setUp(){}
  18.     void tearDown(){}
  19.     void CTestMyClass::TestCase_1();
  20.     void CTestMyClass::TestCase_2();
  21.     void CTestMyClass::TestCase_3();
  22. };
  23. #endif

CTestMyClass.cpp

  1. //CTestMyClass.cpp
  2. #include "CTestMyClass.h"
  3. #include "myClass.h"
  4. CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(CTestMyClass, "mytest");
  5. void CTestMyClass::TestCase_1()
  6. {
  7.     myClass *mc = new myClass;
  8.     CPPUNIT_ASSERT_EQUAL(5,mc->add(2,3));
  9.     delete mc;
  10. }
  11. void CTestMyClass::TestCase_2()
  12. {
  13.     myClass *mc = new myClass;
  14.     CPPUNIT_ASSERT_EQUAL(-1,mc->sub(2,3));
  15.     delete mc;
  16. }
  17. void CTestMyClass::TestCase_3()
  18. {
  19.     myClass *mc = new myClass;
  20.     //CPPUNIT_ASSERT_EQUAL(2,3-1);
  21.     int a = 4;
  22.     CPPUNIT_ASSERT(4 == a);
  23.     delete mc;
  24. }

2.3 运行测试用例

2.3.1 控制台程序

  1. // TestCppUnit1.cpp : Defines the entry point for the console application.
  2. //
  3. #include "stdafx.h"
  4. #include "CTestMyClass.h"
  5. #include "cppunit/TestCaller.h"
  6. #include "cppunit/extensions/HelperMacros.h"
  7. #include "cppunit/TestSuite.h"
  8. #include "cppunit/ui/text/TestRunner.h"
  9. #include "cppunit/TextOutputter.h"
  10. #include "cppunit/CompilerOutputter.h"
  11. #include <iostream>
  12. #include <fstream>
  13. using namespace std;
  14. int _tmain(int argc, _TCHAR* argv[])
  15. {
  16.     CppUnit::Test *suite = CppUnit::TestFactoryRegistry::getRegistry("mytest").makeTest();
  17.     CppUnit::TextUi::TestRunner runner;
  18.     runner.addTest(suite);
  19.     
  20.     //print the result to the console
  21.     //runner.setOutputter(new CppUnit::CompilerOutputter(&runner.result(), std::cerr));
  22.     //print the result to the file of c:/outputfile.txt
  23.     ofstream outfile;
  24.     outfile.open("c://outputfile.txt",ios::out);
  25.     runner.setOutputter(new CppUnit::TextOutputter(&runner.result(),outfile));
  26.     runner.run();   
  27. }

 

2.3.3 MFC程序

加入头文件:

  1. #include "CTestMyClass.h"
  2. #include "cppunit/TestCaller.h"
  3. #include "cppunit/extensions/HelperMacros.h"
  4. #include "cppunit/TestSuite.h"
  5. #include "cppunit/ui/text/TestRunner.h"
  6. #include "cppunit/ui/mfc/TestRunner.h"
  7. #include "cppunit/TextOutputter.h"
  8. #include "cppunit/CompilerOutputter.h"

调用run函数对测试用例进行测试:

  1. void CMyDlg::OnBnClickedButton1()
  2. {
  3. CppUnit::Test *suite = CppUnit::TestFactoryRegistry::getRegistry("mytest").makeTest();
  4.     CppUnit::MfcUi::TestRunner runner;
  5.     runner.addTest(suite);
  6.     runner.run();
  7. }

3、未完成的课题

3.1WinCE中使用

似乎CppUnit不能在winceSDK下编过,网上也没有发现相应的资料。可以考虑将CppUnit中有用的代码找到,重新自己写,从而在WinCE上实现它。

3.2 CppUnit原码分析

    CppUnit原码很好地运用了STL、设计模式等,而且难度适中,值得学习借鉴。而且网络上有不少人对其进行过研究。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值