@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);
}