• <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++ 基礎} {C++ 高級} {C#界面,C++核心算法} {設計模式} {C#基礎}

            CPP Unit 使用


            一 準備

            在sourceforge.net上下載CPPUnit的源代碼,地址:http://sourceforge.net/projects/cppunit ,現在最新的版本: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:作用同上,區別是上面的是靜態的lib,此為動態的DLL,我們使用時可以從中任選一個就可以
            TestRunner:MFC擴展的DLL,負責Report的GUI瀏覽

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

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

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

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

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

            }
            ;

            2)我們需要實現相應的測試模塊test case:(此類繼承CppUnit::TestFixture或CPPUnit::TestCase,可以實現2個虛函數setup()和teardown(),他們在每個測試函數即case的前后自動調用)
            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負責所有test suite的管理。可以使用CPPUNIT_REGISTRY_ADD( which, to ) 建立suite的樹形關系。比如:CPPUNIT_REGISTRY_ADD( A, B ) ,CPPUNIT_REGISTRY_ADD( B, C )

            CPPUNIT_REGISTRY_ADD_TO_DEFAULT(SampleClass_TestCase::GetSuiteName());


            5)調用Testrunner,顯示結果,一般有三種方式:
                  第一種:GUI的方式,需要我們建立一個MFC的exe,把下面的代碼加入,在我們的theapp::InitInstance()中調用下面的函數,且函數要改為返回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中調用下面的函數:
            // 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中調用下面的宏之一:(需要注意的是你的dll本身有沒有main函數)
                  我們可以對此DLL的post -event事件中增加對DllPlugInTesterud.exe的調用來在編譯的時候就得到結果, 或對編譯好的DLL使用TestPlugInRunner來打 開,用GUI的方式查看結果。
            // //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)如果我們對想對我們的整個工程下的函數進行單元測試,且想使測試代碼和我們的正式代碼分離,這時我們可以為我們的測試代碼建立一個單獨的dll(使用plugin)或exe(輸出到GUI或console),在此測試工程中加入我們正式的代碼,但是不對我們正式的代碼進行拷貝,但是如果我們的project的靜態的lib,則我們可以直接調用不需要把代碼再加入測試模塊中。

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

            公告

            EMail:itech001#126.com

            導航

            統計

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

            常用鏈接

            隨筆分類

            隨筆檔案

            收藏夾

            Blogs

            c#(csharp)

            C++(cpp)

            Enlish

            Forums(bbs)

            My self

            Often go

            Useful Webs

            Xml/Uml/html

            搜索

            •  

            積分與排名

            • 積分 - 1804430
            • 排名 - 5

            最新評論

            閱讀排行榜

            色综合久久中文字幕综合网| 久久国产成人| 久久国产免费观看精品3| 久久国产精品成人影院| 久久99热这里只有精品国产 | 久久人妻少妇嫩草AV蜜桃| 色老头网站久久网| 99国产精品久久久久久久成人热| 久久99热精品| 久久婷婷五月综合成人D啪| 久久亚洲国产欧洲精品一| 伊人久久大香线蕉综合5g| 狠狠色婷婷综合天天久久丁香| 久久久久亚洲爆乳少妇无| 久久夜色精品国产噜噜麻豆| 蜜臀久久99精品久久久久久| 97久久久精品综合88久久| 伊人热热久久原色播放www| 夜夜亚洲天天久久| 精品国产一区二区三区久久久狼 | 久久精品黄AA片一区二区三区| 爱做久久久久久| 99久久免费国产精精品| 国产A级毛片久久久精品毛片| 精品久久久久久久久久中文字幕| 久久狠狠爱亚洲综合影院| 久久影视综合亚洲| 国产午夜福利精品久久| 久久综合久久久| 99久久精品国产麻豆| 久久久久亚洲精品天堂| 久久婷婷是五月综合色狠狠| 久久伊人影视| 欧美成人免费观看久久| 色婷婷狠狠久久综合五月| 无码国内精品久久人妻麻豆按摩 | 久久99精品久久只有精品| 人妻丰满AV无码久久不卡| 欧美丰满熟妇BBB久久久| 久久婷婷五月综合色奶水99啪| 国产亚洲美女精品久久久2020|