1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
|
"
@file: StepUnit.st
@author: Mateu Batle mateu.batle@tragnarion.com
@brief Very simple unit testing framework done in StepTalk
for testing Objective C applications
@brief $Id$
StepUnit is released under the terms of the LGPL license.
(check http://www.gnu.org/licenses/gpl.txt)
Help:
Testing framework is executed running stexec StepUnit, or
directly StepUnit.sh. This script scans the currend diretory recursively
and runs every script whose filename matches 'test*.st'. It provides in
the environment an object called stepunit which has some useful methods
for testing. Here is a list of this methods:
- should: val. Tests result of boolean expression which must be true for
test ok.
- shouldNot: val. Tests result of boolean expression which must be false
for test ok.
- shouldBeEqual: left to: right. Tests objects are the same.
- shouldRaise: exp. Exp must be a block. Runs the block, which must throw
exception for test ok.
- shouldBeEmpty: object. Object must be nil, or object isEmpty must return
true for test ok.
- shouldNotBeEmpty: object
All methods can be added a 'desc: string' argument, which is recommended to
use to identify the test.
Each test script just has to run the test code and call the testing methods
specified before (on the object stepunit already present in the environment).
You should also load modules in the environment to access new classes, etc.
if needed (with Environment loadModule: 'modulename'). Test example:
res := 2 + 2.
stepunit should: (res = 4) desc: '2 + 2 works'
"
[|
:numTestFilesOK
:numTestFilesFailed
:numTestsFailed
:numTestsOK
:numTotalFailed
:numTotalOK
:env
:conversation
:fm
main
"*** Initialize counters ***"
numTestsFailed := 0.
numTestsOK := 0.
numTotalFailed := 0.
numTotalOK := 0.
numTestFilesFailed := 0.
numTestFilesOK := 0.
"*** Add some classes ***"
classes := (NSMutableArray alloc) init.
classes += 'STEnvironment'.
classes += 'STConversation'.
Environment addClassesWithNames: classes.
"*** Store object in the environment ***"
env := Environment.
env setObject: self forName:'stepunit'.
env setFullScriptingEnabled: YES.
"*** Create conversation ***"
conv := STConversation conversationWithContext: env language: 'Smalltalk'.
conv setLanguage: 'Smalltalk'.
"*** Execute all tests here ***"
fm := NSFileManager defaultManager.
path := (fm currentDirectoryPath) stringByStandardizingPath.
self runTests: path.
"*** Summary of running test ***"
self showSummary.
^ self
!
runTests: path
| dirent attr |
(((path lastPathComponent) substringToIndex: 1) = '.')
ifFalse:
[
Transcript showLine: '*** Scanning for tests in directory ', (path), ' ***'.
dirent := fm directoryContentsAtPath: path.
dirent do:
[ :file |
attr := fm fileAttributesAtPath: (path / file) traverseLink: NO.
filetype := (attr @ 'NSFileType').
(filetype isEqualToString: 'NSFileTypeDirectory')
ifTrue:
[
fm changeCurrentDirectoryPath: (path / file).
self runTests: (path / file).
fm changeCurrentDirectoryPath: path.
]
ifFalse:
[
(((file pathExtension) = 'st') and: ((file lowercaseString) hasPrefix: 'test'))
ifTrue: [self executeTest: (path / file)].
]
].
^self
]
!
executeTest: path
numTestsFailed := 0.
numTestsOK := 0.
Transcript show: '*** Test Case '.
Transcript show: (numTestFilesOK + numTestFilesFailed + 1).
Transcript showLine: ': ', path, ' start ***'.
code := NSString stringWithContentsOfFile: path.
res := conv interpretScript: code; result.
Transcript showLine: '*** Test Case ', path, ' finish ***'.
(numTestsFailed = 0)
ifTrue:
[
numTestFilesOK := numTestFilesOK + 1.
]
ifFalse:
[
numTestFilesFailed := numTestFilesFailed + 1.
].
^self
!
showSummary
Transcript showLine: '***** SUMMARY *****'.
Transcript showLine: 'Name', ' ', 'Failed', ' ', 'OK', ' ', 'Total'.
Transcript show: 'Files', ' '.
Transcript show: numTestFilesFailed.
Transcript show: ' '.
Transcript show: numTestFilesOK.
Transcript show: ' '.
Transcript show: (numTestFilesFailed + numTestFilesOK).
Transcript showLine: nil.
Transcript show: 'Tests', ' '.
Transcript show: numTotalFailed.
Transcript show: ' '.
Transcript show: numTotalOK.
Transcript show: ' '.
Transcript show: (numTotalFailed + numTotalOK).
Transcript showLine: ''.
^self
""" Methods for unit testing """
!
should: val
val ifFalse: [self testfail] ifTrue: [self testok].
^self
!
should: val desc: desc
val ifFalse: [self testfail: desc] ifTrue: [self testok: desc].
^self
!
shouldNot: val
val ifTrue: [self testfail] ifFalse: [self testok].
^self
!
shouldNot: val desc: desc
val ifTrue: [self testfail: desc] ifFalse: [self testok: desc].
^self
!
shouldBeEqual: left to: right
(left = right) ifFalse: [self testfail] ifTrue: [self testok].
^self
!
shouldBeEqual: left to: right desc: desc
(left = right) ifFalse: [self testfail: desc] ifTrue: [self testok: desc].
^self
!
shouldRaise: exp
flag := false.
desc := 'exception not raised'.
exp
handler:
[ :exception |
flag := true.
desc := (exception name), ' ', (exception reason).
].
self should: flag desc: desc.
^self
!
shouldRaise: exp desc: desc
flag := false.
desc2 := desc, ' exception not raised'.
exp
handler:
[ :exception |
flag := true.
desc := desc, ' ', (exception name), ' ', (exception reason).
].
self should: flag desc: desc.
^self
!
shouldBeEmpty: object
(object = nil)
ifFalse:
[
(object respondsToSelector: #isEmpty)
ifFalse: [self testfail: 'object not responds to isEmpty, assumed not empty'.]
ifTrue: [self should: (object isEmpty) desc: 'object empty'.]
]
ifTrue:
[
self testok: 'object empty'
].
^self
!
shouldBeEmpty: object desc: desc
(object = nil)
ifFalse:
[
(object respondsToSelector: #isEmpty)
ifFalse: [self testfail: desc]
ifTrue: [self should: (object isEmpty) desc: desc]
]
ifTrue:
[
self testok: desc
].
^self
!
shouldNotBeEmpty: object
(object = nil)
ifFalse:
[
(object respondsToSelector: #isEmpty)
ifFalse: [self testok: 'object not responds to isEmpty, assumed not empty']
ifTrue: [self shouldNot: (object isEmpty) desc: 'object empty']
]
ifTrue:
[
self testfail: 'object not empty'.
].
^self
!
shouldNotBeEmpty: object desc: desc
(object = nil)
ifFalse:
[
(object respondsToSelector: #isEmpty)
ifFalse: [self testok: desc]
ifTrue: [self shouldNot: (object isEmpty) desc: desc].
]
ifTrue:
[
self testfail: desc.
].
^self
!
testfail
^ self testfail: 'NO DESCRIPTION'
!
testfail: desc
numTotalFailed := numTotalFailed + 1.
numTestsFailed := numTestsFailed + 1.
Transcript showLine: 'TEST FAILED: ', desc.
^nil
!
testok
^ self testok: 'NO DESCRIPTION'
!
testok: desc
numTotalOK := numTotalOK + 1.
numTestsOK := numTestsOK + 1.
Transcript showLine: 'TEST OK: ', desc.
^ self
]
|