答えを知っていれば簡単であることが判明したので、ここでは将来の読者のために説明します.
質問で述べたようにIHasCategories
、メソッドを使用して抽象基本クラスを作成しましたget_Categories()
。テスト フィクスチャはメソッドをオーバーライドして、カテゴリのコンマ区切りリストを返します。
パズルの欠けている部分は、CppUnit::TestFixture
CppUnit からインスタンスを取得する方法でした。結局のところ、CppUnit はオブジェクトの階層を提供し、これらのオブジェクトを必要なクラスにCppUnit::Test
単純化できます。dynamic_cast
次のコードは、私のニーズに少し固有のものですが、役立つ場合があります。このメソッドは、TestFixtures を階層的に表す、階層内のオブジェクトのリストを提供します。のようなコマンドで実行するのは、このリスト内のオブジェクトですtest->run(&controller);
。カテゴリの実際の TestFixture オブジェクトを調べたい場合、動的にキャストしたいのはこれらのオブジェクトの子になりますIHasCategories
。
/**
* Get TestSuite objects from the CppUnit hierarchy, that have, as their children,
* TestFixture objects.
*/
void GetTestSuitesWithFixtures(const CppUnit::Test * test, std::vector<CppUnit::Test*> & suites)
{
for (int childIndex = 0; childIndex < test->getChildTestCount(); ++childIndex)
{
CppUnit::Test * child = test->getChildTestAt(childIndex);
CppUnit::TestSuite * testSuite = dynamic_cast<CppUnit::TestSuite*>(child);
if (testSuite
&& testSuite->getChildTestCount() > 0
&& dynamic_cast<CppUnit::TestFixture*>(testSuite->getChildTestAt(0)))
{
suites.push_back(testSuite);
}
else GetTestSuitesWithFixtures(child, suites);
}
}