【发布时间】:2012-01-25 08:39:27
【问题描述】:
我有两个测试类,名为“TT_Common”和“TT_Container”,它们扩展了 CPPUNIT_NS::TestFixture:
class TT_Common : public CPPUNIT_NS::TestFixture ......
class TT_Container : public CPPUNIT_NS::TestFixture ......
还有一个名为 TT_Runner 的类扩展了 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
}
}
我已经有了 TestFixture 的名称,但是如何创建它的 Instace?我找不到采用名称并返回实例的工厂或注册表。
【问题讨论】:
-
为什么要手动实例化一个fixture?夹具是测试的基类。他们自己可能不会做任何有用的事情。
-
我想创建多个类型为 TT_Common 和 TT_Container 的对象,使用其名称在多个线程中启动这些对象的上下文中的函数。
-
测试将在适当的夹具上下文中自动运行(每个测试都在新实例中)。我还是不明白你为什么要手动做。
-
我想为所有测试函数创建一个实例(某些函数将依赖于其他函数)。对于每个线程,我需要其中一个实例。
标签: c++ unit-testing cppunit