• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.38 API Reference
  • KDE Home
  • Contact Us
 

KUnitTest

  • kde3support
  • kunittest
runner.cpp
Go to the documentation of this file.
1
28#include "runner.h"
29
30#include <stdio.h>
31#include <iostream>
32using namespace std;
33
34#include <QtCore/QRegExp>
35#include <QtCore/QDir>
36#include <QtCore/QMetaEnum>
37
38#include <kdebug.h>
39#include <kglobal.h>
40#include <kpluginfactory.h>
41#include <kpluginloader.h>
42#include <kstandarddirs.h>
43
44#include "tester.h"
45
46namespace KUnitTest
47{
48 Runner *Runner::s_self = 0L;
49 bool Runner::s_debugCapturingEnabled = false;
50
51 void Runner::registerTester(const char *name, Tester *test)
52 {
53 Runner::self()->m_registry.insert(name, test);
54 }
55
56 void Runner::loadModules(const QString &folder, const QString &query)
57 {
58 QRegExp reQuery(query);
59 QDir dir(folder, "kunittest_*.la");
60
61 // Add the folder to the "module" resource such that the KLibLoader can
62 // find the modules in this folder.
63 KGlobal::dirs()->addResourceDir("module", folder);
64 kDebug() << "Looking in folder: " << dir.absolutePath();
65
66 // Get a list of all modules.
67 QStringList modules = dir.entryList();
68
69 for ( int i = 0; i < modules.count(); ++i )
70 {
71 QString module = modules[i];
72 kDebug() << "Module: " << dir.absolutePath() + '/' + module;
73
74 if ( reQuery.indexIn(module) != -1 )
75 {
76 // strip the .la extension
77 module.truncate(module.length()-3);
78 KPluginLoader loader(module.toLocal8Bit());
79 KPluginFactory *factory = loader.factory();
80 if ( factory )
81 factory->create<QObject>();
82 else {
83 kWarning() << "\tError loading " << module << " : " << loader.errorString();
84 ::exit( 1 );
85 }
86 }
87 else
88 kDebug() << "\tModule doesn't match.";
89 }
90 }
91
92 void Runner::setDebugCapturingEnabled(bool enabled)
93 {
94 s_debugCapturingEnabled = enabled;
95 }
96
97 //RegistryType &Runner::registry()
98 Registry &Runner::registry()
99 {
100 return m_registry;
101 }
102
103 int Runner::numberOfTestCases()
104 {
105 return m_registry.count();
106 }
107
108 Runner *Runner::self()
109 {
110 if ( !s_self )
111 s_self = new Runner();
112
113 return s_self;
114 }
115
116
117 Runner::Runner()
118 {
119 reset();
120 }
121
122 int Runner::numberOfTests() const
123 {
124 return globalSteps;
125 }
126
127 int Runner::numberOfPassedTests() const
128 {
129 return globalPasses;
130 }
131
132 int Runner::numberOfFailedTests() const
133 {
134 return globalFails;
135 }
136
137 int Runner::numberOfExpectedFailures() const
138 {
139 return globalXFails;
140 }
141
142 int Runner::numberOfSkippedTests() const
143 {
144 return globalSkipped;
145 }
146
147 void Runner::reset()
148 {
149 globalSteps = 0;
150 globalPasses = 0;
151 globalFails = 0;
152 globalXFails = 0;
153 globalXPasses = 0;
154 globalSkipped = 0;
155 }
156
157 int Runner::runTests()
158 {
159 globalSteps = 0;
160 globalPasses = 0;
161 globalFails = 0;
162 globalXFails = 0;
163 globalXPasses = 0;
164 globalSkipped = 0;
165
166 cout << "# Running normal tests... #" << endl << endl;
167
168 Registry::const_iterator it = m_registry.constBegin();
169 for( ; it != m_registry.constEnd(); ++it )
170 runTest( it.key( ) );
171
172#if 0 // very thorough, but not very readable
173 cout << "# Done with normal tests:" << endl;
174 cout << " Total test cases: " << m_registry.count() << endl;
175 cout << " Total test steps : " << globalSteps << endl;
176 cout << " Total passed test steps (including unexpected) : " << globalPasses << endl;
177 cout << " Total unexpected passed test steps : " << globalXPasses << endl;
178 cout << " Total failed test steps (including expected) : " << globalFails << endl;
179 cout << " Total expected failed test steps : " << globalXFails << endl;
180 cout << " Total skipped test steps : " << globalSkipped << endl;
181#else
182 unsigned int numTests = m_registry.count(); // should this be globalSteps instead?
183 QString str;
184 if ( globalFails == 0 )
185 if ( globalXFails == 0 )
186 str = QString( "All %1 tests passed" ).arg( numTests );
187 else
188 str = QString( "All %1 tests behaved as expected (%2 expected failures)" ).arg( numTests ).arg( globalXFails );
189 else
190 if ( globalXPasses == 0 )
191 str = QString( "%1 of %2 tests failed" ).arg( globalFails ).arg( numTests );
192 else
193 str = QString( "%1 of %2 tests did not behave as expected (%1 unexpected passes)" ).arg( globalFails ).arg( numTests ).arg( globalXPasses );
194 if ( globalSkipped )
195 str += QString( " (%1 tests skipped)" ).arg( globalSkipped );
196 cout << str.toLocal8Bit().constData() << endl;
197#endif
198
199 return m_registry.count();
200 }
201
202 void Runner::runMatchingTests(const QString &prefix)
203 {
204 Registry::const_iterator it = m_registry.constBegin();
205 for( ; it != m_registry.constEnd(); ++it )
206 if ( QString( it.key() ).startsWith(prefix) )
207 runTest( it.key() );
208 }
209
210 void Runner::runTest(const char *name)
211 {
212 Tester *test = m_registry.value( name );
213 if ( !test ) return;
214
215 if ( s_debugCapturingEnabled )
216 {
217 cout << "KUnitTest_Debug_Start[" << name << "]" << endl;
218 }
219
220 test->results()->clear();
221 test->allTests();
222
223 if ( s_debugCapturingEnabled )
224 {
225 cout << "KUnitTest_Debug_End[" << name << "]" << endl << endl << flush;
226 }
227
228 int numPass = 0;
229 int numFail = 0;
230 int numXFail = 0;
231 int numXPass = 0;
232 int numSkip = 0;
233
234 if ( test->inherits("KUnitTest::SlotTester") )
235 {
236 SlotTester *sltest = static_cast<SlotTester*>(test);
237 foreach( TestResults* res, sltest->resultsList() )
238 {
239 numPass += res->passed() + res->xpasses();
240 numFail += res->errors() + res->xfails();
241 numXFail += res->xfails();
242 numXPass += res->xpasses();
243 numSkip += res->skipped();
244 globalSteps += res->testsFinished();
245 }
246 }
247 else
248 {
249 numPass= test->results()->passed() + test->results()->xpasses();
250 numFail= test->results()->errors() + test->results()->xfails();
251 numXFail = test->results()->xfails();
252 numXPass = test->results()->xpasses();
253 numSkip= test->results()->skipped();
254 globalSteps += test->results()->testsFinished();
255 }
256
257
258 globalPasses += numPass;
259 globalFails += numFail;
260 globalXFails += numXFail;
261 globalXPasses += numXPass;
262 globalSkipped += numSkip;
263
264 cout << name << " - ";
265 cout << numPass << " test" << ( ( 1 == numPass )?"":"s") << " passed";
266 if ( 0 < test->results()->xpassList().count() ) {
267 cout << " (" << numXPass << " unexpected pass" << ( ( 1 == numXPass )?"":"es") << ")";
268 }
269 cout << ", " << numFail << " test" << ( ( 1 == numFail )?"":"s") << " failed";
270 if ( 0 < numXFail ) {
271 cout << " (" << numXFail << " expected failure" << ( ( 1 == numXFail )?"":"s") << ")";
272 }
273 if ( 0 < numSkip ) {
274 cout << "; also " << numSkip << " skipped";
275 }
276 cout << endl;
277
278 if ( 0 < numXPass ) {
279 cout << " Unexpected pass" << ( ( 1 == numXPass )?"":"es") << ":" << endl;
280 QStringList list = test->results()->xpassList();
281 for ( QStringList::Iterator itr = list.begin(); itr != list.end(); ++itr ) {
282 cout << "\t" << (*itr).toLatin1().constData() << endl;
283 }
284 }
285 if ( 0 < (numFail - numXFail) ) {
286 cout << " Unexpected failure" << ( ( 1 == numFail )?"":"s") << ":" << endl;
287 QStringList list = test->results()->errorList();
288 for ( QStringList::Iterator itr = list.begin(); itr != list.end(); ++itr ) {
289 cout << "\t" << (*itr).toLatin1().constData() << endl;
290 }
291 }
292 if ( 0 < numXFail ) {
293 cout << " Expected failure" << ( ( 1 == numXFail)?"":"s") << ":" << endl;
294 QStringList list = test->results()->xfailList();
295 for ( QStringList::Iterator itr = list.begin(); itr != list.end(); ++itr ) {
296 cout << "\t" << (*itr).toLatin1().constData() << endl;
297 }
298 }
299 if ( 0 < numSkip ) {
300 cout << " Skipped test" << ( ( 1 == numSkip )?"":"s") << ":" << endl;
301 QStringList list = test->results()->skipList();
302 for ( QStringList::Iterator itr = list.begin(); itr != list.end(); ++itr ) {
303 cout << "\t" << (*itr).toLatin1().constData() << endl;
304 }
305 }
306 cout << endl;
307
308 emit finished(name, test);
309 }
310}
311
312#include "runner.moc"
313
KPluginFactory::create
T * create(const QString &keyword, QObject *parent=0, const QVariantList &args=QVariantList())
KPluginLoader
KPluginLoader::errorString
QString errorString() const
KPluginLoader::factory
KPluginFactory * factory()
KStandardDirs::addResourceDir
bool addResourceDir(const char *type, const QString &absdir, bool priority=true)
KUnitTest::Runner
Definition: runner.h:87
KUnitTest::Runner::numberOfFailedTests
int numberOfFailedTests() const
Definition: runner.cpp:132
KUnitTest::Runner::loadModules
static void loadModules(const QString &folder, const QString &query)
Definition: runner.cpp:56
KUnitTest::Runner::runTest
void runTest(const char *name)
Definition: runner.cpp:210
KUnitTest::Runner::numberOfSkippedTests
int numberOfSkippedTests() const
Definition: runner.cpp:142
KUnitTest::Runner::runMatchingTests
void runMatchingTests(const QString &prefix)
Definition: runner.cpp:202
KUnitTest::Runner::registry
Registry & registry()
Definition: runner.cpp:98
KUnitTest::Runner::runTests
int runTests()
Definition: runner.cpp:157
KUnitTest::Runner::numberOfExpectedFailures
int numberOfExpectedFailures() const
Definition: runner.cpp:137
KUnitTest::Runner::numberOfPassedTests
int numberOfPassedTests() const
Definition: runner.cpp:127
KUnitTest::Runner::registerTester
static void registerTester(const char *name, Tester *test)
Definition: runner.cpp:51
KUnitTest::Runner::numberOfTests
int numberOfTests() const
Definition: runner.cpp:122
KUnitTest::Runner::reset
void reset()
Definition: runner.cpp:147
KUnitTest::Runner::self
static Runner * self()
Definition: runner.cpp:108
KUnitTest::Runner::finished
void finished(const char *name, Tester *test)
KUnitTest::Runner::numberOfTestCases
int numberOfTestCases()
Definition: runner.cpp:103
KUnitTest::Runner::setDebugCapturingEnabled
static void setDebugCapturingEnabled(bool enabled)
Definition: runner.cpp:92
KUnitTest::Runner::Runner
Runner()
Definition: runner.cpp:117
KUnitTest::SlotTester
Definition: tester.h:683
KUnitTest::SlotTester::resultsList
const TestResultsList & resultsList() const
Return the list of results - used internally by Runner.
Definition: tester.h:698
KUnitTest::TestResults
Definition: tester.h:427
KUnitTest::TestResults::skipList
QStringList skipList() const
Definition: tester.h:488
KUnitTest::TestResults::xpasses
int xpasses() const
Definition: tester.h:470
KUnitTest::TestResults::xfails
int xfails() const
Definition: tester.h:467
KUnitTest::TestResults::xfailList
QStringList xfailList() const
Definition: tester.h:482
KUnitTest::TestResults::clear
virtual void clear()
Definition: tester.h:437
KUnitTest::TestResults::errorList
QStringList errorList() const
Definition: tester.h:479
KUnitTest::TestResults::errors
int errors() const
Definition: tester.h:464
KUnitTest::TestResults::skipped
int skipped() const
Definition: tester.h:473
KUnitTest::TestResults::xpassList
QStringList xpassList() const
Definition: tester.h:485
KUnitTest::TestResults::testsFinished
int testsFinished() const
Definition: tester.h:461
KUnitTest::TestResults::passed
int passed() const
Definition: tester.h:476
KUnitTest::Tester
Definition: tester.h:517
KUnitTest::Tester::results
virtual TestResults * results() const
Definition: tester.h:535
KUnitTest::Tester::allTests
virtual void allTests()=0
QHash< QByteArray, Tester * >
QObject
kDebug
#define kDebug
kWarning
#define kWarning
kdebug.h
kglobal.h
kpluginfactory.h
kpluginloader.h
kstandarddirs.h
prefix
QString prefix()
KGlobal::dirs
KStandardDirs * dirs()
KUnitTest
Copyright (C) 2005 Jeroen Wijnhout Jeroen.Wijnhout@kdemail.net
Definition: module.h:35
runner.h
KPluginFactory
tester.h
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KUnitTest

Skip menu "KUnitTest"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdelibs-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal