2

I have built the example for DUnitX for Rad Studio Berlin in C++. The code is a copy of : http://docwiki.embarcadero.com/RADStudio/Seattle/en/DUnitX_Overview

The header is:

 class __declspec(delphirtti) TestCalc : public TObject
 {
  public:
    virtual void __fastcall SetUp();
    virtual void __fastcall TearDown();

  __published:
     void __fastcall TestAdd();
     void __fastcall TestSub();
  };

TestAdd and TestSub are called because they are under __published, but SetUp and TearDown are never called. I understand that they should be called for each test. Seeing the Delphi code, I can see the [Setup] attribute but it seems that for C++ is not necessary. Am I missing something?

kokokok
  • 1,030
  • 1
  • 9
  • 26

3 Answers3

2

I have the same Problem.

As a workaround I developed a little helper class:

template <typename T>
class TestEnviroment{
public:
    TestEnviroment(T* theTest)
        :itsTest(theTest)
    { itsTest->SetUp(); }

    ~TestEnviroment() { itsTest->TearDown(); }

private:
    T* itsTest;
};

Which is the first local variable in every test case:

void __fastcall UnitTest::Test()
{
    TestEnviroment<UnitTest> testenv{this};

    // TODO Testing
}
Niceman
  • 463
  • 4
  • 8
  • After a little more usage of Dunitx in CppBuilder I advise to use instead the Dunit (without X). Delphi Attributes, which are heavily used by Dunitx but not by Dunit are not supported by CppBuilder. – Niceman Nov 11 '16 at 18:51
0

One solution that fixes this problem is to override the two virtual methods bellow of the TObject base class:

virtual void __fastcall AfterConstruction(void);
__fastcall virtual ~TTestCalc(void);

The first method is executed after the object was created and calls SetUp and the second method overrides the virtual destructor to call TearDown.

Complete solution:

class __declspec(delphirtti) TTestCalc : public TObject
{
public:
    __fastcall virtual ~TTestCalc();
    virtual void __fastcall AfterConstruction();
    virtual void __fastcall SetUp();
    virtual void __fastcall TearDown();

__published:
    void __fastcall TestAdd();
    void __fastcall TestSub();
};
//---------------------------------------------------------------------------

__fastcall TTestCalc::~TTestCalc()
{
TearDown();
}
//---------------------------------------------------------------------------

void __fastcall TTestCalc::AfterConstruction()
{
SetUp();
TObject::AfterConstruction();
}
//---------------------------------------------------------------------------

// Other methods ...
G. Pinto
  • 1
  • 1
0

use:

class __declspec(delphirtti) TestCalc : public TTestCase

instead of:

class __declspec(delphirtti) TestCalc : public `TObject

danutz
  • 1
  • 1