1 |
jgs |
82 |
|
2 |
|
|
|
3 |
|
|
/* |
4 |
|
|
* A command line based tool to run tests. |
5 |
|
|
* TestRunner expects as its only argument the name of a TestCase class. |
6 |
|
|
* TestRunner prints out a trace as the tests are executed followed by a |
7 |
|
|
* summary at the end. |
8 |
|
|
* |
9 |
|
|
* You can add to the tests that the TestRunner knows about by |
10 |
|
|
* making additional calls to "addTest (...)" in main. |
11 |
|
|
* |
12 |
|
|
* Here is the synopsis: |
13 |
|
|
* |
14 |
|
|
* TestRunner [-wait] ExampleTestCase |
15 |
|
|
* |
16 |
|
|
*/ |
17 |
|
|
|
18 |
|
|
|
19 |
|
|
#include <vector> |
20 |
|
|
|
21 |
|
|
#include "CppUnitTest/TestRunner.h" |
22 |
|
|
#include "CppUnitTest/TextTestResult.h" |
23 |
|
|
#include "CppUnitTest/Test.h" |
24 |
|
|
|
25 |
|
|
USING_NAMESPACE_CPPUNITTEST |
26 |
|
|
|
27 |
|
|
void TestRunner::run (int ac, char **av) |
28 |
|
|
{ |
29 |
|
|
std::string testCase; |
30 |
|
|
int numberOfTests = 0; |
31 |
|
|
std::vector<std::string> tCases; |
32 |
|
|
std::vector<std::string> argList; |
33 |
|
|
bool failuresOccurred=false; |
34 |
|
|
// |
35 |
|
|
// process the arguments |
36 |
|
|
// |
37 |
|
|
// suites to run are specified by suite=aaa,bbb,ccc |
38 |
|
|
// more than one suite argument can be given |
39 |
|
|
// no attempt is made to remove duplicates |
40 |
|
|
// |
41 |
|
|
std::string suiteKey = "suite="; |
42 |
|
|
std::string suiteSep = ","; |
43 |
|
|
for (int i = 0; i < ac; ++i) { |
44 |
|
|
std::string testArg = av[i]; |
45 |
|
|
std::string::size_type loc = testArg.find(suiteKey); |
46 |
|
|
if(i>0 && loc==0) { |
47 |
|
|
testArg.erase(0,suiteKey.size()); |
48 |
|
|
while(testArg.size()>0) { |
49 |
|
|
loc = testArg.find(suiteSep); |
50 |
|
|
std::string suiteName; |
51 |
|
|
if(loc==std::string::npos) { |
52 |
|
|
suiteName = testArg; |
53 |
|
|
testArg.erase(); |
54 |
|
|
} |
55 |
|
|
else if(loc>0) { |
56 |
|
|
suiteName = std::string(testArg,0,loc); |
57 |
|
|
testArg.erase(0,loc+suiteSep.size()); |
58 |
|
|
} |
59 |
|
|
else { |
60 |
|
|
testArg.erase(0,suiteSep.size()); |
61 |
|
|
} |
62 |
|
|
if(suiteName.size()>0) { |
63 |
|
|
tCases.push_back(suiteName); |
64 |
|
|
} |
65 |
|
|
} |
66 |
|
|
} |
67 |
|
|
else { |
68 |
|
|
argList.push_back(testArg); |
69 |
|
|
} |
70 |
|
|
} |
71 |
|
|
mappings::iterator it; |
72 |
|
|
Test *testToRun; |
73 |
|
|
if (tCases.size()==0) { |
74 |
|
|
// |
75 |
|
|
// no particular cases have been selected so run all cases |
76 |
|
|
for (it = m_mappings.begin (); it!=m_mappings.end(); ++it) { |
77 |
|
|
std::cout << "Suite: " << (*it).first << std::endl; |
78 |
|
|
testToRun = (*it).second; |
79 |
|
|
testToRun->setArgs(argList); |
80 |
|
|
run (testToRun); |
81 |
|
|
if(testToRun->hasFailure()) { |
82 |
|
|
failuresOccurred=true; |
83 |
|
|
} |
84 |
|
|
numberOfTests++; |
85 |
|
|
} |
86 |
|
|
} else { |
87 |
|
|
std::vector<std::string>::const_iterator tci; |
88 |
|
|
for (tci=tCases.begin(); tci!=tCases.end(); ++tci) { |
89 |
|
|
testToRun=NULL; |
90 |
|
|
for (it = m_mappings.begin(); |
91 |
|
|
it != m_mappings.end() && testToRun==NULL; |
92 |
|
|
++it) { |
93 |
|
|
testCase=*tci; |
94 |
|
|
if ((*it).first == (*tci)) { |
95 |
|
|
std::cout << "Suite: " << (*it).first << std::endl; |
96 |
|
|
testToRun = (*it).second; |
97 |
|
|
testToRun->setArgs(argList); |
98 |
|
|
run (testToRun); |
99 |
|
|
if(testToRun->hasFailure()) { |
100 |
|
|
failuresOccurred=true; |
101 |
|
|
} |
102 |
|
|
numberOfTests++; |
103 |
|
|
} |
104 |
|
|
} |
105 |
|
|
if (!testToRun) { |
106 |
|
|
std::cout << "Test " << testCase << " not found." << std::endl; |
107 |
|
|
failuresOccurred=true; |
108 |
|
|
} |
109 |
|
|
} |
110 |
|
|
} |
111 |
|
|
if(failuresOccurred) { |
112 |
|
|
exit(1); |
113 |
|
|
} |
114 |
|
|
} |
115 |
|
|
|
116 |
|
|
|
117 |
|
|
TestRunner::~TestRunner () |
118 |
|
|
{ |
119 |
|
|
for (mappings::iterator it = m_mappings.begin (); |
120 |
|
|
it != m_mappings.end (); |
121 |
|
|
++it) |
122 |
|
|
delete it->second; |
123 |
|
|
|
124 |
|
|
} |
125 |
|
|
|
126 |
|
|
|
127 |
|
|
void TestRunner::run (Test *test) |
128 |
|
|
{ |
129 |
|
|
TextTestResult result; |
130 |
|
|
|
131 |
|
|
test->run (&result); |
132 |
|
|
|
133 |
|
|
std::cout << result << std::endl; |
134 |
|
|
} |
135 |
|
|
|