Skip to main content
Engineering LibreTexts

9.5: The SUnit Cookbook

  • Page ID
    39623
    \( \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 JUnit, 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, TestAsserter >> assert:description: and TestAsserter >> deny:description: take a second argument which is a message string that describes the reason for the failure, if it is not obvious from the test itself. These methods are described in Section 9.7.

    Next, SUnit provides two additional methods, TestAsserter >> should:raise: and TestAsserter >> 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. The method below illustrates the use of should:raise:.

    MyExampleSetTest >> testIllegal
        self should: [ empty at: 5 ] raise: Error.
        self should: [ empty at: 5 put: #zork ] raise: Error
    

    Try running this test. Note that the first argument of the should: and shouldnt: methods is a block that contains the expression to be executed.

    Running a single test

    Normally, you will run your tests using the Test Runner or using your code browser. If you don’t want to launch the Test Runner from the World menu, you can execute TestRunner open. You can also run a single test as follows:

    MyExampleSetTest 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:

    MyExampleSetTest 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 SUnit 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, however, that you not try to do this. The framework is there: use it.


    This page titled 9.5: The SUnit Cookbook 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.