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

时间:2010-08-23 09:02:08

标签: 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);
}

对于调试,我想写一些像UnitTest::RunSingleTests("MyNewUnitTest");而不是UnitTest::RunAllTests();的东西。 UnitTest ++是否提供了这样的功能?若然,语法是什么?

6 个答案:

答案 0 :(得分: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();
  }
}

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

> myexe MyTestName

或单一套件

> myexe suite MySuite

答案 1 :(得分:7)

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

所以你需要更换

  p = p->next;

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

杰弗里

答案 2 :(得分:2)

如果你告诉它名字,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);
    }
}

答案 3 :(得分:1)

您可以使用predicate的{​​{1}}参数:

来执行此操作
RunTestsIf

如果您没有套件,或者想要搜索所有套件,则可以将TestReporterStdout reporter; TestRunner runner(reporter); return runner.RunTestsIf(Test::GetTestList(), "MySuite", [](Test* t) { return strcmp(t->m_details.testName, "MyTest") == 0; }, 0); 替换为"MySuite"

答案 4 :(得分: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);
}

答案 5 :(得分: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

}