通过这个技巧,您可以将单独的测试 xml 报告收集到临时缓冲区/文件中;全部来自一个测试二进制文件。让我们使用 QProcess 从一个二进制文件中收集单独的测试输出;测试使用修改后的参数调用自身。首先,我们引入了一个特殊的命令行参数,它可以正确地利用子测试——所有这些都仍然在您的测试可执行文件中。为方便起见,我们使用接受 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 的能力为您收集标准输出。只需将这些行附加到您的主目录。这个想法是再次调用我们的可执行文件,如果没有要求明确的测试,但使用我们的特殊参数:
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 版本可以让这更容易做到。