【问题标题】:DUnit on older C++Builder versions?旧 C++Builder 版本上的 DUnit?
【发布时间】:2014-04-07 08:42:18
【问题描述】:

我们目前正在将在 C++Builder 5 下开发的源代码迁移到较新的 Embarcadero 的 XE5。展望未来,我们希望在 C++Builder5 下编写我们的单元测试,理想情况下,它会在迁移后完全正常运行,几乎不需要维护。

不过,我的问题很简单。是否可以在 C++Builder 5 上使用与 Embarcadero 相同的 DUnit 框架?如果是这样,您能给我们一些提示吗?

谢谢。

【问题讨论】:

  • “我们目前正在将在 C++Builder 5 下开发的源代码迁移到较新的 Embarcadero 的 XE5” - 为此留出几周时间......:) 花了我足够长的时间才能从BDS2006 转 XE5。

标签: testing c++builder dunit


【解决方案1】:

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)
  • 项目可以编译运行。测试应该顺利执行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-31
    • 2015-08-23
    • 1970-01-01
    • 1970-01-01
    • 2015-08-06
    • 2020-01-18
    • 2015-01-03
    相关资源
    最近更新 更多