TestCase.st
changeset 382 05683696d647
parent 377 71ada743cae2
child 390 c2c3895aabd6
equal deleted inserted replaced
381:8c4f92047623 382:05683696d647
     5 	classVariableNames:''
     5 	classVariableNames:''
     6 	poolDictionaries:''
     6 	poolDictionaries:''
     7 	category:'SUnit-Base'
     7 	category:'SUnit-Base'
     8 !
     8 !
     9 
     9 
    10 TestCase class instanceVariableNames:'lastTestRunResultOrNil lastTestRunsPassedTests lastTestRunsFailedTests
    10 TestCase class instanceVariableNames:'lastOutcomes'
    11 	lastTestRunsErrorTests'
       
    12 
    11 
    13 "
    12 "
    14  No other class instance variables are inherited by this class.
    13  No other class instance variables are inherited by this class.
    15 "
    14 "
    16 !
    15 !
    92     "Created: / 06-08-2006 / 11:46:17 / cg"
    91     "Created: / 06-08-2006 / 11:46:17 / cg"
    93     "Modified: / 05-12-2009 / 18:50:57 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    92     "Modified: / 05-12-2009 / 18:50:57 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    94     "Modified: / 02-08-2011 / 17:46:51 / cg"
    93     "Modified: / 02-08-2011 / 17:46:51 / cg"
    95 !
    94 !
    96 
    95 
    97 lastTestRunResult
       
    98 
       
    99     | result |
       
   100     result := TestResult new.
       
   101     lastTestRunsPassedTests ? #() do:
       
   102         [:selector|result passed add: (self selector: selector)].
       
   103     lastTestRunsFailedTests ? #() do:
       
   104         [:selector|result failures add: (self selector: selector)].
       
   105     lastTestRunsErrorTests ? #() do:
       
   106         [:selector|result errors add: (self selector: selector)].
       
   107 
       
   108     "Created: / 15-03-2010 / 19:12:03 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   109 !
       
   110 
       
   111 lastTestRunResultOrNil
    96 lastTestRunResultOrNil
   112     ^ lastTestRunResultOrNil
    97 
       
    98     "Returns true if all tests passed, false if at least
       
    99      one failed/error  or nil if never run            
       
   100     "
       
   101     lastOutcomes isNil ifTrue:[^nil].
       
   102     lastOutcomes size ~= self testSelectors size ifTrue:[^nil].
       
   103     lastOutcomes do:[:outcome|
       
   104         outcome result == #pass ifFalse:[
       
   105             ^false
       
   106         ].
       
   107     ].
       
   108     ^true
       
   109 
       
   110     "Modified: / 20-08-2011 / 14:59:26 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   113 !
   111 !
   114 
   112 
   115 lookupHierarchyRoot
   113 lookupHierarchyRoot
   116         ^TestCase
   114         ^TestCase
   117 !
   115 !
   118 
   116 
   119 rememberErrorTest:selector
   117 rememberOutcome: outcome
   120 
   118 
   121     | emitChange |
   119     lastOutcomes isNil ifTrue:[
   122 
   120         lastOutcomes := OrderedCollection new.
   123     lastTestRunsErrorTests isNil ifTrue:[
   121     ].
   124         lastTestRunsErrorTests := Set new.
   122     "Not a nice code, but portable..."
   125     ].
   123     1 to: lastOutcomes size do:[:i|
   126 
   124         | each |
   127     emitChange := (self removeSelector: selector from: lastTestRunsPassedTests).
   125 
   128     emitChange := (self removeSelector: selector from: lastTestRunsFailedTests) or:[emitChange].
   126         each := lastOutcomes at: i.
   129     emitChange := (self addSelector: selector to: lastTestRunsErrorTests) or:[emitChange].
   127         (each testCase class == outcome testCase class and:
   130 
   128             [each testCase selector == outcome testCase selector]) ifTrue:[
   131     emitChange ifTrue:[self lastTestRunResultChanged: selector].
   129                 lastOutcomes at: i put: outcome.
   132 
   130                 each result ~= outcome result ifTrue:[
   133     self rememberFailedTestRun
   131                     self lastTestRunResultChanged: outcome selector. 
   134 
   132                 ].
   135     "Modified: / 06-08-2006 / 11:01:08 / cg"
   133                 ^self.                    
   136     "Created: / 15-03-2010 / 19:16:19 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   134             ].
   137 !
   135     ].
   138 
   136     lastOutcomes add: outcome.
   139 rememberFailedTest:selector
   137     self lastTestRunResultChanged: outcome selector.
   140 
   138     ^self
   141     | emitChange |
   139 
   142 
   140     "Created: / 20-08-2011 / 12:43:12 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   143     lastTestRunsFailedTests isNil ifTrue:[
   141 !
   144         lastTestRunsFailedTests := Set new.
   142 
   145     ].
   143 rememberedOutcomeFor: selector
   146 
   144 
   147     emitChange := (self removeSelector: selector from: lastTestRunsPassedTests).
   145     lastOutcomes isNil ifTrue:[^nil].
   148     emitChange := (self removeSelector: selector from: lastTestRunsErrorTests) or:[emitChange].
   146     lastOutcomes do:[:outcome|
   149     emitChange := (self addSelector: selector to: lastTestRunsFailedTests) or:[emitChange].
   147         outcome testCase selector == selector ifTrue:[
   150 
   148             ^outcome
   151     emitChange ifTrue:[self lastTestRunResultChanged: selector].
   149         ].
   152 
   150     ].
   153     self rememberFailedTestRun
   151     ^nil
   154 
   152 
   155     "Modified: / 06-08-2006 / 11:01:08 / cg"
   153     "Created: / 20-08-2011 / 14:27:09 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   156     "Modified: / 15-03-2010 / 19:15:38 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   157 !
       
   158 
       
   159 rememberFailedTestRun
       
   160     lastTestRunResultOrNil ~~ false ifTrue:[
       
   161         lastTestRunResultOrNil := false.
       
   162         Smalltalk changed:#lastTestRunResult with:(Array with:self with:nil).
       
   163         self changed:#lastTestRunResult.
       
   164     ]
       
   165 
       
   166     "Modified: / 06-08-2006 / 11:00:42 / cg"
       
   167 !
       
   168 
       
   169 rememberFailedTestRunWithResult:result
       
   170     self rememberFailedTestRun.
       
   171     self rememberFailedTestsFromResult:result.
       
   172 
       
   173     "Modified: / 05-08-2006 / 12:45:19 / cg"
       
   174 !
       
   175 
       
   176 rememberFailedTestsFromResult:result
       
   177     (result failures union:result errors) do:[:eachFailedTest |
       
   178         self rememberFailedTest:(eachFailedTest selector).
       
   179     ].
       
   180 
       
   181     "Created: / 05-08-2006 / 12:45:01 / cg"
       
   182     "Modified: / 06-08-2006 / 10:54:31 / cg"
       
   183 !
       
   184 
       
   185 rememberPassedTest:selector
       
   186 
       
   187     | emitChange |
       
   188 
       
   189     lastTestRunsPassedTests isNil ifTrue:[
       
   190         lastTestRunsPassedTests := Set new.
       
   191     ].
       
   192 
       
   193     emitChange := (self removeSelector: selector from: lastTestRunsFailedTests).
       
   194     emitChange := (self removeSelector: selector from: lastTestRunsErrorTests) or:[emitChange].
       
   195     emitChange := (self addSelector: selector to: lastTestRunsPassedTests) or:[emitChange].
       
   196 
       
   197     emitChange ifTrue:[self lastTestRunResultChanged: selector].
       
   198 
       
   199     self rememberPassedTestRun
       
   200 
       
   201     "Modified: / 06-08-2006 / 11:01:08 / cg"
       
   202     "Modified: / 15-03-2010 / 19:15:46 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   203 !
       
   204 
       
   205 rememberPassedTestRun
       
   206     lastTestRunResultOrNil ~~ true ifTrue:[
       
   207         lastTestRunResultOrNil := true.
       
   208         "/lastTestRunsFailedTests := nil.
       
   209         Smalltalk changed:#lastTestRunResult with:(Array with:self with:nil).
       
   210         self changed:#lastTestRunResult.
       
   211     ]
       
   212 
       
   213     "Modified: / 06-08-2006 / 11:01:22 / cg"
       
   214     "Modified: / 15-03-2010 / 18:22:10 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   215 !
       
   216 
       
   217 rememberPassedTestsFromResult:result
       
   218     (result passed) do:[:eachPassedTest |
       
   219         self rememberPassedTest:(eachPassedTest selector).
       
   220     ].
       
   221 
       
   222     "Created: / 06-08-2006 / 10:29:47 / cg"
       
   223     "Modified: / 06-08-2006 / 11:42:01 / cg"
       
   224 !
   154 !
   225 
   155 
   226 resources
   156 resources
   227 
   157 
   228         ^#()
   158         ^#()
   238 sunitVersion
   168 sunitVersion
   239         ^'4.0'
   169         ^'4.0'
   240 !
   170 !
   241 
   171 
   242 testSelectorError:selector
   172 testSelectorError:selector
   243     ^ lastTestRunsErrorTests notNil and:[lastTestRunsErrorTests includes:selector]
   173 
       
   174     lastOutcomes isNil ifTrue:[^false].
       
   175     lastOutcomes do:[:each|
       
   176         (each testCase class == self 
       
   177             and:[each testCase selector == selector
       
   178                 and:[each result == #error]]) ifTrue:[
       
   179                    ^true
       
   180                 ].                        
       
   181     ].
       
   182     ^false
   244 
   183 
   245     "Created: / 15-03-2010 / 19:44:40 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   184     "Created: / 15-03-2010 / 19:44:40 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   246 !
   185 !
   247 
   186 
   248 testSelectorFailed:selector
   187 testSelectorFailed:selector
   249     ^ (lastTestRunsFailedTests notNil and:[lastTestRunsFailedTests includes:selector]) or:
   188 
   250       [lastTestRunsErrorTests notNil and:[lastTestRunsErrorTests includes:selector]]
   189     lastOutcomes isNil ifTrue:[^false].
   251 
   190     lastOutcomes do:[:each|
   252     "Modified: / 15-03-2010 / 19:44:22 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   191         (each testCase class == self 
       
   192             and:[each testCase selector == selector
       
   193                 and:[each result == #fail]]) ifTrue:[
       
   194                    ^true
       
   195                 ].                        
       
   196     ].
       
   197     ^false
       
   198 
       
   199     "Modified: / 20-08-2011 / 13:02:51 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   253 !
   200 !
   254 
   201 
   255 testSelectorPassed:selector
   202 testSelectorPassed:selector
   256     ^ lastTestRunsPassedTests notNil and:[lastTestRunsPassedTests includes:selector]
   203 
       
   204     lastOutcomes isNil ifTrue:[^false].
       
   205     lastOutcomes do:[:each|
       
   206         (each testCase class == self 
       
   207             and:[each testCase selector == selector
       
   208                 and:[each result == #pass]]) ifTrue:[
       
   209                    ^true
       
   210                 ].                        
       
   211     ].
       
   212     ^false
   257 
   213 
   258     "Created: / 15-03-2010 / 17:58:40 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   214     "Created: / 15-03-2010 / 17:58:40 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   259 ! !
   215 ! !
   260 
   216 
   261 !TestCase class methodsFor:'building suites'!
   217 !TestCase class methodsFor:'building suites'!
   383 
   339 
   384     "Created: / 06-03-2011 / 00:16:06 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   340     "Created: / 06-03-2011 / 00:16:06 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   385 !
   341 !
   386 
   342 
   387 rememberResult:result
   343 rememberResult:result
   388     result hasPassed ifTrue:[
   344 
   389         self rememberPassedTestRun
   345     result outcomesDo:[:outcome|self rememberOutcome: outcome].
   390     ] ifFalse:[
       
   391         self rememberFailedTestRunWithResult:result
       
   392     ].
       
   393 
   346 
   394     "Created: / 05-08-2006 / 12:33:08 / cg"
   347     "Created: / 05-08-2006 / 12:33:08 / cg"
       
   348     "Modified: / 20-08-2011 / 14:02:58 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   395 !
   349 !
   396 
   350 
   397 runTests
   351 runTests
   398     |result|
   352     |result|
   399 
   353 
   629 ! !
   583 ! !
   630 
   584 
   631 !TestCase methodsFor:'running'!
   585 !TestCase methodsFor:'running'!
   632 
   586 
   633 debug
   587 debug
   634         [
   588 
   635             (self class selector: testSelector) runCase.
   589     | testCase outcome |
   636             self class rememberPassedTest: testSelector.
   590 
   637         ] sunitEnsure: [
   591     [
   638             TestResource resetResources: self resources
   592         (testCase := self class selector: testSelector) runCase.
   639         ].
   593         outcome := TestCaseOutcome new.
       
   594         outcome testCase: testCase.
       
   595         outcome result: #pass.
       
   596         outcome remember.
       
   597     ] sunitEnsure: [
       
   598         TestResource resetResources: self resources
       
   599     ].
   640 
   600 
   641     "Modified: / 07-07-2011 / 11:10:50 / jv"
   601     "Modified: / 07-07-2011 / 11:10:50 / jv"
   642     "Modified: / 07-07-2011 / 11:34:08 / Jan Vrany <jan.vrant@fit.cvut,cz>"
   602     "Modified: / 07-07-2011 / 11:34:08 / Jan Vrany <jan.vrant@fit.cvut,cz>"
       
   603     "Modified: / 20-08-2011 / 14:15:51 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   643 !
   604 !
   644 
   605 
   645 debugAsFailure
   606 debugAsFailure
   646         | semaphore |
   607         | semaphore |
   647         semaphore := Semaphore new.
   608         semaphore := Semaphore new.
   828 ! !
   789 ! !
   829 
   790 
   830 !TestCase class methodsFor:'documentation'!
   791 !TestCase class methodsFor:'documentation'!
   831 
   792 
   832 version
   793 version
   833     ^ '$Header: /cvs/stx/stx/goodies/sunit/TestCase.st,v 1.75 2011-08-18 18:37:10 cg Exp $'
   794     ^ '$Header: /cvs/stx/stx/goodies/sunit/TestCase.st,v 1.76 2011-08-20 14:00:44 vrany Exp $'
   834 !
   795 !
   835 
   796 
   836 version_CVS
   797 version_CVS
   837     ^ '$Header: /cvs/stx/stx/goodies/sunit/TestCase.st,v 1.75 2011-08-18 18:37:10 cg Exp $'
   798     ^ '$Header: /cvs/stx/stx/goodies/sunit/TestCase.st,v 1.76 2011-08-20 14:00:44 vrany Exp $'
   838 !
   799 !
   839 
   800 
   840 version_SVN
   801 version_SVN
   841     ^ '§Id: TestCase.st 218 2011-06-13 15:45:06Z vranyj1 §'
   802     ^ '§Id: TestCase.st 218 2011-06-13 15:45:06Z vranyj1 §'
   842 ! !
   803 ! !