author | boris |
Thu, 31 Aug 2006 11:23:04 +0200 | |
changeset 138 | 90efce9992ea |
parent 131 | 86ad122e57f6 |
child 139 | 5a48f282d789 |
permissions | -rw-r--r-- |
138 | 1 |
"{ Package: '__NoProject__' }" |
0 | 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 |
||
87
24e88e7f5d88
remember individual failed cases.
Claus Gittinger <cg@exept.de>
parents:
82
diff
changeset
|
10 |
TestCase class instanceVariableNames:'lastTestRunResultOrNil lastTestRunsFailedTests' |
81 | 11 |
|
12 |
" |
|
13 |
No other class instance variables are inherited by this class. |
|
14 |
" |
|
15 |
! |
|
16 |
||
103 | 17 |
TestCase comment:'A TestCase is a Command representing the future running of a test case. Create one with the class method #selector: aSymbol, passing the name of the method to be run when the test case runs. |
18 |
||
19 |
When you discover a new fixture, subclass TestCase, declare instance variables for the objects in the fixture, override #setUp to initialize the variables, and possibly override# tearDown to deallocate any external resources allocated in #setUp. |
|
20 |
||
21 |
When you are writing a test case method, send #assert: aBoolean when you want to check for an expected value. For example, you might say "self assert: socket isOpen" to test whether or not a socket is open at a point in a test.' |
|
22 |
! |
|
23 |
||
138 | 24 |
!TestCase class methodsFor:'documentation'! |
12 | 25 |
|
138 | 26 |
documentation |
27 |
" |
|
28 |
the concept of a prep, is to provide an opportunity to set up some UnitTestCase parameters |
|
29 |
before the fixture is setUp, there is a #defaultPrep, or a #prepFor_<eachTest> |
|
30 |
||
31 |
[author:] |
|
32 |
Keith Hodges (kh@cuthbert) |
|
33 |
||
34 |
[see also:] |
|
35 |
||
36 |
[instance variables:] |
|
2 | 37 |
|
138 | 38 |
[class variables:] |
39 |
" |
|
40 |
! |
|
2 | 41 |
|
138 | 42 |
examples |
43 |
" |
|
44 |
examples to be added. |
|
45 |
[exBegin] |
|
46 |
... add code fragment for |
|
47 |
... executable example here ... |
|
48 |
[exEnd] |
|
49 |
" |
|
50 |
! |
|
51 |
||
52 |
history |
|
53 |
"Created: / 9.7.1999 / 17:28:21 / kh" |
|
4 | 54 |
! ! |
2 | 55 |
|
138 | 56 |
|
68 | 57 |
!TestCase class methodsFor:'instance creation'! |
0 | 58 |
|
59 |
selector: aSymbol |
|
103 | 60 |
|
6
78bb1397e43d
added rerun-defect tests; fixed button enable bug
Claus Gittinger <cg@exept.de>
parents:
4
diff
changeset
|
61 |
^self new setTestSelector: aSymbol |
103 | 62 |
|
0 | 63 |
! ! |
64 |
||
66 | 65 |
|
66 |
||
138 | 67 |
!TestCase class methodsFor:'running'! |
81 | 68 |
|
138 | 69 |
allSelectorsPrefixed: string |
87
24e88e7f5d88
remember individual failed cases.
Claus Gittinger <cg@exept.de>
parents:
82
diff
changeset
|
70 |
|
138 | 71 |
| selectors | |
81 | 72 |
|
138 | 73 |
selectors := OrderedCollection new. |
87
24e88e7f5d88
remember individual failed cases.
Claus Gittinger <cg@exept.de>
parents:
82
diff
changeset
|
74 |
|
138 | 75 |
"/self superclasses do: [ :class | selectors addAll: (class selectors select: [ :selector | selector startsWith: string ]) ]. |
87
24e88e7f5d88
remember individual failed cases.
Claus Gittinger <cg@exept.de>
parents:
82
diff
changeset
|
76 |
|
138 | 77 |
selectors addAll: (self selectors select: [ :selector | selector startsWith: string ]). |
87
24e88e7f5d88
remember individual failed cases.
Claus Gittinger <cg@exept.de>
parents:
82
diff
changeset
|
78 |
|
138 | 79 |
^selectors |
131 | 80 |
|
81 |
||
103 | 82 |
! |
83 |
||
138 | 84 |
allTestCases |
103 | 85 |
|
138 | 86 |
^(self allSelectorsPrefixed: 'test') collect: [ :selector | self selector: selector ]. |
68 | 87 |
|
138 | 88 |
|
68 | 89 |
! |
90 |
||
138 | 91 |
runAllUnitTests |
92 |
| test | |
|
93 |
test := TestSuite named: (self name). |
|
94 |
test addTestCases: self allTestCases. |
|
95 |
^test run |
|
96 |
" |
|
97 |
self run. |
|
98 |
Transcript showCR: self runAll. |
|
99 |
" |
|
103 | 100 |
|
68 | 101 |
! ! |
102 |
||
103 | 103 |
|
104 |
||
97 | 105 |
|
106 |
!TestCase methodsFor:'assertions'! |
|
66 | 107 |
|
108 |
assert: aBoolean |
|
109 |
"fail, if the argument is not true" |
|
110 |
||
103 | 111 |
"/ aBoolean ifFalse: [self signalFailure: 'Assertion failed'] |
69 | 112 |
|
113 |
self assert: aBoolean message:'Assertion failed' |
|
114 |
! |
|
115 |
||
103 | 116 |
should:aBlock |
117 |
"fail, if the block does not evaluate to true" |
|
118 |
||
119 |
self assert:aBlock value |
|
66 | 120 |
! |
121 |
||
103 | 122 |
should:aBlock raise:anExceptionalEvent |
66 | 123 |
"fail, if the block does not raise the given event" |
103 | 124 |
|
125 |
^ self assert:(self executeShould:aBlock inScopeOf:anExceptionalEvent) |
|
66 | 126 |
! |
127 |
||
103 | 128 |
shouldnt:aBlock |
129 |
"fail, if the block does evaluate to true" |
|
130 |
||
131 |
self deny:aBlock value |
|
66 | 132 |
! ! |
133 |
||
67 | 134 |
|
135 |
!TestCase methodsFor:'printing'! |
|
136 |
||
137 |
printOn: aStream |
|
138 |
||
103 | 139 |
"/ aStream |
140 |
"/ nextPutAll: self class printString; |
|
141 |
"/ nextPutAll: '>>#'; |
|
142 |
"/ nextPutAll: testSelector |
|
143 |
||
144 |
aStream nextPutAll: self name. |
|
145 |
aStream nextPutAll: '>>'. |
|
118
9464f408680f
care for printing uninitialized testcases
Claus Gittinger <cg@exept.de>
parents:
103
diff
changeset
|
146 |
testSelector printOn: aStream |
67 | 147 |
! ! |
148 |
||
149 |
!TestCase methodsFor:'private'! |
|
150 |
||
138 | 151 |
errorException |
103 | 152 |
|
138 | 153 |
^self class errorSignal |
103 | 154 |
|
138 | 155 |
|
156 |
||
67 | 157 |
! |
158 |
||
159 |
performTest |
|
103 | 160 |
|
67 | 161 |
self perform: testSelector asSymbol |
162 |
! |
|
163 |
||
138 | 164 |
setSelector: aSymbol |
165 |
selector := aSymbol |
|
67 | 166 |
! ! |
167 |
||
122 | 168 |
|
67 | 169 |
!TestCase methodsFor:'running'! |
170 |
||
138 | 171 |
defaultPrep |
67 | 172 |
|
138 | 173 |
"prep methods are called before UnitTestCase-#setUp |
174 |
this is the default prep method if there |
|
175 |
is none defined for a unit test" |
|
67 | 176 |
! |
177 |
||
138 | 178 |
prepTest |
103 | 179 |
|
138 | 180 |
| prepSelector | |
103 | 181 |
|
138 | 182 |
prepSelector := ('prepFor_', selector) asSymbol. |
103 | 183 |
|
138 | 184 |
(self respondsTo: prepSelector) ifTrue: [ self perform: prepSelector ] |
185 |
ifFalse: [ self defaultPrep ]. |
|
67 | 186 |
! |
187 |
||
188 |
run |
|
189 |
| result | |
|
190 |
result := TestResult new. |
|
191 |
self run: result. |
|
192 |
^result |
|
103 | 193 |
|
67 | 194 |
! |
195 |
||
196 |
run: aResult |
|
197 |
aResult runCase: self |
|
103 | 198 |
|
67 | 199 |
! |
200 |
||
201 |
setUp |
|
103 | 202 |
|
67 | 203 |
! |
204 |
||
205 |
tearDown |
|
103 | 206 |
|
50 | 207 |
! ! |
208 |
||
138 | 209 |
!TestCase methodsFor:'set up'! |
210 |
||
211 |
onSetUpFailed |
|
212 |
"Run whatever code you need to tidy up if the Setup procedure fails" |
|
213 |
! ! |
|
214 |
||
12 | 215 |
!TestCase class methodsFor:'documentation'! |
216 |
||
217 |
version |
|
138 | 218 |
^ '$Header: /cvs/stx/stx/goodies/sunit/TestCase.st,v 1.43 2006-08-31 09:23:04 boris Exp $' |
12 | 219 |
! ! |
81 | 220 |
|
2 | 221 |
TestCase initialize! |