Skip to main content
Engineering LibreTexts

9.7: Advanced Features of SUnit

  • Page ID
    43063
    \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    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
            ].
    
        ...
    

    This page titled 9.7: Advanced Features of SUnit is shared under a CC BY-SA 3.0 license and was authored, remixed, and/or curated by via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.