Skip to main content
Engineering LibreTexts

7.5: The SUnit Cook Book

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

    This section will give you more details on how to use SUnit. If you have used another testing framework such as JUnit1, much of this will be familiar, since all these frameworks have their roots in SUnit. Normally you will use SUnit’s GUI to run tests, but there are situations where you may not want to use it.

    Other assertions

    In addition to assert: and deny:, there are several other methods that can be used to make assertions.

    First, assert:description: and deny:description: take a second argument which is a message string that can be used to describe the reason for the failure, if it is not obvious from the test itself. These methods are described in Section 7.7.

    Next, SUnit provides two additional methods, should:raise: and shouldnt:raise: for testing exception propagation. For example, you would use (self should: aBlock raise: anException) to test that a particular exception is raised during the execution of aBlock. Code \(\PageIndex{1}\) illustrates the use of should:raise:.

    \(\bigstar\) Try running this test.

    Note that the first argument of the should: and shouldnt: methods is a block that contains the expression to be evaluated.

    Code \(\PageIndex{1}\) (Squeak): Testing Error Raising

    ExampleSetTest»testIllegal
        self should: [empty at: 5] raise: Error.
        self should: [empty at: 5 put: #zork] raise: Error
    

    SUnit is portable: it can be used from all dialects of Smalltalk. To make SUnit portable, its developers factored-out the dialect-dependent aspects. The class method TestResult class»error answers the system’s error class in a dialect-independent fashion. You can take advantage of this: if you want to write tests that will work in any dialect of Smalltalk, instead of Code \(\PageIndex{1}\) you would write:

    Code \(\PageIndex{2}\) (Squeak): Portable Error Handling

    ExampleSetTest»testIllegal
        self should: [empty at: 5] raise: TestResult error.
        self should: [empty at: 5 put: #zork] raise: TestResult error 
    

    \(\bigstar\) Give it a try.

    Running a single test

    Normally, you will run your tests using the Test Runner. If you don’t want to launch the Test Runner from the open. . . menu or from the Tools flap, you can execute TestRunner open as a print it.

    You can run a single test as follows.

    ExampleSetTest run: #testRemove −→ 1 run, 1 passed, 0 failed, 0 errors
    

    Running all the tests in a test class

    Any subclass of TestCase responds to the message suite, which will build a test suite that contains all the methods in the class whose names start with the string “test”. To run the tests in the suite, send it the message run. For example:

    ExampleSetTest suite run −→ 5 run, 5 passed, 0 failed, 0 errors
    

    Must I subclass TestCase?

    In JUnit you can build a TestSuite from an arbitrary class containing test* methods. In Smalltalk you can do the same but you will then have to create a suite by hand and your class will have to implement all the essential TestCase methods like assert:. We recommend that you not try to do this. The framework is there: use it.


    1. junit.org

    This page titled 7.5: The SUnit Cook Book 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.