In addition to
TestResource, the current version of SUnit contains assertion description strings, logging support, and resumable test failures.
Assertion description strings
TestCase 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| e := 42. self assert: e = 23 description: 'expected 23, got ', e printString
The relevant methods in
#assert:description: #deny:description: #should:description: #shouldnt:description:
The description strings described above may also be logged to a
Stream such as the
Transcript, or a file stream. You can choose whether to log by overriding TestCase»isLogging in your test class; you must also choose where to log by overriding TestCase»failureLog to answer an appropriate stream.
Continuing after a failure
SUnit also allows us to specify whether or not a test should continue after a failure. This is a really powerful feature that uses the exception mechanisms offered by Smalltalk. To see what this can be used for, let’s look at an example. Consider the following test expression:
aCollection do: [ :each | self assert: each even]
In this case, as soon as the test finds the first element of the collection that isn’t
even, the test stops. However, we would usually like to continue, and see both how many elements, and which elements, aren’t
even, and maybe also log this information. You can do this as follows:
aCollection do: [:each | self assert: each even description: each printString , ' is not even' resumable: true]
This will print out a message on your logging stream for each element that fails. It doesn’t accumulate failures, i.e., if the assertion fails 10 times in your test method, you’ll still only see one failure. All the other assertion methods that we have seen are not resumable;
assert: p description: s is equivalent to
assert: p description: s resumable: false.