9.7: Advanced Features of SUnit
- Page ID
- 43063
In addition to TestResource
, SUnit contains assertion description strings, logging support, the ability to skip tests, and resumable test failures.
Assertion description strings
The TestAsserter
assertion protocol includes a number of methods that allow the programmer to supply a description of the assertion. The description is a String
; if the test case fails, this string will be displayed by the test runner. Of course, this string can be constructed dynamically.
... e := 42. self assert: e = 23 description: 'expected 23, got ', e printString ...
The relevant methods in TestAsserter
are:
assert:description: deny:description: should:description: shouldnt:description:
Using assert:equals:
In addition to assert:
, there is also assert:equals:
that offers a better report in case of error (incidentally, assert:equals:
uses assert:description:
).
For example, the two following tests are equivalent. However, the second one will report the value that the test is expecting: this makes easier to understand the failure. In this example, we suppose that aDateAndTime
is an instance variable of the test class.
testAsDate self assert: aDateAndTime asDate = ('February 29, 2004' asDate translateTo: 2 hours). testAsDate self assert: aDateAndTime asDate equals: ('February 29, 2004' asDate translateTo: 2 hours).
Logging support
The description strings mentioned above may also be logged to a Stream
, such as the Transcript
or a file stream. You can choose whether to log by overriding isLogging
in your test class; you must also choose where to log by overriding failureLog
to answer an appropriate stream. By default, the Transcript
is used to log.
Skipping tests
Sometimes in the middle of a development, you may want to skip a test instead of removing it or renaming it to prevent it from running. You can simply invoke the TestAsserter
message skip
on your test case instance. For example, the following test uses it to define a conditional test.
OCCompiledMethodIntegrityTest >> testPragmas | newCompiledMethod originalCompiledMethod | (Smalltalk globals hasClassNamed: #Compiler) ifFalse: [ ^ self skip ]. ...