【问题标题】:CPPUNIT: How to create a instance of TestFixture using its nameCPPUNIT:如何使用名称创建 TestFixture 的实例
【发布时间】: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


【解决方案1】:

这不是固定装置的工作方式,也不是测试的工作方式。为每个测试单独实例化一个夹具,因此测试是隔离的。并且测试以未指定的顺序运行,因此它们不能相互依赖。

通常,当您拥有相互依赖的函数时,您只有一个很长的测试用例来调用它们。如果你想要其他任何东西,你真的必须在 cppunit 之外手动完成。

【讨论】:

  • 你是对的,通常测试函数不应该依赖于其他函数。我已经有一个自己编写的测试环境,它允许我指定要使用 XML 调用的函数(函数顺序、调用时间、参数等......)。而且我已经在 CPPUnit 中有一些测试项目。我尝试在我的 CPPUnit 项目中启用这些功能,以便我拥有一个测试环境而不是两个。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-21
  • 2021-10-17
  • 1970-01-01
  • 2011-10-08
  • 2019-06-25
  • 2014-06-04
相关资源
最近更新 更多