由@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);
}
您需要更多地介紹一下你的環境,你已經嘗試和失敗等 – 2010-08-23 09:04:19
所以你已經知道如何運行兩個測試然後? – 2010-08-23 09:08:40
我正在運行UnitTest ++。我的主要功能如下: int main(){ \t \t printf(「diamond test v0.1%s \ n \ n」,__ TIMESTAMP__); \t diamond :: startup(); \t \t UnitTest :: RunAllTests(); \t \t diamond :: shutdown(); \t \t printf(「按任意鍵繼續...」); \t getc(stdin); \t \t } 調試我會像寫 \t單元測試:: RunSingleTests( 「MyNewUnitTest」); 而不是 \t UnitTest :: RunAllTests(); 。我想知道是否有這種類型的函數,如果是,語法如何。 – Arno 2010-08-23 09:08:52