|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the Qt Mobility Components. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 #include "testrunner.h" |
|
43 #include <QtTest/QtTest> |
|
44 #include <QDir> |
|
45 #include <stdio.h> |
|
46 |
|
47 const char testFunctionElement[] = "TestFunction"; |
|
48 const char incidentElement[] = "Incident"; |
|
49 const char descriptionElement[] = "Description"; |
|
50 const char nameAttr[] = "name"; |
|
51 const char typeAttr[] = "type"; |
|
52 const char fileAttr[] = "file"; |
|
53 const char lineAttr[] = "line"; |
|
54 const char attrValueFail[] = "fail"; |
|
55 |
|
56 |
|
57 TestRunner::TestRunner(const QString& name) |
|
58 : mTestCount(0), |
|
59 mParsingIncidentElement(false), |
|
60 mParsingDescriptionElement(false), |
|
61 mCurrentTestFailed(false), |
|
62 mCurrentTestFailureLine(0) |
|
63 { |
|
64 mTestRunParams.append(name); |
|
65 mTestRunParams.append("-xml"); |
|
66 mTestRunParams.append("-o"); |
|
67 mHomeDir = QDir::homePath(); |
|
68 mTestRunParams.append(QString()); // Initial result file name |
|
69 |
|
70 if (!mHomeDir.endsWith(QString::fromAscii("/"))) |
|
71 mHomeDir += QString::fromAscii("/"); |
|
72 } |
|
73 |
|
74 TestRunner::~TestRunner() |
|
75 { |
|
76 } |
|
77 |
|
78 int TestRunner::runTests(QObject& testObject) |
|
79 { |
|
80 QString className(testObject.metaObject()->className()); |
|
81 printf("Running tests for %s ... ", className.toUtf8().data()); |
|
82 QString resultFileName = mHomeDir + className + ".xml"; |
|
83 mTestRunParams.replace(mTestRunParams.count()-1,resultFileName); |
|
84 int errorsBefore = mErrors.count(); |
|
85 int error = QTest::qExec(&testObject, mTestRunParams); |
|
86 parse(resultFileName); |
|
87 printf("Failures: %d\n",mErrors.count()-errorsBefore); |
|
88 fflush(stdout); |
|
89 return error; |
|
90 } |
|
91 |
|
92 void TestRunner::printResults() |
|
93 { |
|
94 printf("\nTests executed: %d\n",mTestCount); |
|
95 if (mErrors.count() > 0) { |
|
96 printf("Failures (%d):\n", mErrors.count()); |
|
97 foreach(QString error, mErrors) { |
|
98 printf("\n%s", error.toUtf8().data()); |
|
99 } |
|
100 printf("\n"); |
|
101 } else { |
|
102 printf("All passed.\n\n"); |
|
103 } |
|
104 fflush(stdout); |
|
105 } |
|
106 |
|
107 void TestRunner::parse(const QString& fileName) |
|
108 { |
|
109 QFile file(fileName); |
|
110 QXmlInputSource inputSource(&file); |
|
111 QXmlSimpleReader reader; |
|
112 reader.setContentHandler(this); |
|
113 reader.parse(inputSource); |
|
114 } |
|
115 |
|
116 bool TestRunner::startElement( |
|
117 const QString& /*namespaceURI*/, |
|
118 const QString& /*localName*/, |
|
119 const QString& qName, |
|
120 const QXmlAttributes& atts) |
|
121 { |
|
122 if (qName == QString::fromAscii(testFunctionElement)) { |
|
123 mTestCount++; |
|
124 mCurrentTestName = atts.value(QString::fromAscii(nameAttr)); |
|
125 return true; |
|
126 } |
|
127 if (qName == QString::fromAscii(incidentElement)) { |
|
128 mParsingIncidentElement = true; |
|
129 if (atts.value(QString::fromAscii(typeAttr)) == QString::fromAscii(attrValueFail)) { |
|
130 mCurrentTestFailed = true; |
|
131 mCurrentTestFile = atts.value(QString::fromAscii(fileAttr)); |
|
132 mCurrentTestFailureLine = atts.value(QString::fromAscii(lineAttr)).toInt(); |
|
133 } |
|
134 return true; |
|
135 } |
|
136 mParsingDescriptionElement = |
|
137 (qName == QString::fromAscii(descriptionElement)); |
|
138 return true; |
|
139 } |
|
140 |
|
141 bool TestRunner::endElement( |
|
142 const QString& /*namespaceURI*/, |
|
143 const QString& /*localName*/, |
|
144 const QString& qName) |
|
145 { |
|
146 if (qName == QString::fromAscii(incidentElement)) { |
|
147 mParsingIncidentElement = false; |
|
148 mCurrentTestFailed = false; |
|
149 return true; |
|
150 } |
|
151 if (qName == QString::fromAscii(descriptionElement)) { |
|
152 mParsingDescriptionElement = false; |
|
153 } |
|
154 return true; |
|
155 } |
|
156 |
|
157 bool TestRunner::characters(const QString& ch) |
|
158 { |
|
159 if (mParsingIncidentElement && |
|
160 mParsingDescriptionElement && |
|
161 mCurrentTestFailed) { |
|
162 QByteArray testResult = mCurrentTestName.toAscii() + " failed:\n"; |
|
163 testResult += "File: "; |
|
164 testResult += mCurrentTestFile.toAscii(); |
|
165 testResult += "\n"; |
|
166 testResult += "Line: "; |
|
167 testResult += QByteArray::number(mCurrentTestFailureLine); |
|
168 testResult += "\n"; |
|
169 testResult += "Reason: "; |
|
170 testResult += ch.toAscii(); |
|
171 testResult += "\n"; |
|
172 mErrors.append(QString::fromAscii(testResult.data())); |
|
173 } |
|
174 return true; |
|
175 } |
|
176 |