| #ifndef CPP_UNIT_TestNode_H #define CPP_UNIT_TestNode_H //包含测试框架的头文件 #include <cppunit/TestCase.h> #include <cppunit/extensions/HelperMacros.h> //包含被测试单元的头文件 #include "mabstring.h" //派生测试框架的测试用例类 class TestMabString : public CppUnit::TestCase { //定义测试用例列表,此列表将出现在运行测试用例的选择对话框中 CPPUNIT_TEST_SUITE( TestMabString ); CPPUNIT_TEST( FindByName ); CPPUNIT_TEST_SUITE_END(); protected: // CMabString m_MabStr; public: //用例初始化,可作为桩函数 void setUp (); //用例析构 void tearDown(); protected: //测试用例 void FindByName (void); }; #endif 类文件:testmabstring.cpp #include "TestMabString.h" #include "iostream.h" #include "strstrea.h" //注册本测试单元 CPPUNIT_TEST_SUITE_REGISTRATION( TestMabString ); //定义测试用例 void TestMabString::FindByName () { //功能性测试,属黑盒测试 //normal test //条件及错误测试,属白盒测试 //extra test, //例外测试,属白盒测试 //exception test, bool bRet=false; try{ //put the exception code here... } //catch(CXXX& e) catch(...) { bRet=true; } CPPUNIT_ASSERT(bRet); //由于并不能够执行所有单元测试应该执行的路径,比如CMabString是从CString //类中派生出来的,而可能CMabString中的Find只简单调用了CString中的Find方法,//所以并不需要测试; //在此处说明所有不用测试的路径; //other test, see the ... } void TestMabString::setUp () { //开始测试前的初始代码 m_pNode=new Node(); } void TestMabString::tearDown() { //测试结束代码 if(m_pNode) delete m_pNode; } |