• <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 夢在天涯 閱讀(5577) 評論(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

            搜索

            •  

            積分與排名

            • 積分 - 1808482
            • 排名 - 5

            最新評論

            閱讀排行榜

            久久国产色AV免费观看| 久久亚洲国产午夜精品理论片| 99久久国产综合精品五月天喷水| 国产精品亚洲综合专区片高清久久久 | 亚洲色欲久久久综合网| 久久国产一区二区| 国产A级毛片久久久精品毛片| 亚洲精品无码久久一线| 久久婷婷人人澡人人爽人人爱| 国产精品久久国产精品99盘| 久久精品国产亚洲AV香蕉| 久久精品水蜜桃av综合天堂| 久久国产影院| 久久夜色精品国产www| 91精品免费久久久久久久久| 人人妻久久人人澡人人爽人人精品 | 日韩精品无码久久久久久| 久久久久亚洲国产| 久久精品免费观看| 久久综合鬼色88久久精品综合自在自线噜噜 | 久久成人影院精品777| 香蕉aa三级久久毛片| 久久久这里有精品中文字幕| 久久久久AV综合网成人| 久久久久精品国产亚洲AV无码| 国产午夜精品理论片久久| 很黄很污的网站久久mimi色 | 久久e热在这里只有国产中文精品99| 久久久精品国产免大香伊| 亚洲国产成人精品91久久久| 国产成人精品久久综合| 亚洲欧美日韩精品久久| 精品久久久久久久| 久久99国产精品久久99| 99精品国产在热久久无毒不卡 | 亚洲欧美日韩精品久久亚洲区| 久久久久久毛片免费看| 精品99久久aaa一级毛片| 久久美女人爽女人爽| 99久久国产热无码精品免费久久久久| 久久AV高清无码|