Search code examples
c++builderdunitx

SetUp is not called in DUnitX in Rad Studio


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?


Solution

  • 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
    }