• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            C++ Programmer's Cookbook

            {C++ 基礎(chǔ)} {C++ 高級} {C#界面,C++核心算法} {設(shè)計模式} {C#基礎(chǔ)}

            CPP Unit 使用


            一 準備

            在sourceforge.net上下載CPPUnit的源代碼,地址:http://sourceforge.net/projects/cppunit ,現(xiàn)在最新的版本:cppunit-1.12.0.tar.gz 

            二  參考

            1)看CPPUnit的源代碼,在你下載的目錄下的src目錄下,還有examples目錄下的samples。
            2)在源碼的工程下有一個txt的幫助文件:INSTALL-WIN32.txt, 很不錯,但是是英文的。    
            3)更多的幫助可以查看:
            // http://www.vckbase.com/document/viewdoc/?id=1762
            // http://www.codeproject.com/library/Using_CPPUnit.asp
            // http://cppunit.sourceforge.net/doc/1.8.0/cppunit_cookbook.html#cppunit_cookbook

            三  編譯(我使用vs編譯)

            CPPUnit: Unit test 的核心框架庫
            CPPUnit_dll:作用同上,區(qū)別是上面的是靜態(tài)的lib,此為動態(tài)的DLL,我們使用時可以從中任選一個就可以
            TestRunner:MFC擴展的DLL,負責(zé)Report的GUI瀏覽

            TestPlugInRunner:PlugIn方式運行時,用于運行實現(xiàn)了CPPUnit指定接口的PlugIn DLL
            DllPlugInTester:PlugIn方式運行時,在plugIn dll的post event 中使用,在對Plugin dll的編譯時候檢測結(jié)果。
            DSPlugIn:vc6需要的addin,當某些case沒有通過是,雙擊可以跳到指定的代碼行,VS200*中不需要

            四 實例 (只介紹使用宏定義來實現(xiàn),宏定義使用真是太簡單了,其他的原理和類的實現(xiàn)及關(guān)系可以參考上面的鏈接)

            1)假設(shè)我們要測試的類:

            //test data 
            class SampleClass
            {
            public:
                
            int Add(int i,int j)
                
            {
                    
            return i+j;
                }

                
            int Square(int i)
                
            {
                    
            return i*i;
                }

            }
            ;

            2)我們需要實現(xiàn)相應(yīng)的測試模塊test case:(此類繼承CppUnit::TestFixture或CPPUnit::TestCase,可以實現(xiàn)2個虛函數(shù)setup()和teardown(),他們在每個測試函數(shù)即case的前后自動調(diào)用)
            class SampleClass_TestCase : public CppUnit::TestFixture 
            {
              CPPUNIT_TEST_SUITE(SampleClass_TestCase);
                  CPPUNIT_TEST(Add_Test);
                  CPPUNIT_TEST(Square_Test);
              CPPUNIT_TEST_SUITE_END();

            public:
                
            void setUp()
                
            {
                    testClass 
            = new SampleClass();
                }


                
            void tearDown()
                
            {
                    delete testClass;
                    testClass 
            = NULL;
                }


            protected:

                
            void Add_Test()
                
            {
                    CPPUNIT_ASSERT(
            6 == testClass->Add(1,4));
                }

                
                
            void Square_Test()
                
            {
                    CPPUNIT_ASSERT(
            10 == testClass->Square(3));
                }


            public:
                
            static std::string GetSuiteName();        

            private:
                SampleClass 
            *testClass;    
            }
            ;

             std::
            string SampleClass_TestCase::GetSuiteName()
            {
               
            return "TestSample";
            }
             

            3)增加上面的testcase到test suite中,當然test suite的名字是可以自己定義的。
            //CPPUNIT_TEST_SUITE_REGISTRATION(SampleClassTestCase);
            CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(SampleClass_TestCase,SampleClass_TestCase::GetSuiteName());

            4)增加上面的test suite到test runner,testrunner負責(zé)所有test suite的管理。可以使用CPPUNIT_REGISTRY_ADD( which, to ) 建立suite的樹形關(guān)系。比如:CPPUNIT_REGISTRY_ADD( A, B ) ,CPPUNIT_REGISTRY_ADD( B, C )

            CPPUNIT_REGISTRY_ADD_TO_DEFAULT(SampleClass_TestCase::GetSuiteName());


            5)調(diào)用Testrunner,顯示結(jié)果,一般有三種方式:
                  第一種:GUI的方式,需要我們建立一個MFC的exe,把下面的代碼加入,在我們的theapp::InitInstance()中調(diào)用下面的函數(shù),且函數(shù)要改為返回true:     
            // 1) run ui for output in mfc dialog project(.exe)

            void TestMain()
            {
                
                 
            // declare a test runner, fill it with our registered tests and run them
               CppUnit::MfcUi::TestRunner runner;
               runner.addTest( CPPUNIT_NS::TestFactoryRegistry::getRegistry().makeTest());      

               runner.run();
            //show UI

            }

                第二種:console的方式,需要建立一個console的exe,把下面的代碼加入,在main中調(diào)用下面的函數(shù):
            // 2) run console for output in console project(.exe)

            void TestMain()
            {
                
            // Create the event manager and test controller
                CPPUNIT_NS::TestResult controller;

                
            // Add a listener that colllects test result
                CPPUNIT_NS::TestResultCollector result;
                controller.addListener( 
            &result );        

                
            // Add a listener that print dots as test run.
                CPPUNIT_NS::BriefTestProgressListener progress;
                controller.addListener( 
            &progress );      

                
            // Add the top suite to the test runner
                CPPUNIT_NS::TestRunner runner;
                
            //runner.addTest( CPPUNIT_NS::TestFactoryRegistry::getRegistry().makeTest() );
                runner.run( controller );

                
            // Print test in a compiler compatible format.
                CPPUNIT_NS::CompilerOutputter outputter( &result, CPPUNIT_NS::stdCOut() );
                outputter.write(); 
            }
              
                  第三種:PlugIn的方式,建立一個DLL,加入下面的代碼,且必須在此DLL中調(diào)用下面的宏之一:(需要注意的是你的dll本身有沒有main函數(shù))
                  我們可以對此DLL的post -event事件中增加對DllPlugInTesterud.exe的調(diào)用來在編譯的時候就得到結(jié)果, 或?qū)幾g好的DLL使用TestPlugInRunner來打 開,用GUI的方式查看結(jié)果。
            // //Implements all the plug-in stuffs, WinMain
             
            CPPUNIT_PLUGIN_IMPLEMENT();
            // //or
            // //only export function not has main function. if you have main ,use below macro
            // CPPUNIT_PLUGIN_EXPORTED_FUNCTION_IMPL( CPPUNIT_NS::TestPlugInDefaultImpl );

            四  更多

            1)一般需要使用的頭文件:
            #include <cppunit/BriefTestProgressListener.h>
            #include 
            <cppunit/CompilerOutputter.h>
            #include 
            <cppunit/TestResult.h>
            #include 
            <cppunit/TestResultCollector.h>
            #include 
            <cppunit/TestRunner.h>
            #include 
            <cppunit/extensions/TestFactoryRegistry.h>
            #include 
            <cppunit/ui/mfc/TestRunner.h>
            2)一般需要使用的宏定義:
            //CPPUNIT_ASSERT(condition): checks condition and throws an exception if it's false. 
            //CPPUNIT_ASSERT_MESSAGE(message, condition): checks condition and throws an exception and showing 
            //specified message 
            if it is false
            //CPPUNIT_ASSERT_EQUAL(expected,current): checks if expected is the same as current, and raises exception 
            //showing expected and current values. 
            //CPPUNIT_ASSERT_EQUAL_MESSAGE(message,expected,current): checks if expected is the same as actual, and 
            //raises exception showing expected and current values, and specified message. 
            //CPPUNIT_ASSERT_DOUBLES_EQUAL(expected,current,delta): checks if expected and current difference is 
            //smaller than delta. If it fails, expected and current values are shown. 
            3)如果我們對想對我們的整個工程下的函數(shù)進行單元測試,且想使測試代碼和我們的正式代碼分離,這時我們可以為我們的測試代碼建立一個單獨的dll(使用plugin)或exe(輸出到GUI或console),在此測試工程中加入我們正式的代碼,但是不對我們正式的代碼進行拷貝,但是如果我們的project的靜態(tài)的lib,則我們可以直接調(diào)用不需要把代碼再加入測試模塊中。

            posted on 2007-06-25 22:50 夢在天涯 閱讀(5574) 評論(0)  編輯 收藏 引用 所屬分類: CPlusPlus

            公告

            EMail:itech001#126.com

            導(dǎo)航

            統(tǒng)計

            • 隨筆 - 461
            • 文章 - 4
            • 評論 - 746
            • 引用 - 0

            常用鏈接

            隨筆分類

            隨筆檔案

            收藏夾

            Blogs

            c#(csharp)

            C++(cpp)

            Enlish

            Forums(bbs)

            My self

            Often go

            Useful Webs

            Xml/Uml/html

            搜索

            •  

            積分與排名

            • 積分 - 1807503
            • 排名 - 5

            最新評論

            閱讀排行榜

            国产精品免费看久久久| 久久久久亚洲av无码专区喷水| 婷婷伊人久久大香线蕉AV| 无码人妻久久一区二区三区免费丨 | 日本久久中文字幕| 精品国产日韩久久亚洲| 久久精品亚洲日本波多野结衣| 久久影院综合精品| 亚洲欧洲久久久精品| 色综合久久88色综合天天| 色综合合久久天天综合绕视看| 精品国产青草久久久久福利| 久久精品国产亚洲av麻豆色欲 | 久久久WWW成人| 99国产欧美久久久精品蜜芽| 少妇无套内谢久久久久| 久久成人国产精品| 亚洲欧洲精品成人久久曰影片| 亚洲αv久久久噜噜噜噜噜| 91久久精品无码一区二区毛片| 午夜天堂av天堂久久久| 国产精品xxxx国产喷水亚洲国产精品无码久久一区 | 亚洲女久久久噜噜噜熟女| 精品亚洲综合久久中文字幕| 99蜜桃臀久久久欧美精品网站| 久久伊人色| 久久久网中文字幕| 久久精品国产影库免费看| 久久久久久久波多野结衣高潮| 欧美久久亚洲精品| 久久亚洲国产中v天仙www| 精品久久久久香蕉网| 无码专区久久综合久中文字幕| 国内精品久久久久影院老司| 国产视频久久| 99国内精品久久久久久久| 丰满少妇人妻久久久久久| 久久亚洲精品中文字幕| 久久久久久精品成人免费图片| 久久e热在这里只有国产中文精品99| 久久综合丁香激情久久|