summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMagne Pettersen Zachrisen <magne.zachrisen@tandberg.com>2010-08-10 19:52:06 (GMT)
committerThiago Macieira <thiago.macieira@nokia.com>2010-08-11 14:22:09 (GMT)
commit56396b4b2b66da3133aff1b3864c550ce892d233 (patch)
treed783766e74ef9914e60a350921745ceef63069b5
parentef0d191e3d138f695a39128b6040d0884821f5b5 (diff)
downloadQt-56396b4b2b66da3133aff1b3864c550ce892d233.zip
Qt-56396b4b2b66da3133aff1b3864c550ce892d233.tar.gz
Qt-56396b4b2b66da3133aff1b3864c550ce892d233.tar.bz2
Added -random option to tests, making the test cases within a test execute in arbitrary order. Very useful for avoiding test cases being dependent on the running order.
Added -seed option -random to make it possible to reroduce test results. The seed is printed out when -random is specified Added selftests for -random and -seed options Changed int parsing into strtol and QTime->QDateTime as suggested by Mr Macieira, fixed selftests Merge-request: 2362 Reviewed-by: Thiago Macieira <thiago.macieira@nokia.com>
-rw-r--r--src/testlib/qabstracttestlogger_p.h2
-rw-r--r--src/testlib/qplaintestlogger.cpp22
-rw-r--r--src/testlib/qplaintestlogger_p.h4
-rw-r--r--src/testlib/qtestcase.cpp157
-rw-r--r--src/testlib/qtestlightxmlstreamer.cpp8
-rw-r--r--src/testlib/qtestlog.cpp55
-rw-r--r--src/testlib/qtestlog_p.h2
-rw-r--r--src/testlib/qtestlogger.cpp27
-rw-r--r--src/testlib/qtestlogger_p.h5
-rw-r--r--src/testlib/qtestxmlstreamer.cpp10
-rw-r--r--src/testlib/qxmltestlogger.cpp27
-rw-r--r--src/testlib/qxmltestlogger_p.h4
-rw-r--r--src/testlib/testlib.pro1
-rw-r--r--tests/auto/selftests/expected_random.lightxml20
-rw-r--r--tests/auto/selftests/expected_random.txt9
-rw-r--r--tests/auto/selftests/expected_random.xml23
-rw-r--r--tests/auto/selftests/expected_random.xunitxml14
-rw-r--r--tests/auto/selftests/random/random.pro9
-rw-r--r--tests/auto/selftests/random/tst_random.cpp28
-rw-r--r--tests/auto/selftests/selftests.pro2
-rw-r--r--tests/auto/selftests/selftests.qrc6
-rw-r--r--tests/auto/selftests/tst_selftests.cpp5
22 files changed, 368 insertions, 72 deletions
diff --git a/src/testlib/qabstracttestlogger_p.h b/src/testlib/qabstracttestlogger_p.h
index c84f327..a996e88 100644
--- a/src/testlib/qabstracttestlogger_p.h
+++ b/src/testlib/qabstracttestlogger_p.h
@@ -95,6 +95,8 @@ public:
virtual void addMessage(MessageTypes type, const char *message,
const char *file = 0, int line = 0) = 0;
+ virtual void registerRandomSeed(unsigned int seed) = 0;
+
static void outputString(const char *msg);
static bool isTtyOutput();
};
diff --git a/src/testlib/qplaintestlogger.cpp b/src/testlib/qplaintestlogger.cpp
index 1a0e737..59248a9 100644
--- a/src/testlib/qplaintestlogger.cpp
+++ b/src/testlib/qplaintestlogger.cpp
@@ -384,6 +384,7 @@ namespace QTest {
}
QPlainTestLogger::QPlainTestLogger()
+: randomSeed(9), hasRandomSeed(false)
{
#if defined(Q_OS_WIN) && !defined(Q_OS_WINCE)
InitializeCriticalSection(&QTest::outputCriticalSection);
@@ -415,10 +416,17 @@ void QPlainTestLogger::startLogging()
QTest::qt_snprintf(buf, sizeof(buf), "Testing %s\n",
QTestResult::currentTestObjectName());
} else {
- QTest::qt_snprintf(buf, sizeof(buf),
- "********* Start testing of %s *********\n"
- "Config: Using QTest library " QTEST_VERSION_STR
- ", Qt %s\n", QTestResult::currentTestObjectName(), qVersion());
+ if (hasRandomSeed) {
+ QTest::qt_snprintf(buf, sizeof(buf),
+ "********* Start testing of %s *********\n"
+ "Config: Using QTest library " QTEST_VERSION_STR
+ ", Qt %s, Random seed %d\n", QTestResult::currentTestObjectName(), qVersion(), randomSeed);
+ } else {
+ QTest::qt_snprintf(buf, sizeof(buf),
+ "********* Start testing of %s *********\n"
+ "Config: Using QTest library " QTEST_VERSION_STR
+ ", Qt %s\n", QTestResult::currentTestObjectName(), qVersion());
+ }
}
QTest::outputMessage(buf);
}
@@ -480,4 +488,10 @@ void QPlainTestLogger::addMessage(MessageTypes type, const char *message,
QTest::printMessage(QTest::messageType2String(type), message, file, line);
}
+void QPlainTestLogger::registerRandomSeed(unsigned int seed)
+{
+ randomSeed = seed;
+ hasRandomSeed = true;
+}
+
QT_END_NAMESPACE
diff --git a/src/testlib/qplaintestlogger_p.h b/src/testlib/qplaintestlogger_p.h
index f1f1d4e..9195600 100644
--- a/src/testlib/qplaintestlogger_p.h
+++ b/src/testlib/qplaintestlogger_p.h
@@ -75,6 +75,10 @@ public:
void addMessage(MessageTypes type, const char *message,
const char *file = 0, int line = 0);
+ void registerRandomSeed(unsigned int seed);
+private:
+ unsigned int randomSeed;
+ bool hasRandomSeed;
};
QT_END_NAMESPACE
diff --git a/src/testlib/qtestcase.cpp b/src/testlib/qtestcase.cpp
index 17f1a6b..5934850 100644
--- a/src/testlib/qtestcase.cpp
+++ b/src/testlib/qtestcase.cpp
@@ -826,25 +826,46 @@ namespace QTest
{
static QObject *currentTestObject = 0;
- static struct TestFunction {
- TestFunction():function(0), data(0) {}
- ~TestFunction() { delete [] data; }
- int function;
- char *data;
- } *testFuncs;
-
+ class TestFunction {
+ public:
+ TestFunction() : function_(-1), data_(0) {}
+ void set(int function, char *data) { function_ = function; data_ = data; }
+ char *data() const { return data_; }
+ int function() const { return function_; }
+ ~TestFunction() { delete[] data_; }
+ private:
+ int function_;
+ char *data_;
+ };
/**
- * Contains the count of test functions that was supplied
- * on the command line, if any. Hence, if lastTestFuncIdx is
- * more than zero, those functions should be run instead of
+ * Contains the list of test functions that was supplied
+ * on the command line, if any. Hence, if not empty,
+ * those functions should be run instead of
* all appearing in the test case.
*/
- static int lastTestFuncIdx = -1;
+ static TestFunction * testFuncs = 0;
+ static int testFuncCount = 0;
+
+ /** Don't leak testFuncs on exit even on error */
+ static struct TestFuncCleanup
+ {
+ void cleanup()
+ {
+ delete[] testFuncs;
+ testFuncCount = 0;
+ testFuncs = 0;
+ }
+
+ ~TestFuncCleanup() { cleanup(); }
+ } testFuncCleaner;
static int keyDelay = -1;
static int mouseDelay = -1;
static int eventDelay = -1;
+ static bool randomOrder = false;
static int keyVerbose = -1;
+ static unsigned int seed = 0;
+ static bool seedSet = false;
#if defined(Q_OS_UNIX) && !defined(Q_OS_SYMBIAN)
static bool noCrashHandler = false;
#endif
@@ -930,6 +951,41 @@ int Q_TESTLIB_EXPORT defaultKeyDelay()
return keyDelay;
}
+void seedRandom()
+{
+ static bool randomSeeded = false;
+ if (!randomSeeded) {
+ if (!QTest::seedSet) {
+ QTest::seed = QDateTime::currentDateTime().toTime_t();
+ }
+ qsrand(QTest::seed);
+ randomSeeded = true;
+ }
+}
+
+int qTestRandomSeed()
+{
+ Q_ASSERT(QTest::seedSet);
+ return QTest::seed;
+}
+
+template<typename T>
+void swap(T * array, int pos, int otherPos)
+{
+ T tmp = array[pos];
+ array[pos] = array[otherPos];
+ array[otherPos] = tmp;
+}
+
+template<typename T>
+static void randomizeList(T * array, int size)
+{
+ for (int i = 0; i != size; i++) {
+ int pos = qrand() % size;
+ swap(array, pos, i);
+ }
+}
+
static bool isValidSlot(const QMetaMethod &sl)
{
if (sl.access() != QMetaMethod::Private || !sl.parameterTypes().isEmpty()
@@ -971,8 +1027,6 @@ static int qToInt(char *str)
static void qParseArgs(int argc, char *argv[])
{
- lastTestFuncIdx = -1;
-
const char *testOptions =
" options:\n"
" -functions : Returns a list of current testfunctions\n"
@@ -985,6 +1039,9 @@ static void qParseArgs(int argc, char *argv[])
" -v1 : Print enter messages for each testfunction\n"
" -v2 : Also print out each QVERIFY/QCOMPARE/QTEST\n"
" -vs : Print every signal emitted\n"
+ " -random : Run testcases within each test in random order\n"
+ " -seed n : Positive integer to be used as seed for -random. If not specified,\n"
+ " the current time will be used as seed.\n"
" -eventdelay ms : Set default delay for mouse and keyboard simulation to ms milliseconds\n"
" -keydelay ms : Set default delay for keyboard simulation to ms milliseconds\n"
" -mousedelay ms : Set default delay for mouse simulation to ms milliseconds\n"
@@ -1100,6 +1157,22 @@ static void qParseArgs(int argc, char *argv[])
#endif
} else if (strcmp(argv[i], "-eventcounter") == 0) {
QBenchmarkGlobalData::current->setMode(QBenchmarkGlobalData::EventCounter);
+ } else if (strcmp(argv[i], "-random") == 0) {
+ QTest::randomOrder = true;
+ } else if (strcmp(argv[i], "-seed") == 0) {
+ bool argumentOk = false;
+ if (i + 1 < argc) {
+ char * endpt = 0;
+ long longSeed = strtol(argv[++i], &endpt, 10);
+ argumentOk = (*endpt == '\0' && longSeed >= 0);
+ QTest::seed = longSeed;
+ }
+ if (!argumentOk) {
+ printf("-seed needs an extra positive integer parameter to specify the seed\n");
+ exit(1);
+ } else {
+ QTest::seedSet = true;
+ }
} else if (strcmp(argv[i], "-minimumvalue") == 0) {
if (i + 1 >= argc) {
printf("-minimumvalue needs an extra parameter to indicate the minimum time(ms)\n");
@@ -1140,6 +1213,10 @@ static void qParseArgs(int argc, char *argv[])
printf("Unknown option: '%s'\n\n%s", argv[i], testOptions);
exit(1);
} else {
+ if (!QTest::testFuncs) {
+ QTest::testFuncs = new QTest::TestFunction[512];
+ }
+
int colon = -1;
char buf[512], *data=0;
int off;
@@ -1161,17 +1238,16 @@ static void qParseArgs(int argc, char *argv[])
qPrintTestSlots();
exit(1);
}
- ++QTest::lastTestFuncIdx;
- if (!QTest::testFuncs) {
- struct Cleanup { ~Cleanup() { delete[] QTest::testFuncs; } };
- static Cleanup cleanup;
- QTest::testFuncs = new TestFunction[512];
- }
- QTest::testFuncs[QTest::lastTestFuncIdx].function = idx;
- QTest::testFuncs[QTest::lastTestFuncIdx].data = data;
- QTEST_ASSERT(QTest::lastTestFuncIdx < 512);
+ testFuncs[testFuncCount].set(idx, data);
+ testFuncCount++;
+ QTEST_ASSERT(QTest::testFuncCount < 512);
}
}
+
+ if (QTest::seedSet && !QTest::randomOrder) {
+ printf("-seed requires -random\n");
+ exit(1);
+ }
}
QBenchmarkResult qMedian(const QList<QBenchmarkResult> &container)
@@ -1466,9 +1542,11 @@ static void qInvokeTestMethods(QObject *testObject)
{
const QMetaObject *metaObject = testObject->metaObject();
QTEST_ASSERT(metaObject);
-
- QTestLog::startLogging();
-
+ if (QTest::randomOrder) {
+ QTestLog::startLogging(QTest::seed);
+ } else {
+ QTestLog::startLogging();
+ }
QTestResult::setCurrentTestFunction("initTestCase");
QTestResult::setCurrentTestLocation(QTestResult::DataFunc);
QTestTable::globalTestTable();
@@ -1484,21 +1562,31 @@ static void qInvokeTestMethods(QObject *testObject)
if(!QTestResult::skipCurrentTest() && !previousFailed) {
- if (lastTestFuncIdx >= 0) {
- for (int i = 0; i <= lastTestFuncIdx; ++i) {
- if (!qInvokeTestMethod(metaObject->method(testFuncs[i].function).signature(),
- testFuncs[i].data))
+ if (QTest::testFuncs) {
+ if (QTest::randomOrder)
+ randomizeList(QTest::testFuncs, QTest::testFuncCount);
+ for (int i = 0; i != QTest::testFuncCount; i++) {
+ if (!qInvokeTestMethod(metaObject->method(QTest::testFuncs[i].function()).signature(),
+ QTest::testFuncs[i].data())) {
break;
+ }
}
+ testFuncCleaner.cleanup();
} else {
int methodCount = metaObject->methodCount();
- for (int i = 0; i < methodCount; ++i) {
- QMetaMethod slotMethod = metaObject->method(i);
- if (!isValidSlot(slotMethod))
+ QMetaMethod *testMethods = new QMetaMethod[methodCount];
+ for (int i = 0; i != methodCount; i++)
+ testMethods[i] = metaObject->method(i);
+ if (QTest::randomOrder)
+ randomizeList(testMethods, methodCount);
+ for (int i = 0; i != methodCount; i++) {
+ if (!isValidSlot(testMethods[i]))
continue;
- if (!qInvokeTestMethod(slotMethod.signature()))
+ if (!qInvokeTestMethod(testMethods[i].signature()))
break;
}
+ delete[] testMethods;
+ testMethods = 0;
}
}
@@ -1689,6 +1777,9 @@ int QTest::qExec(QObject *testObject, int argc, char **argv)
QTestResult::setCurrentTestObject(metaObject->className());
qParseArgs(argc, argv);
+ if (QTest::randomOrder) {
+ seedRandom();
+ }
#ifdef QTESTLIB_USE_VALGRIND
if (QBenchmarkGlobalData::current->mode() == QBenchmarkGlobalData::CallgrindParentProcess) {
const QStringList origAppArgs(QCoreApplication::arguments());
diff --git a/src/testlib/qtestlightxmlstreamer.cpp b/src/testlib/qtestlightxmlstreamer.cpp
index 0ac9ea8..cc5397a 100644
--- a/src/testlib/qtestlightxmlstreamer.cpp
+++ b/src/testlib/qtestlightxmlstreamer.cpp
@@ -42,6 +42,7 @@
#include "qtestlightxmlstreamer.h"
#include "qtestelement.h"
#include "qtestelementattribute.h"
+#include "qtestlogger_p.h"
#include "QtTest/private/qtestlog_p.h"
#include "QtTest/private/qtestresult_p.h"
@@ -164,8 +165,13 @@ void QTestLightXmlStreamer::formatBeforeAttributes(const QTestElement *element,
void QTestLightXmlStreamer::output(QTestElement *element) const
{
QTestCharBuffer buf;
- QTest::qt_asprintf(&buf, "<Environment>\n <QtVersion>%s</QtVersion>\n <QTestVersion>%s</QTestVersion>\n",
+ if (logger()->hasRandomSeed()) {
+ QTest::qt_asprintf(&buf, "<Environment>\n <QtVersion>%s</QtVersion>\n <QTestVersion>%s</QTestVersion>\n <RandomSeed>%d</RandomSeed>\n",
+ qVersion(), QTEST_VERSION_STR, logger()->randomSeed() );
+ } else {
+ QTest::qt_asprintf(&buf, "<Environment>\n <QtVersion>%s</QtVersion>\n <QTestVersion>%s</QTestVersion>\n",
qVersion(), QTEST_VERSION_STR );
+ }
outputString(buf.constData());
QTest::qt_asprintf(&buf, "</Environment>\n");
diff --git a/src/testlib/qtestlog.cpp b/src/testlib/qtestlog.cpp
index 398dec5..d58e231 100644
--- a/src/testlib/qtestlog.cpp
+++ b/src/testlib/qtestlog.cpp
@@ -168,6 +168,29 @@ namespace QTest {
}
}
+void initLogger()
+{
+ switch (QTest::logMode) {
+ case QTestLog::Plain:
+ QTest::testLogger = new QPlainTestLogger;
+ break;
+ case QTestLog::XML:{
+ if(QTest::flushMode == QTestLog::FLushOn)
+ QTest::testLogger = new QXmlTestLogger(QXmlTestLogger::Complete);
+ else
+ QTest::testLogger = new QTestLogger(QTestLogger::TLF_XML);
+ break;
+ }case QTestLog::LightXML:{
+ if(QTest::flushMode == QTestLog::FLushOn)
+ QTest::testLogger = new QXmlTestLogger(QXmlTestLogger::Light);
+ else
+ QTest::testLogger = new QTestLogger(QTestLogger::TLF_LightXml);
+ break;
+ }case QTestLog::XunitXML:
+ QTest::testLogger = new QTestLogger(QTestLogger::TLF_XunitXml);
+ }
+}
+
}
QTestLog::QTestLog()
@@ -268,32 +291,20 @@ void QTestLog::addBenchmarkResult(const QBenchmarkResult &result)
QTest::testLogger->addBenchmarkResult(result);
}
-void QTestLog::startLogging()
+void QTestLog::startLogging(unsigned int randomSeed)
{
QTEST_ASSERT(!QTest::testLogger);
-
- switch (QTest::logMode) {
- case QTestLog::Plain:
- QTest::testLogger = new QPlainTestLogger;
- break;
- case QTestLog::XML:{
- if(QTest::flushMode == QTestLog::FLushOn)
- QTest::testLogger = new QXmlTestLogger(QXmlTestLogger::Complete);
- else
- QTest::testLogger = new QTestLogger(QTestLogger::TLF_XML);
- break;
- }case QTestLog::LightXML:{
- if(QTest::flushMode == QTestLog::FLushOn)
- QTest::testLogger = new QXmlTestLogger(QXmlTestLogger::Light);
- else
- QTest::testLogger = new QTestLogger(QTestLogger::TLF_LightXml);
- break;
- }case QTestLog::XunitXML:
- QTest::testLogger = new QTestLogger(QTestLogger::TLF_XunitXml);
- }
-
+ QTest::initLogger();
+ QTest::testLogger->registerRandomSeed(randomSeed);
QTest::testLogger->startLogging();
+ QTest::oldMessageHandler = qInstallMsgHandler(QTest::messageHandler);
+}
+void QTestLog::startLogging()
+{
+ QTEST_ASSERT(!QTest::testLogger);
+ QTest::initLogger();
+ QTest::testLogger->startLogging();
QTest::oldMessageHandler = qInstallMsgHandler(QTest::messageHandler);
}
diff --git a/src/testlib/qtestlog_p.h b/src/testlib/qtestlog_p.h
index d36ff04..01d39eb 100644
--- a/src/testlib/qtestlog_p.h
+++ b/src/testlib/qtestlog_p.h
@@ -83,6 +83,7 @@ public:
static void info(const char *msg, const char *file, int line);
static void startLogging();
+ static void startLogging(unsigned int randomSeed);
static void stopLogging();
static void setLogMode(LogMode mode);
@@ -97,7 +98,6 @@ public:
static void setMaxWarnings(int max);
static void setFlushMode(FlushMode mode);
-
private:
QTestLog();
~QTestLog();
diff --git a/src/testlib/qtestlogger.cpp b/src/testlib/qtestlogger.cpp
index f0be6be..cab97d1 100644
--- a/src/testlib/qtestlogger.cpp
+++ b/src/testlib/qtestlogger.cpp
@@ -62,7 +62,8 @@ QTestLogger::QTestLogger(int fm)
warningCounter(0), skipCounter(0),
systemCounter(0), qdebugCounter(0),
qwarnCounter(0), qfatalCounter(0),
- infoCounter(0)
+ infoCounter(0), randomSeed_(0),
+ hasRandomSeed_(false)
{
}
@@ -133,6 +134,14 @@ void QTestLogger::stopLogging()
property->addAttribute(QTest::AI_PropertyValue, qVersion());
properties->addLogElement(property);
+ if (hasRandomSeed()) {
+ property = new QTestElement(QTest::LET_Property);
+ property->addAttribute(QTest::AI_Name, "RandomSeed");
+ QTest::qt_snprintf(buf, sizeof(buf), "%i", randomSeed());
+ property->addAttribute(QTest::AI_PropertyValue, buf);
+ properties->addLogElement(property);
+ }
+
currentLogElement->addLogElement(properties);
currentLogElement->addLogElement(iterator);
@@ -420,5 +429,21 @@ int QTestLogger::infoCount() const
return infoCounter;
}
+void QTestLogger::registerRandomSeed(unsigned int seed)
+{
+ randomSeed_ = seed;
+ hasRandomSeed_ = true;
+}
+
+unsigned int QTestLogger::randomSeed() const
+{
+ return randomSeed_;
+}
+
+bool QTestLogger::hasRandomSeed() const
+{
+ return hasRandomSeed_;
+}
+
QT_END_NAMESPACE
diff --git a/src/testlib/qtestlogger_p.h b/src/testlib/qtestlogger_p.h
index bb7a358..8932a85 100644
--- a/src/testlib/qtestlogger_p.h
+++ b/src/testlib/qtestlogger_p.h
@@ -101,6 +101,9 @@ class QTestLogger : public QAbstractTestLogger
int qwarnCount() const;
int qfatalCount() const;
int infoCount() const;
+ void registerRandomSeed(unsigned int seed);
+ unsigned int randomSeed() const;
+ bool hasRandomSeed() const;
private:
QTestElement *listOfTestcases;
@@ -121,6 +124,8 @@ class QTestLogger : public QAbstractTestLogger
int qwarnCounter;
int qfatalCounter;
int infoCounter;
+ unsigned int randomSeed_;
+ bool hasRandomSeed_;
};
QT_END_NAMESPACE
diff --git a/src/testlib/qtestxmlstreamer.cpp b/src/testlib/qtestxmlstreamer.cpp
index f63c221..a6b8376 100644
--- a/src/testlib/qtestxmlstreamer.cpp
+++ b/src/testlib/qtestxmlstreamer.cpp
@@ -42,6 +42,7 @@
#include "qtestxmlstreamer.h"
#include "qtestelement.h"
#include "qtestelementattribute.h"
+#include "qtestlogger_p.h"
#include "QtTest/private/qtestlog_p.h"
#include "QtTest/private/qtestresult_p.h"
@@ -204,8 +205,13 @@ void QTestXmlStreamer::output(QTestElement *element) const
quotedTc.constData());
outputString(buf.constData());
- QTest::qt_asprintf(&buf, "<Environment>\n <QtVersion>%s</QtVersion>\n <QTestVersion>%s</QTestVersion>\n",
- qVersion(), QTEST_VERSION_STR );
+ if (logger()->hasRandomSeed()) {
+ QTest::qt_asprintf(&buf, "<Environment>\n <QtVersion>%s</QtVersion>\n <QTestVersion>%s</QTestVersion>\n <RandomSeed>%d</RandomSeed>\n",
+ qVersion(), QTEST_VERSION_STR, logger()->randomSeed() );
+ } else {
+ QTest::qt_asprintf(&buf, "<Environment>\n <QtVersion>%s</QtVersion>\n <QTestVersion>%s</QTestVersion>\n",
+ qVersion(), QTEST_VERSION_STR );
+ }
outputString(buf.constData());
QTest::qt_asprintf(&buf, "</Environment>\n");
diff --git a/src/testlib/qxmltestlogger.cpp b/src/testlib/qxmltestlogger.cpp
index 07e8ef0..2bf7d77 100644
--- a/src/testlib/qxmltestlogger.cpp
+++ b/src/testlib/qxmltestlogger.cpp
@@ -93,7 +93,7 @@ namespace QTest {
QXmlTestLogger::QXmlTestLogger(XmlMode mode )
- :xmlmode(mode)
+ :xmlmode(mode), randomSeed(0), hasRandomSeed(false)
{
}
@@ -116,11 +116,20 @@ void QXmlTestLogger::startLogging()
outputString(buf.constData());
}
- QTest::qt_asprintf(&buf,
- "<Environment>\n"
- " <QtVersion>%s</QtVersion>\n"
- " <QTestVersion>"QTEST_VERSION_STR"</QTestVersion>\n"
- "</Environment>\n", qVersion());
+ if (hasRandomSeed) {
+ QTest::qt_asprintf(&buf,
+ "<Environment>\n"
+ " <QtVersion>%s</QtVersion>\n"
+ " <QTestVersion>"QTEST_VERSION_STR"</QTestVersion>\n"
+ " <RandomSeed>%d</RandomSeed>\n"
+ "</Environment>\n", qVersion(), randomSeed);
+ } else {
+ QTest::qt_asprintf(&buf,
+ "<Environment>\n"
+ " <QtVersion>%s</QtVersion>\n"
+ " <QTestVersion>"QTEST_VERSION_STR"</QTestVersion>\n"
+ "</Environment>\n", qVersion());
+ }
outputString(buf.constData());
}
@@ -441,4 +450,10 @@ int QXmlTestLogger::xmlCdata(QTestCharBuffer* str, char const* src)
return allocateStringFn(str, src, QXmlTestLogger::xmlCdata);
}
+void QXmlTestLogger::registerRandomSeed(unsigned int seed)
+{
+ randomSeed = seed;
+ hasRandomSeed = true;
+}
+
QT_END_NAMESPACE
diff --git a/src/testlib/qxmltestlogger_p.h b/src/testlib/qxmltestlogger_p.h
index ae918dc..82dd8a6 100644
--- a/src/testlib/qxmltestlogger_p.h
+++ b/src/testlib/qxmltestlogger_p.h
@@ -79,6 +79,8 @@ public:
void addMessage(MessageTypes type, const char *message,
const char *file = 0, int line = 0);
+ void registerRandomSeed(unsigned int seed);
+
static int xmlCdata(QTestCharBuffer *dest, char const* src);
static int xmlQuote(QTestCharBuffer *dest, char const* src);
static int xmlCdata(QTestCharBuffer *dest, char const* src, size_t n);
@@ -86,6 +88,8 @@ public:
private:
XmlMode xmlmode;
+ unsigned int randomSeed;
+ bool hasRandomSeed;
};
QT_END_NAMESPACE
diff --git a/src/testlib/testlib.pro b/src/testlib/testlib.pro
index a8186d8..6bb0492 100644
--- a/src/testlib/testlib.pro
+++ b/src/testlib/testlib.pro
@@ -1,6 +1,7 @@
TARGET = QtTest
QPRO_PWD = $$PWD
QT = core
+CONFIG += debug
INCLUDEPATH += .
unix:!embedded:QMAKE_PKGCONFIG_DESCRIPTION = Qt \
Unit \
diff --git a/tests/auto/selftests/expected_random.lightxml b/tests/auto/selftests/expected_random.lightxml
new file mode 100644
index 0000000..15f56a1
--- /dev/null
+++ b/tests/auto/selftests/expected_random.lightxml
@@ -0,0 +1,20 @@
+<Environment>
+ <QtVersion>@INSERT_QT_VERSION_HERE@</QtVersion>
+ <QTestVersion>@INSERT_QT_VERSION_HERE@</QTestVersion>
+ <RandomSeed>325</RandomSeed>
+</Environment>
+<TestFunction name="initTestCase">
+<Incident type="pass" file="" line="0"/>
+</TestFunction>
+<TestFunction name="testTwo">
+<Incident type="pass" file="" line="0"/>
+</TestFunction>
+<TestFunction name="testOne">
+<Incident type="pass" file="" line="0"/>
+</TestFunction>
+<TestFunction name="testThree">
+<Incident type="pass" file="" line="0"/>
+</TestFunction>
+<TestFunction name="cleanupTestCase">
+<Incident type="pass" file="" line="0"/>
+</TestFunction>
diff --git a/tests/auto/selftests/expected_random.txt b/tests/auto/selftests/expected_random.txt
new file mode 100644
index 0000000..3f4c11d
--- /dev/null
+++ b/tests/auto/selftests/expected_random.txt
@@ -0,0 +1,9 @@
+********* Start testing of tst_Random *********
+Config: Using QTest library @INSERT_QT_VERSION_HERE@, Qt @INSERT_QT_VERSION_HERE@, Random seed 325
+PASS : tst_Random::initTestCase()
+PASS : tst_Random::testTwo()
+PASS : tst_Random::testOne()
+PASS : tst_Random::testThree()
+PASS : tst_Random::cleanupTestCase()
+Totals: 5 passed, 0 failed, 0 skipped
+********* Finished testing of tst_Random *********
diff --git a/tests/auto/selftests/expected_random.xml b/tests/auto/selftests/expected_random.xml
new file mode 100644
index 0000000..3677287
--- /dev/null
+++ b/tests/auto/selftests/expected_random.xml
@@ -0,0 +1,23 @@
+<?xml version="1.0" encoding="ISO-8859-1"?>
+<TestCase name="tst_Random">
+<Environment>
+ <QtVersion>@INSERT_QT_VERSION_HERE@</QtVersion>
+ <QTestVersion>@INSERT_QT_VERSION_HERE@</QTestVersion>
+ <RandomSeed>325</RandomSeed>
+</Environment>
+<TestFunction name="initTestCase">
+<Incident type="pass" file="" line="0" />
+</TestFunction>
+<TestFunction name="testTwo">
+<Incident type="pass" file="" line="0" />
+</TestFunction>
+<TestFunction name="testOne">
+<Incident type="pass" file="" line="0" />
+</TestFunction>
+<TestFunction name="testThree">
+<Incident type="pass" file="" line="0" />
+</TestFunction>
+<TestFunction name="cleanupTestCase">
+<Incident type="pass" file="" line="0" />
+</TestFunction>
+</TestCase>
diff --git a/tests/auto/selftests/expected_random.xunitxml b/tests/auto/selftests/expected_random.xunitxml
new file mode 100644
index 0000000..262aa0a
--- /dev/null
+++ b/tests/auto/selftests/expected_random.xunitxml
@@ -0,0 +1,14 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<testsuite errors="0" failures="0" tests="5" name="tst_Random">
+ <properties>
+ <property value="@INSERT_QT_VERSION_HERE@" name="QTestVersion"/>
+ <property value="@INSERT_QT_VERSION_HERE@" name="QtVersion"/>
+ <property value="325" name="RandomSeed"/>
+ </properties>
+ <testcase result="pass" name="initTestCase"/>
+ <testcase result="pass" name="testTwo"/>
+ <testcase result="pass" name="testOne"/>
+ <testcase result="pass" name="testThree"/>
+ <testcase result="pass" name="cleanupTestCase"/>
+ <system-err/>
+</testsuite>
diff --git a/tests/auto/selftests/random/random.pro b/tests/auto/selftests/random/random.pro
new file mode 100644
index 0000000..19572ad
--- /dev/null
+++ b/tests/auto/selftests/random/random.pro
@@ -0,0 +1,9 @@
+load(qttest_p4)
+SOURCES += tst_random.cpp
+QT = core
+
+mac:CONFIG -= app_bundle
+CONFIG -= debug_and_release_target
+
+
+TARGET = random
diff --git a/tests/auto/selftests/random/tst_random.cpp b/tests/auto/selftests/random/tst_random.cpp
new file mode 100644
index 0000000..1ee922e
--- /dev/null
+++ b/tests/auto/selftests/random/tst_random.cpp
@@ -0,0 +1,28 @@
+#include <QtCore>
+#include <QtTest/QtTest>
+
+class tst_Random: public QObject
+{
+ Q_OBJECT
+
+private slots:
+ void testOne() const {}
+ void testTwo() const {}
+ void testThree() const {}
+};
+
+/*
+int main(int argc, char * argv[])
+{
+ int failures = 0;
+ tst_Random tst;
+ for (int i = 0; i != 10; i++) {
+ failures += QTest::qExec(&tst, argc, argv);
+ }
+ return failures;
+}
+*/
+QTEST_MAIN(tst_Random)
+
+#include "tst_random.moc"
+
diff --git a/tests/auto/selftests/selftests.pro b/tests/auto/selftests/selftests.pro
index d854b5e..bfbd598 100644
--- a/tests/auto/selftests/selftests.pro
+++ b/tests/auto/selftests/selftests.pro
@@ -5,7 +5,7 @@ SUBDIRS = subtest test warnings maxwarnings cmptest globaldata skipglobal skip \
skipinit skipinitdata datetime singleskip assert waitwithoutgui differentexec \
exceptionthrow qexecstringlist datatable commandlinedata\
benchlibwalltime benchlibcallgrind benchlibeventcounter benchlibtickcounter \
- benchliboptions xunit badxml longstring
+ benchliboptions xunit badxml longstring random
INSTALLS =
diff --git a/tests/auto/selftests/selftests.qrc b/tests/auto/selftests/selftests.qrc
index f82722b..ba7ca4f 100644
--- a/tests/auto/selftests/selftests.qrc
+++ b/tests/auto/selftests/selftests.qrc
@@ -90,6 +90,10 @@
<file>expected_multiexec.xml</file>
<file>expected_multiexec.xunitxml</file>
<file>expected_qexecstringlist.txt</file>
+ <file>expected_random.txt</file>
+ <file>expected_random.lightxml</file>
+ <file>expected_random.xml</file>
+ <file>expected_random.xunitxml</file>
<file>expected_singleskip.lightxml</file>
<file>expected_singleskip.txt</file>
<file>expected_singleskip.xml</file>
@@ -134,5 +138,5 @@
<file>expected_xunit.txt</file>
<file>expected_xunit.xml</file>
<file>expected_xunit.xunitxml</file>
- </qresource>
+ </qresource>
</RCC>
diff --git a/tests/auto/selftests/tst_selftests.cpp b/tests/auto/selftests/tst_selftests.cpp
index 0818b4c..9fb7ce6 100644
--- a/tests/auto/selftests/tst_selftests.cpp
+++ b/tests/auto/selftests/tst_selftests.cpp
@@ -245,6 +245,7 @@ void tst_Selftests::runSubTest_data()
<< "xunit"
<< "longstring"
<< "badxml"
+ << "random"
;
foreach (Logger const& logger, allLoggers()) {
@@ -273,6 +274,9 @@ void tst_Selftests::runSubTest_data()
else if (subtest == "badxml") {
arguments << "-eventcounter";
}
+ else if (subtest == "random") {
+ arguments << QString("-random -seed 325").split(' ');
+ }
// These tests don't work right with loggers other than plain, usually because
// they internally supply arguments to themselves.
@@ -632,3 +636,4 @@ void tst_Selftests::cleanupTestCase()
QTEST_MAIN(tst_Selftests)
#include "tst_selftests.moc"
+