1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 #include "testrunner.h" |
|
19 #include <QtTest/QtTest> |
|
20 #include <QDir> |
|
21 #include <stdio.h> |
|
22 |
|
23 const char testFunctionElement[] = "TestFunction"; |
|
24 const char incidentElement[] = "Incident"; |
|
25 const char descriptionElement[] = "Description"; |
|
26 const char nameAttr[] = "name"; |
|
27 const char typeAttr[] = "type"; |
|
28 const char fileAttr[] = "file"; |
|
29 const char lineAttr[] = "line"; |
|
30 const char attrValueFail[] = "fail"; |
|
31 |
|
32 |
|
33 TestRunner::TestRunner(const QString& name) |
|
34 : mTestCount(0), |
|
35 mParsingIncidentElement(false), |
|
36 mParsingDescriptionElement(false), |
|
37 mCurrentTestFailed(false), |
|
38 mCurrentTestFailureLine(0) |
|
39 { |
|
40 mTestRunParams.append(name); |
|
41 mTestRunParams.append("-xml"); |
|
42 mTestRunParams.append("-o"); |
|
43 mHomeDir = QDir::homePath(); |
|
44 mTestRunParams.append(QString()); // Initial result file name |
|
45 |
|
46 if (!mHomeDir.endsWith(QString::fromAscii("/"))) |
|
47 mHomeDir += QString::fromAscii("/"); |
|
48 } |
|
49 |
|
50 TestRunner::~TestRunner() |
|
51 { |
|
52 } |
|
53 |
|
54 int TestRunner::runTests(QObject& testObject) |
|
55 { |
|
56 QString className(testObject.metaObject()->className()); |
|
57 printf("Running tests for %s ... ", className.toUtf8().data()); |
|
58 QString resultFileName = mHomeDir + className + ".xml"; |
|
59 mTestRunParams.replace(mTestRunParams.count()-1,resultFileName); |
|
60 int errorsBefore = mErrors.count(); |
|
61 int error = QTest::qExec(&testObject, mTestRunParams); |
|
62 parse(resultFileName); |
|
63 printf("Failures: %d\n",mErrors.count()-errorsBefore); |
|
64 fflush(stdout); |
|
65 return error; |
|
66 } |
|
67 |
|
68 void TestRunner::printResults() |
|
69 { |
|
70 printf("\nTests executed: %d\n",mTestCount); |
|
71 if (mErrors.count() > 0) { |
|
72 printf("Failures (%d):\n", mErrors.count()); |
|
73 foreach(QString error, mErrors) { |
|
74 printf("\n%s", error.toUtf8().data()); |
|
75 } |
|
76 printf("\n"); |
|
77 } else { |
|
78 printf("All passed.\n\n"); |
|
79 } |
|
80 fflush(stdout); |
|
81 } |
|
82 |
|
83 void TestRunner::parse(const QString& fileName) |
|
84 { |
|
85 QFile file(fileName); |
|
86 QXmlInputSource inputSource(&file); |
|
87 QXmlSimpleReader reader; |
|
88 reader.setContentHandler(this); |
|
89 reader.parse(inputSource); |
|
90 } |
|
91 |
|
92 bool TestRunner::startElement( |
|
93 const QString& /*namespaceURI*/, |
|
94 const QString& /*localName*/, |
|
95 const QString& qName, |
|
96 const QXmlAttributes& atts) |
|
97 { |
|
98 if (qName == QString::fromAscii(testFunctionElement)) { |
|
99 mTestCount++; |
|
100 mCurrentTestName = atts.value(QString::fromAscii(nameAttr)); |
|
101 return true; |
|
102 } |
|
103 if (qName == QString::fromAscii(incidentElement)) { |
|
104 mParsingIncidentElement = true; |
|
105 if (atts.value(QString::fromAscii(typeAttr)) == QString::fromAscii(attrValueFail)) { |
|
106 mCurrentTestFailed = true; |
|
107 mCurrentTestFile = atts.value(QString::fromAscii(fileAttr)); |
|
108 mCurrentTestFailureLine = atts.value(QString::fromAscii(lineAttr)).toInt(); |
|
109 } |
|
110 return true; |
|
111 } |
|
112 mParsingDescriptionElement = |
|
113 (qName == QString::fromAscii(descriptionElement)); |
|
114 return true; |
|
115 } |
|
116 |
|
117 bool TestRunner::endElement( |
|
118 const QString& /*namespaceURI*/, |
|
119 const QString& /*localName*/, |
|
120 const QString& qName) |
|
121 { |
|
122 if (qName == QString::fromAscii(incidentElement)) { |
|
123 mParsingIncidentElement = false; |
|
124 mCurrentTestFailed = false; |
|
125 return true; |
|
126 } |
|
127 if (qName == QString::fromAscii(descriptionElement)) { |
|
128 mParsingDescriptionElement = false; |
|
129 } |
|
130 return true; |
|
131 } |
|
132 |
|
133 bool TestRunner::characters(const QString& ch) |
|
134 { |
|
135 if (mParsingIncidentElement && |
|
136 mParsingDescriptionElement && |
|
137 mCurrentTestFailed) { |
|
138 QByteArray testResult = mCurrentTestName.toAscii() + " failed:\n"; |
|
139 testResult += "File: "; |
|
140 testResult += mCurrentTestFile.toAscii(); |
|
141 testResult += "\n"; |
|
142 testResult += "Line: "; |
|
143 testResult += QByteArray::number(mCurrentTestFailureLine); |
|
144 testResult += "\n"; |
|
145 testResult += "Reason: "; |
|
146 testResult += ch.toAscii(); |
|
147 testResult += "\n"; |
|
148 mErrors.append(QString::fromAscii(testResult.data())); |
|
149 } |
|
150 return true; |
|
151 } |
|
152 |
|