Achraf LEBRAZI
Achraf LEBRAZI

Reputation: 51

DUnit on older C++Builder versions?

We are currently migrating our sources who were developed under C++Builder 5 to the newer Embarcadero's XE5. Thinking ahead, we would like to write our unit tests under C++Builder5 which, ideally, would be fully functional after the migration with few maintenance to none.

My question is simple, though. Is it possible to use the same DUnit framework Embarcadero has on C++Builder 5? If so, can you please provide us with any hints?

Thank you.

Upvotes: 0

Views: 544

Answers (1)

Achraf LEBRAZI
Achraf LEBRAZI

Reputation: 51

DUnit can indeed be used on CppBuilder5. To do so:

  • Get the source code for DUnit from here : http://sourceforge.net/projects/dunit/files/latest/download
  • Build DUNITRTL.lib using the following command lines, or you can make a .bat file and execute it from /dunit/src folder :

    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
    

Once done, making a test project becomes easy:

  • Create a VCL Form application.
  • Remove Unit1.cpp along its form from the project.
  • Add DUNITRTL.lib file we built into the project (Project > Add to Project).
  • Add /dunit/src paths to both library and include paths. (Project > Options > Folder/Conditions).
  • Go to Project1.cpp file and make sure it looks like this:
    #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;
    }
  • Add a new unit to the project which will be used as a 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)
  • The project can be compiled and ran. The tests should be executed without a hitch.

Upvotes: 4

Related Questions