author | Claus Gittinger <cg@exept.de> |
Mon, 04 Nov 2002 12:52:10 +0100 | |
changeset 81 | 10852410025d |
parent 73 | dcd22ffe7d1f |
child 82 | e0e67a37e1ec |
permissions | -rw-r--r-- |
0 | 1 |
"{ Package: 'stx:goodies/sunit' }" |
2 |
||
3 |
Object subclass:#TestCase |
|
4 |
instanceVariableNames:'testSelector' |
|
5 |
classVariableNames:'' |
|
6 |
poolDictionaries:'' |
|
6
78bb1397e43d
added rerun-defect tests; fixed button enable bug
Claus Gittinger <cg@exept.de>
parents:
4
diff
changeset
|
7 |
category:'SUnit-Base' |
0 | 8 |
! |
9 |
||
81 | 10 |
TestCase class instanceVariableNames:'lastTestrunResultOrNil' |
11 |
||
12 |
" |
|
13 |
No other class instance variables are inherited by this class. |
|
14 |
" |
|
15 |
! |
|
16 |
||
12 | 17 |
|
2 | 18 |
!TestCase class methodsFor:'initialization'! |
19 |
||
20 |
initialize |
|
21 |
"ensure, that the sunit extensions are loaded" |
|
22 |
||
65
019891d527b7
implements -> includesSelector
Claus Gittinger <cg@exept.de>
parents:
63
diff
changeset
|
23 |
(Class includesSelector:#sunitName) ifFalse:[ |
019891d527b7
implements -> includesSelector
Claus Gittinger <cg@exept.de>
parents:
63
diff
changeset
|
24 |
Smalltalk fileIn:'extensions.st' inPackage:(self package) |
2 | 25 |
]. |
72 | 26 |
TestFailure autoload |
2 | 27 |
|
28 |
" |
|
29 |
self initialize |
|
4 | 30 |
" |
31 |
! ! |
|
2 | 32 |
|
68 | 33 |
!TestCase class methodsFor:'instance creation'! |
0 | 34 |
|
35 |
debug: aSymbol |
|
6
78bb1397e43d
added rerun-defect tests; fixed button enable bug
Claus Gittinger <cg@exept.de>
parents:
4
diff
changeset
|
36 |
^(self selector: aSymbol) debug |
78bb1397e43d
added rerun-defect tests; fixed button enable bug
Claus Gittinger <cg@exept.de>
parents:
4
diff
changeset
|
37 |
! |
0 | 38 |
|
39 |
run: aSymbol |
|
6
78bb1397e43d
added rerun-defect tests; fixed button enable bug
Claus Gittinger <cg@exept.de>
parents:
4
diff
changeset
|
40 |
^(self selector: aSymbol) run |
78bb1397e43d
added rerun-defect tests; fixed button enable bug
Claus Gittinger <cg@exept.de>
parents:
4
diff
changeset
|
41 |
! |
0 | 42 |
|
43 |
selector: aSymbol |
|
6
78bb1397e43d
added rerun-defect tests; fixed button enable bug
Claus Gittinger <cg@exept.de>
parents:
4
diff
changeset
|
44 |
^self new setTestSelector: aSymbol |
78bb1397e43d
added rerun-defect tests; fixed button enable bug
Claus Gittinger <cg@exept.de>
parents:
4
diff
changeset
|
45 |
! |
0 | 46 |
|
47 |
suite |
|
50 | 48 |
"/ ^self buildSuite |
37 | 49 |
| testSelectors result | |
50 |
testSelectors := self sunitSelectors select: [:each | 'test*' match: each]. |
|
51 |
testSelectors sort. |
|
52 |
result := TestSuite new. |
|
53 |
result name:self name. |
|
54 |
testSelectors do: [:each | result addTest: (self selector: each)]. |
|
55 |
^result |
|
0 | 56 |
|
57 |
"Modified: / 21.6.2000 / 10:05:24 / Sames" |
|
58 |
! ! |
|
59 |
||
66 | 60 |
!TestCase class methodsFor:'accessing'! |
61 |
||
62 |
allTestSelectors |
|
63 |
^self sunitAllSelectors select: [:each | 'test*' match: each] |
|
64 |
! |
|
65 |
||
81 | 66 |
forgetLastTestRunResult |
67 |
lastTestrunResultOrNil := nil |
|
68 |
! |
|
69 |
||
70 |
lastTestrunResultOrNil |
|
71 |
^ lastTestrunResultOrNil |
|
72 |
! |
|
73 |
||
74 |
rememberFailedTestRun |
|
75 |
lastTestrunResultOrNil := false |
|
76 |
! |
|
77 |
||
78 |
rememberPassedTestRun |
|
79 |
lastTestrunResultOrNil := true |
|
80 |
! |
|
81 |
||
66 | 82 |
resources |
83 |
^#() |
|
84 |
! |
|
85 |
||
86 |
testSelectors |
|
87 |
^self sunitSelectors select: [:each | 'test*' match: each] |
|
88 |
! ! |
|
89 |
||
68 | 90 |
!TestCase class methodsFor:'building Suites'! |
91 |
||
92 |
buildSuite |
|
93 |
||
94 |
| suite | |
|
95 |
^self isAbstract |
|
96 |
ifTrue: |
|
97 |
[suite := TestSuite new. |
|
98 |
suite name: self name asString. |
|
99 |
self allSubclasses do: [:each | each isAbstract ifFalse: [suite addTest: each buildSuiteFromSelectors]]. |
|
100 |
suite] |
|
101 |
ifFalse: [self buildSuiteFromSelectors] |
|
102 |
! |
|
103 |
||
104 |
buildSuiteFromAllSelectors |
|
105 |
^self buildSuiteFromMethods: self allTestSelectors |
|
106 |
! |
|
107 |
||
108 |
buildSuiteFromLocalSelectors |
|
109 |
^self buildSuiteFromMethods: self testSelectors |
|
110 |
! |
|
111 |
||
112 |
buildSuiteFromMethods: testMethods |
|
113 |
^testMethods |
|
114 |
inject: ((TestSuite new) |
|
115 |
name: self name asString; |
|
116 |
yourself) |
|
117 |
into: |
|
118 |
[:suite :selector | |
|
119 |
suite |
|
120 |
addTest: (self selector: selector); |
|
121 |
yourself] |
|
122 |
! |
|
123 |
||
124 |
buildSuiteFromSelectors |
|
125 |
^self shouldInheritSelectors |
|
126 |
ifTrue: [self buildSuiteFromAllSelectors] |
|
127 |
ifFalse: [self buildSuiteFromLocalSelectors] |
|
128 |
! ! |
|
129 |
||
66 | 130 |
!TestCase class methodsFor:'testing'! |
50 | 131 |
|
132 |
isAbstract |
|
133 |
"Override to true if a TestCase subclass is Abstract and should not have |
|
134 |
TestCase instances built from it" |
|
135 |
^self name = #TestCase. |
|
136 |
! |
|
137 |
||
138 |
shouldInheritSelectors |
|
139 |
"answer true to inherit selectors from superclasses" |
|
140 |
||
141 |
^true |
|
142 |
! ! |
|
143 |
||
66 | 144 |
!TestCase methodsFor:'accessing'! |
145 |
||
146 |
assert: aBoolean |
|
147 |
"fail, if the argument is not true" |
|
148 |
||
69 | 149 |
"/ "check the testCase itself" |
150 |
"/ (aBoolean isBoolean) ifFalse:[ self error:'non boolean assertion' ]. |
|
151 |
"/ aBoolean ifFalse: [self signalFailure: 'Assertion failed'] |
|
152 |
||
153 |
self assert: aBoolean message:'Assertion failed' |
|
154 |
! |
|
155 |
||
156 |
assert: aBoolean message:messageIfFailing |
|
157 |
"fail, if the argument is not true" |
|
158 |
||
66 | 159 |
"check the testCase itself" |
160 |
(aBoolean isBoolean) ifFalse:[ self error:'non boolean assertion' ]. |
|
69 | 161 |
aBoolean ifFalse: [self signalFailure: messageIfFailing] |
66 | 162 |
|
163 |
"Modified: / 21.6.2000 / 10:00:05 / Sames" |
|
164 |
! |
|
165 |
||
166 |
assertFalse:aBoolean |
|
167 |
^ self assert:aBoolean not |
|
168 |
! |
|
169 |
||
170 |
assertFalse:aBoolean named:testName |
|
171 |
^ self assert:aBoolean not |
|
172 |
! |
|
173 |
||
174 |
assertTrue:aBoolean |
|
175 |
^ self assert:aBoolean |
|
176 |
! |
|
177 |
||
178 |
assertTrue:aBoolean named:testName |
|
179 |
^ self assert:aBoolean |
|
180 |
! |
|
181 |
||
182 |
deny: aBoolean |
|
183 |
"fail, if the argument is not false" |
|
184 |
||
185 |
self assert: aBoolean not |
|
186 |
! |
|
187 |
||
188 |
resources |
|
189 |
^self class resources |
|
190 |
! |
|
191 |
||
192 |
selector |
|
193 |
^testSelector |
|
194 |
! |
|
195 |
||
196 |
should: aBlock |
|
197 |
"fail, if the block does not evaluate to true" |
|
198 |
||
199 |
self assert: aBlock value |
|
200 |
! |
|
201 |
||
202 |
should: aBlock raise: anExceptionalEvent |
|
203 |
"fail, if the block does not raise the given event" |
|
204 |
||
205 |
^self assert: (self executeShould: aBlock inScopeOf: anExceptionalEvent) |
|
206 |
||
207 |
"Modified: / 21.6.2000 / 10:01:08 / Sames" |
|
208 |
! |
|
209 |
||
210 |
shouldnt: aBlock |
|
211 |
"fail, if the block does evaluate to true" |
|
212 |
||
213 |
self deny: aBlock value |
|
214 |
! |
|
215 |
||
216 |
shouldnt: aBlock raise: anExceptionalEvent |
|
217 |
"fail, if the block does raise the given event" |
|
218 |
||
219 |
^self assert: (self executeShould: aBlock inScopeOf: anExceptionalEvent) not |
|
220 |
||
221 |
"Modified: / 21.6.2000 / 10:01:16 / Sames" |
|
222 |
! |
|
223 |
||
224 |
signalFailure: aString |
|
225 |
"/ TestResult failure sunitSignalWith: aString |
|
226 |
TestResult failure raiseErrorString:aString in:thisContext sender sender . |
|
227 |
||
228 |
"Modified: / 21.6.2000 / 10:01:34 / Sames" |
|
229 |
! ! |
|
230 |
||
73 | 231 |
!TestCase methodsFor:'accessing & queries'! |
232 |
||
233 |
unfinished |
|
234 |
||
235 |
"indicates an unfinished test" |
|
236 |
! ! |
|
237 |
||
67 | 238 |
!TestCase methodsFor:'dependencies'! |
239 |
||
240 |
addDependentToHierachy: anObject |
|
241 |
"an empty method. for Composite compability with TestSuite" |
|
242 |
! |
|
243 |
||
244 |
removeDependentFromHierachy: anObject |
|
245 |
"an empty method. for Composite compability with TestSuite" |
|
246 |
! ! |
|
247 |
||
248 |
!TestCase methodsFor:'printing'! |
|
249 |
||
250 |
name |
|
251 |
^ self class name. |
|
252 |
! |
|
253 |
||
254 |
printOn: aStream |
|
255 |
aStream nextPutAll: self class name. |
|
256 |
aStream nextPutAll: '>>'. |
|
257 |
aStream nextPutAll: testSelector |
|
258 |
||
259 |
"Modified: / 4.4.2000 / 18:59:53 / Sames" |
|
260 |
! ! |
|
261 |
||
262 |
!TestCase methodsFor:'private'! |
|
263 |
||
264 |
executeShould: aBlock inScopeOf: anExceptionalEvent |
|
265 |
[[aBlock value] |
|
266 |
on: anExceptionalEvent |
|
267 |
do: [:ex | ^true]] |
|
268 |
on: TestResult error |
|
269 |
do: [:ex | ^false]. |
|
270 |
^false. |
|
271 |
||
272 |
"Modified: / 21.6.2000 / 10:03:03 / Sames" |
|
273 |
! |
|
274 |
||
275 |
performTest |
|
276 |
self perform: testSelector asSymbol |
|
277 |
! |
|
278 |
||
279 |
setTestSelector: aSymbol |
|
280 |
testSelector := aSymbol |
|
281 |
! ! |
|
282 |
||
283 |
!TestCase methodsFor:'running'! |
|
284 |
||
285 |
debug |
|
286 |
self debugUsing:#runCase. |
|
287 |
"/ (self class selector: testSelector) runCase |
|
288 |
! |
|
289 |
||
290 |
debugAsFailure |
|
291 |
self debugUsing: #runCaseAsFailure |
|
292 |
"/ (self class selector: testSelector) runCaseAsFailure |
|
293 |
! |
|
294 |
||
295 |
debugUsing: aSymbol |
|
296 |
self areAllResourcesAvailable ifFalse: [^TestResult signalErrorWith: 'Resource could not be initialized']. |
|
297 |
[(self class selector: testSelector) perform: aSymbol] ensure: [self resources do: [:each | each reset]] |
|
298 |
! |
|
299 |
||
300 |
openDebuggerOnFailingTestMethod |
|
69 | 301 |
"SUnit has halted one step in front of the failing test method. |
302 |
Step over the 'self halt' and send into 'self perform: testSelector' |
|
303 |
to see the failure from the beginning" |
|
67 | 304 |
|
69 | 305 |
"/ self halt. |
306 |
self performTest "/ perform: testSelector asSymbol |
|
67 | 307 |
|
308 |
"Modified: / 21.6.2000 / 10:03:37 / Sames" |
|
309 |
! |
|
310 |
||
311 |
run |
|
312 |
| result | |
|
313 |
result := TestResult new. |
|
314 |
self run: result. |
|
315 |
^result |
|
316 |
! |
|
317 |
||
318 |
run: aResult |
|
319 |
aResult runCase: self |
|
320 |
! |
|
321 |
||
322 |
run: aResult afterEachDo:block2 |
|
323 |
aResult runCase: self. |
|
324 |
block2 value:self value:aResult. |
|
325 |
! |
|
326 |
||
327 |
run: aResult beforeEachDo:block1 afterEachDo:block2 |
|
328 |
block1 value:self value:aResult. |
|
70 | 329 |
"/testSelector == #testReadStatement ifTrue:[self halt]. |
67 | 330 |
aResult runCase: self. |
331 |
block2 value:self value:aResult. |
|
332 |
! |
|
333 |
||
334 |
runCase |
|
335 |
self setUp. |
|
336 |
[self performTest "self perform: testSelector asSymbol"] ensure: [self tearDown] |
|
337 |
||
338 |
"Modified: / 21.6.2000 / 10:04:18 / Sames" |
|
339 |
! |
|
340 |
||
341 |
runCaseAsFailure |
|
342 |
self setUp. |
|
343 |
[[self openDebuggerOnFailingTestMethod] ensure: [self tearDown]] fork |
|
344 |
||
345 |
"Modified: / 21.6.2000 / 10:04:33 / Sames" |
|
346 |
! |
|
347 |
||
348 |
setUp |
|
349 |
! |
|
350 |
||
351 |
tearDown |
|
352 |
! ! |
|
353 |
||
66 | 354 |
!TestCase methodsFor:'testing'! |
50 | 355 |
|
356 |
areAllResourcesAvailable |
|
357 |
^self resources |
|
358 |
inject: true |
|
359 |
into: [:total :each | each isAvailable & total] |
|
360 |
! ! |
|
361 |
||
12 | 362 |
!TestCase class methodsFor:'documentation'! |
363 |
||
364 |
version |
|
81 | 365 |
^ '$Header: /cvs/stx/stx/goodies/sunit/TestCase.st,v 1.27 2002-11-04 11:52:10 cg Exp $' |
12 | 366 |
! ! |
81 | 367 |
|
2 | 368 |
TestCase initialize! |