在较旧的C ++ Builder版本上使用DUnit?

时间:2014-04-07 08:42:18

标签: testing c++builder dunit

我们目前正在将使用C ++ Builder 5开发的源代码迁移到较新的Embarcadero XE5。在未来的思考中,我们希望在C ++ Builder5下编写单元测试,理想情况下,这些测试在迁移后将完全正常运行,几乎没有维护。

但我的问题很简单。是否可以在C ++ Builder 5上使用相同的DUnit框架Embarcadero?如果是的话,请您提供任何提示吗?

谢谢。

1 个答案:

答案 0 :(得分:4)

DUnit确实可以在CppBuilder5上使用。 为此:

  • 从此处获取DUnit的源代码:http://sourceforge.net/projects/dunit/files/latest/download
  • 使用以下命令行构建DUNITRTL.lib,或者您可以创建一个.bat文件并从/ dunit / src文件夹中执行它:

    SET NDC6=C:\PROGRA~2\Borland\CBUILD~1
    %NDC6%\bin\dcc32.exe Dunit.dpr /O..\objs /DBCB /M /H /W /JPHN -$d-l-n+p+r-s-t-w-y- %2 %3 %4
    %NDC6%\bin\tlib.exe DUNITRTL.lib /P32 -+dunit.obj -+DunitAbout.obj -+DUnitMainForm.obj -+GUITestRunner.obj -+TestExtensions.obj -+TestFramework.obj -+TestModules.obj -+TextTestRunner.obj
    

完成后,使测试项目变得简单:

  • 创建VCL表单应用程序。
  • 从项目中删除其表单中的Unit1.cpp。
  • 添加我们在项目中构建的DUNITRTL.lib文件(项目>添加到项目中)。
  • 将/ dunit / src路径添加到库和包含路径。 (项目>选项>文件夹/条件)。
  • 转到 Project1.cpp 文件并确保其如下所示:
    #include <vcl.h>
    #pragma hdrstop

    #include <GUITestRunner.hpp>

    USERES("Project1.res");
    USELIB("DUNITRTL.lib");
    //---------------------------------------------------------------------------
    WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
    {
      try
      {
         Application->Initialize();
         Guitestrunner::RunRegisteredTests();
      }
      catch (Exception &exception)
      {
         Application->ShowException(&exception);
      }
      return 0;
    }
  • 将新单元添加到项目中,该单元将用作TestSuite。

<强> MyTestCase.h

    //---------------------------------------------------------------------------
    #ifndef __TMYTESTCASE_H__
    #define __TMYTESTCASE_H__
    //---------------------------------------------------------------------------
    #include <TestFramework.hpp>
    class TMyTestCase : public TTestCase
    {
      public:
        __fastcall virtual TMyTestCase(AnsiString name) : TTestCase(name) {}
        virtual void __fastcall SetUp();
        virtual void __fastcall TearDown();

      __published:
        void __fastcall MySuccessfulTest();
        void __fastcall MyFailedTest();
    };
    #endif

<强> MyTestCase.cpp

    #include <vcl.h>
    #pragma hdrstop
    //---------------------------------------------------------------------------
    #include "TMyTestCase.h"
    //---------------------------------------------------------------------------

    void __fastcall TMyTestCase::SetUp()
    {}        
    void __fastcall TMyTestCase::TearDown()
    {}

    void __fastcall TMyTestCase::MySuccessfulTest()
    {
      int a = 1;
      a = a + 1;
      CheckEquals(2,a,"test adding");
    }

    void __fastcall TMyTestCase::MyFailedTest()
    {
      int a = 1;
      a = a + 2;
      CheckEquals(2,a,"test adding");
    }

    static void registerTests()
    {
      _di_ITestSuite iSuite;      
      TTestSuite* testSuite = new TTestSuite("Testing TMyTestCase.h");

      if (testSuite->GetInterface(__uuidof(ITestSuite), &iSuite))
      {
        testSuite->AddTests(__classid(TMyTestCase));
        Testframework::RegisterTest(iSuite);
      }
      else
      {
        delete testSuite;
      }
    }

    #pragma startup registerTests 33
    #pragma package(smart_init)
  • 可以编译和运行项目。测试应该毫无障碍地执行。