12

如何使用 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);
}

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

4

6 回答 6

14

试试这个作为你的 main() 进行 unittest (我实际上把它放在一个文件中并将它添加到 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();
  }
}

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

> myexe MyTestName

或单人套房

> myexe suite MySuite
于 2010-08-23T10:14:48.237 回答
7

This is almost correct. "Test" actually is purposed as a node in a linked list, so when you add it to a new list, you have to correct the pointer to avoid including more tests than you intended.

So you need to replace

  p = p->next;

with

  Test* q = p;
  p = p->next;
  q->next = NULL;

Geoffrey

于 2011-02-09T15:17:12.013 回答
3

如果您告诉它名称,RunTestsIf 只能运行一个套件。

class MyTrue
{
    public:
        MyTrue(const std::string & suiteName, const std::string & testName)
                : suite(suiteName), test(testName) {}

        bool operator()(const UnitTest::Test* const testCase) const
        {
            return suite.compare(testCase->m_details.suiteName) == 0 && 
                         test.compare(testCase->m_details.testName) == 0;
        }
    private:
        std::string suite;
        std::string test;
};

int main (...) {
    bool isSuite = false;
    std::string suiteName = "suite01";
    std::string testName  = "test01";

    UnitTest::TestReporterStdout reporter;
    UnitTest::TestRunner runner(reporter);
    if (isSuite) {
        runner.RunTestsIf(UnitTest::Test::GetTestList(),
            NULL, MyTrue(suiteName, testName), 0);
    } else {
        runner.RunTestsIf(UnitTest::Test::GetTestList(),
            NULL, UnitTest::True(), 0);
    }
}
于 2012-05-16T22:35:43.900 回答
1

您可以通过使用以下predicate参数来做到这一点RunTestsIf

TestReporterStdout reporter;
TestRunner runner(reporter);
return runner.RunTestsIf(Test::GetTestList(), "MySuite",
    [](Test* t) { 
        return strcmp(t->m_details.testName, "MyTest") == 0; 
    }, 0);

如果您没有套件,或者您想搜索所有套件,您可以替换"MySuite"NULL.

于 2013-06-30T10:20:58.363 回答
1

@stijn 给出的答案在测试列表操作中有一个错误,因此它通常会运行您没有请求的其他测试。

此示例使用谓词函子并利用 RunTestsIf 提供的内置套件名称匹配。它是正确的,而且简单得多。

#include "UnitTest++.h"
#include "TestReporterStdout.h"
#include <string.h>
using namespace UnitTest;

/// Predicate that is true for tests with matching name,
/// or all tests if no names were given.
class Predicate
{
public:

Predicate(const char **tests, int nTests)
    : _tests(tests),
      _nTests(nTests)
{
}

bool operator()(Test *test) const
{
    bool match = (_nTests == 0);
    for (int i = 0; !match && i < _nTests; ++i) {
        if (!strcmp(test->m_details.testName, _tests[i])) {
            match = true;
        }
    }
    return match;
}

private:
    const char **_tests;
    int _nTests;
};

int main(int argc, const char** argv)
{
    const char *suiteName = 0;
    int arg = 1;

    // Optional "suite" arg must be followed by a suite name.
    if (argc >=3 && strcmp("suite", argv[arg]) == 0) {
        suiteName = argv[++arg];
        ++arg;
    } 

    // Construct predicate that matches any tests given on command line.
    Predicate pred(argv + arg, argc - arg);

    // Run tests that match any given suite and tests.
    TestReporterStdout reporter;
    TestRunner runner(reporter);
    return runner.RunTestsIf(Test::GetTestList(), suiteName, pred, 0);
}
于 2014-09-04T19:44:18.517 回答
0

接受的答案中的解决方案对我不起作用。当套件的第一个测试加载到 p 中时,它不会跳转到下一个测试(不知道确切原因)。

我正在使用 Xcode 和 UnitTest++ v1.4

#include "UnitTest++.h"
#include "TestReporterStdout.h"

#define SUITE_NAME "ActionFeedback"

using namespace UnitTest;

int main( int argc, char** argv )
{
#ifdef SUITE_NAME
    TestReporterStdout reporter;
    TestRunner runner( reporter );
    return runner.RunTestsIf( Test::GetTestList() ,  SUITE_NAME , True(), 0 );
#else
    return RunAllTests();
#endif

}
于 2014-11-07T08:54:17.320 回答