このトリックを使用すると、個々のテストxmlレポートを一時バッファー/ファイルに収集できます。すべて単一のテストバイナリから。QProcessを使用して、1つのバイナリ内から個別のテスト出力を収集しましょう。テストは、変更された引数を使用して自分自身を呼び出します。最初に、サブテストを適切に利用する特別なコマンドライン引数を紹介します。これらはすべて、テスト実行可能ファイル内にあります。便宜上、QStringListを受け入れるオーバーロードされたqExec関数を使用します。そうすれば、「-subtest」引数をより簡単に挿入/削除できます。
// Source code of "Test"
int
main( int argc, char** argv )
{
int result = 0;
// The trick is to remove that argument before qExec can see it; As qExec could be
// picky about an unknown argument, we have to filter the helper
// argument (below called -subtest) from argc/argc;
QStringList args;
for( int i=0; i < argc; i++ )
{
args << argv[i];
}
// Only call tests when -subtest argument is given; that will usually
// only happen through callSubtestAndStoreStdout
// find and filter our -subtest argument
size_t pos = args.indexOf( "-subtest" );
QString subtestName;
if( (-1 != pos) && (pos + 1 < args.length()) )
{
subtestName = args.at( pos+1 );
// remove our special arg, as qExec likely confuses them with test methods
args.removeAt( pos );
args.removeAt( pos );
if( subtestName == "test1" )
{
MyFirstTest test1;
result |= QTest::qExec(&test1, args);
}
if( subtestName == "test2" )
{
MySecondTest test2;
result |= QTest::qExec(&test2, args);
}
return result;
}
次に、スクリプト/コマンドライン呼び出しで:
./Test -subtest test1 -xml ... >test1.xml
./Test -subtest test2 -xml ... >test2.xml
そしてここにあなたがいます-私たちはテスト出力を分離する手段を持っています。これで、QProcessの機能を引き続き使用してstdoutを収集できます。これらの行をメインに追加するだけです。明示的なテストが要求されていない場合は、実行可能ファイルを再度呼び出すという考え方ですが、特別な引数を使用します。
bool
callSubtestAndStoreStdout(const String& subtestId, const String& fileNameTestXml, QStringList args)
{
QProcess proc;
args.pop_front();
args.push_front( subtestId );
args.push_front( "-subtest" );
proc.setStandardOutputFile( fileNameTestXml );
proc.start( "./Test", args );
return proc.waitForFinished( 30000 ); // int msecs
}
int
main( int argc, char** argv )
{
.. copy code from main in box above..
callSubtestAndStoreStdout("test1", "test1.xml", args);
callSubtestAndStoreStdout("test2", "test2.xml", args);
// ie. insert your code here to join the xml files to a single report
return result;
}
次に、スクリプト/コマンドライン呼び出しで:
./Test -xml # will generate test1.xml, test2.xml
実際、将来のQTestLibバージョンでこれが簡単になることを願っています。