如何使用UnitTest ++运行单个测试?

Arn*_*rno 11 c++ unittest++

如何使用UnitTest ++运行单个测试?

我正在运行UnitTest ++.我的main功能如下:

int main()
{
   printf("diamond test v0.1 %s\n\n",TIMESTAMP);
   diamond::startup();
   UnitTest::RunAllTests();
   diamond::shutdown();
   printf("press any key to continue...");
   getc(stdin);
}
Run Code Online (Sandbox Code Playgroud)

对于调试我想写一些UnitTest::RunSingleTests("MyNewUnitTest");代替的东西UnitTest::RunAllTests();.UnitTest ++是否提供了这样的功能?如果是,那么语法是什么?

sti*_*ijn 13

尝试将此作为unittest的main()(我实际上把它放在一个文件中并将其添加到unittest库中,这样当链接到库时,可执行文件会自动使用这个main().非常方便.)

int main( int argc, char** argv )
{
  if( argc > 1 )
  {
      //if first arg is "suite", we search for suite names instead of test names
    const bool bSuite = strcmp( "suite", argv[ 1 ] ) == 0;

      //walk list of all tests, add those with a name that
      //matches one of the arguments  to a new TestList
    const TestList& allTests( Test::GetTestList() );
    TestList selectedTests;
    Test* p = allTests.GetHead();
    while( p )
    {
      for( int i = 1 ; i < argc ; ++i )
        if( strcmp( bSuite ? p->m_details.suiteName
                           : p->m_details.testName, argv[ i ] ) == 0 )
          selectedTests.Add( p );
      p = p->next;
    }

      //run selected test(s) only
    TestReporterStdout reporter;
    TestRunner runner( reporter );
    return runner.RunTestsIf( selectedTests, 0, True(), 0 );
  }
  else
  {
    return RunAllTests();
  }
}
Run Code Online (Sandbox Code Playgroud)

使用参数调用以运行单个测试:

> myexe MyTestName
Run Code Online (Sandbox Code Playgroud)

或单人套房

> myexe suite MySuite
Run Code Online (Sandbox Code Playgroud)


小智 7

这几乎是正确的."测试"实际上是作为链接列表中的节点,因此当您将其添加到新列表时,您必须更正指针以避免包含比您预期更多的测试.

所以你需要更换

  p = p->next;
Run Code Online (Sandbox Code Playgroud)

  Test* q = p;
  p = p->next;
  q->next = NULL;
Run Code Online (Sandbox Code Playgroud)

杰弗里