Skip to main content
Engineering LibreTexts

7.7: Advanced Features of SUnit

  • Page ID
    38017
  • \( \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}}\)

    In addition to TestResource, the current version of SUnit contains assertion description strings, logging support, and resumable test failures.

    Assertion description strings

    The 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 TestCase are:

    #assert:description:
    #deny:description:
    #should:description:
    #shouldnt:description:
    

    Logging support

    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.


    This page titled 7.7: Advanced Features of SUnit is shared under a CC BY-SA 3.0 license and was authored, remixed, and/or curated by Andrew P. Black, Stéphane Ducasse, Oscar Nierstrasz, Damien Pollet via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.