Martin Brandl
Martin Brandl

Reputation: 58991

CPPUNIT: How to create a instance of TestFixture using its name

I have two test classes called "TT_Common" and "TT_Container" which extends CPPUNIT_NS::TestFixture:

class TT_Common    : public CPPUNIT_NS::TestFixture ......
class TT_Container : public CPPUNIT_NS::TestFixture ......

And anoter class called TT_Runner which extends CPPUNIT_NS::TestRunner:

class TT_Runner : public CPPUNIT_NS::TestRunner
{
   .....
void run( CPPUNIT_NS::TestResult &controller, const std::string &testPath )
{

  CPPUNIT_NS::TestPath  path        = m_suite->resolveTestPath( testPath );
  CPPUNIT_NS::Test      *testToRun  = path.getChildTest();

  for ( size_t iIdx = 0; iIdx < testToRun->getChildTestCount(); iIdx++ )
  {
    CPPUNIT_NS::Test* psTest    = testToRun->getChildTestAt(iIdx);
    std::string       testName  = psTest->getName();
    // testName is TT_Common for iIdx = 0 and TT_Container for iIdx = 1
   }
 }

I already have the name of the TestFixture but how to create a Instace of it? I can't find a factory or registry which takes the name and return a instance.

Upvotes: 2

Views: 777

Answers (1)

Jan Hudec
Jan Hudec

Reputation: 76346

It's not how fixtures work and not how tests work. A fixture is instantiated for each test separately, so the tests are isolated. And the tests are run in unspecified order, so they must not depend on each other.

Normally when you have functions that depend on each other, you just have a long test case that calls them all. If you want anything else, you really have to do it manually outside of cppunit.

Upvotes: 3

Related Questions