Как запустить один тест с UnitTest ++? - PullRequest
11 голосов
/ 23 августа 2010

Как запустить один тест с 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 ]

13 голосов
/ 23 августа 2010

попробуйте это как 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
7 голосов
/ 09 февраля 2011

Это почти правильно.«Тест» фактически задуман как узел в связанном списке, поэтому при добавлении его в новый список необходимо исправить указатель, чтобы избежать включения большего количества тестов, чем вы предполагали.

  p = p->next;

с

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

Джеффри

2 голосов
/ 17 мая 2012

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);
    }
}
1 голос
/ 04 сентября 2014

Ответ, данный @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);
}
1 голос
/ 30 июня 2013

Вы можете сделать это, используя параметр 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.

0 голосов
/ 07 ноября 2014

Решение в принятом ответе не работает для меня.Когда первый тест набора загружен в 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

}
...