Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: DUnitX : Per test Setup() and UUT construction in C++ Builder XE8


This question is not answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 0
Keith Lambell

Posts: 2
Registered: 3/2/12
DUnitX : Per test Setup() and UUT construction in C++ Builder XE8  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 23, 2015 3:16 PM
In C++ Builder XE8, If I create a new DUnitX Project and Test Case, I find that SetUp() will only be called once, rather than per Test Case, and only if it is marked __published.
I also note that the Test Framework will not construct the test class, so any support classes and the Unit Under Test need to be constructed dynamically.

To get round this, I have used my own function called from each test to take the place of Constructor and SetUp(). See below.

Is this expected behaviour? It is certainly not what I am used to with other frameworks such as Google Test.
It is also a shame that there is no GUI based Runner for C++ Builder.

thanks for any suggestions,

Keith Lambell



/** TestMultiply.cpp */

#include <DUnitX.TestFramework.hpp>

#include <memory>
#include <string>
#include <iostream>

#pragma option --xrtti

#include "CMultiply.h"

/** Delphi type objects must be constructed using new() */
class __declspec(delphirtti) TestMultiply : public TObject
{
public:
/** Construct and setup Unit Under Test and supports */
void ConstructAndSetUp(std::string testName);

/** Test Helper */
void PerformTest();

__published:
/** Tests in order that they are run : note SetUp() is not called */
void __fastcall TestMultiplyByTwo();
void __fastcall TestMultiplyByTen();

private:
/** Unit Under Test */
std::auto_ptr<CMultiply> m_pMultiply;

/** Test supporting values */
double m_factor;
};

/** Construct and SetUp() the test since framework does not do so */
void TestMultiply::ConstructAndSetUp(std::string testName)
{
std::cout << std::endl << testName << std::endl;
m_pMultiply.reset(new CMultiply());
m_factor = 1.0;
}

/** Test Helper */
void TestMultiply::PerformTest()
{
double value = 1.23;
double result = m_pMultiply->Multiply(value, m_factor);
double expected = value * m_factor;
std::cout << "(" << value << " x " << m_factor << ") => " << expected << std::endl;
Dunitx::Testframework::Assert::IsTrue(fabs(result - expected) < 1.0e-6);
}

/** Test we can multiply by 2 */
void __fastcall TestMultiply::TestMultiplyByTwo()
{
ConstructAndSetUp(__FUNCTION__);
m_factor = 2.0;
PerformTest();
}

/** Test we can multiply by 10 */
void __fastcall TestMultiply::TestMultiplyByTen()
{
ConstructAndSetUp(__FUNCTION__);
m_factor = 10.0;
PerformTest();
}

static void registerTests()
{
TDUnitX::RegisterTestFixture(__classid(TestMultiply));
}
#pragma startup registerTests 33

Output:

DUnitX - [TestRunner.exe] - Starting Tests.

.
TestMultiply::TestMultiplyByTwo
(1.23 x 2) => 2.46
..
TestMultiply::TestMultiplyByTen
(1.23 x 10) => 12.3
.

Tests Found : 2
Tests Ignored : 0
Tests Passed : 2
Tests Leaked : 0
Tests Failed : 0
Tests Errored : 0
Done.. press <Enter> key to quit.

Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02