• <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 使用


            一 準(zhǔn)備

            在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,當(dāng)某些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中,當(dāng)然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 夢在天涯 閱讀(5571) 評論(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

            搜索

            •  

            積分與排名

            • 積分 - 1804303
            • 排名 - 5

            最新評論

            閱讀排行榜

            久久笫一福利免费导航| 国产亚洲综合久久系列| 一本综合久久国产二区| 亚洲国产精品无码久久久蜜芽 | 色噜噜狠狠先锋影音久久| 国产成人精品久久亚洲| 色妞色综合久久夜夜| 美女写真久久影院| 久久久国产打桩机| 国产巨作麻豆欧美亚洲综合久久| 99精品久久久久久久婷婷| 99久久婷婷国产综合精品草原| 久久人人爽人人爽人人av东京热 | 国产精品久久国产精品99盘| 久久综合久久鬼色| 国产精品99久久精品| 狠狠色丁香婷婷久久综合| 国产精品免费久久久久影院| 精品综合久久久久久888蜜芽| 亚洲性久久久影院| 久久国产视频网| 久久er热视频在这里精品| 亚洲AV成人无码久久精品老人| 午夜福利91久久福利| 久久精品国产精品亚洲| 伊人久久大香线焦综合四虎| 久久久久99精品成人片欧美| 亚洲女久久久噜噜噜熟女| 亚洲婷婷国产精品电影人久久| 久久夜色撩人精品国产| 久久国产影院| 久久久精品人妻无码专区不卡| 国产精品gz久久久| 久久高清一级毛片| 久久99精品久久久久久齐齐| 国产高清国内精品福利99久久| 色综合久久综合网观看| 91精品婷婷国产综合久久| 久久99国产精品成人欧美| 九九热久久免费视频| 人人狠狠综合久久亚洲|