Skip to content
Tutorials Center
My learning

Topics

Lesson 27 of 38 ยท Testing and Patterns

Testing with SUnit

Learn SUnit, the original xUnit framework: TestCase classes, setUp, assertions, expected failures, test resources and running tests in Pharo. Includes a runnable JavaScript mini-framework stand-in.

SUnit is the testing framework that Kent Beck wrote for Smalltalk. Every later xUnit tool (JUnit, NUnit, PHPUnit, pytest's classes) copies its design. In Smalltalk, a test is a method on a subclass of TestCase, run by a test runner that reports green, failed and error counts. Because the browser, the debugger and the runner all share one live image, writing tests feels very direct.

Smalltalk is not available on this site, so the SUnit code below is read-only. At the end, a JavaScript mini-framework gives you something to run and extend, and the project lesson Build a Mini SUnit goes further.

A first test case

A test class inherits from TestCase. Test methods start with test and take no arguments (read-only):

SMALLTALK
TestCase subclass: #AccountTest
    instanceVariableNames: 'account'
    package: 'Bank-Tests'.

AccountTest >> setUp
    account := Account new.
    account deposit: 100

AccountTest >> testBalanceStartsFromDeposit
    self assert: account balance equals: 100

AccountTest >> testWithdrawReducesBalance
    account withdraw: 30.
    self assert: account balance equals: 70

AccountTest >> testOverdrawRaisesError
    self should: [ account withdraw: 500 ] raise: Error

The runner creates a new instance of the test case for each test method, calls setUp, runs the method, and then calls tearDown. Tests never share instance state, which keeps them independent.

Assertions

The common messages:

Message Passes when
assert: aBoolean the condition is true
assert: a equals: b a = b, and the failure message shows both values
deny: aBoolean the condition is false
should: aBlock raise: Error the block signals that exception
shouldnt: aBlock raise: Error the block finishes without that exception
should: aBlock raise: Error withMessageText: 'x' exception text matches
fail: 'reason' never; marks the test as failed

Prefer assert:equals: over assert: (a = b). When it fails you see "Expected 70 but was 100" rather than just "Assertion failed".

Failures and errors

SUnit separates two outcomes that are easy to confuse:

  • A failure is an assertion that was false: the code ran but gave the wrong answer.
  • An error is an unexpected exception, such as ZeroDivide or MessageNotUnderstood: the code could not finish.

A test that is neither is green. In the runner, red means failure, orange or yellow means error. Click a failing test to open the debugger right on the assertion, as described in the inspector, debugger and playground.

Fixtures, resources and test-first

setUp and tearDown build and clean the fixture. Expensive shared data (a database, a started server) can be provided once through a TestResource, which has its own setUp and tearDown and a lazily created singleton. Use it sparingly: shared state makes tests depend on each other.

Write the test before the code. Run it, watch it fail, and use the debugger's Create button to write the method it needs (the workflow in test-driven development in the debugger). Then run all tests of the package; they take milliseconds, so run them constantly.

A JavaScript mini SUnit

The stand-in below follows the same design: a TestCase base class, a fresh instance per test* method, setUp, assertions, failure versus error, and a runner that prints a summary.

JS Stand-in: a tiny SUnit

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The last two tests are deliberately wrong: one is a failure (wrong value), the other an error (the method does not exist). The runner reports them separately, as SUnit does.

What makes a good test

  • One reason to fail. A test checks one behaviour, named for it (testWithdrawReducesBalance).
  • Independent. Any order, any subset: never rely on another test having run.
  • Fast and deterministic. No sleeping, no real network, no random numbers unless seeded.
  • Readable. Arrange, act, assert, in that order, with a fixture that does not hide the important data.
  • Close to the code. Keep test packages next to the code (Bank-Core and Bank-Tests), as the Metacello baseline does.
JS Stand-in: independence check

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The second test of each run fails because both share a list. With SUnit's fresh instance per test and a proper setUp, the problem disappears.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. How does SUnit isolate test methods?

    A fresh instance per test means instance variables never leak between tests.
  2. 2. What is the difference between a failure and an error?

    Failures mean wrong answers; errors mean the code could not finish.
  3. 3. Which assertion gives the most helpful failure message for comparing values?

    assert:equals: prints expected and actual values.
  4. 4. Which message checks that a block signals an exception?

    should: aBlock raise: ExceptionClass passes when the exception is signalled.

Summary

  • SUnit tests are test* methods on TestCase subclasses; setUp builds the fixture.
  • Each test method runs in its own new instance, so tests stay independent.
  • Use assert:equals:, deny: and should:raise:; know the difference between failures and errors.
  • Write tests first and drive implementation from the debugger.
  • The SUnit code is read-only; the mini framework is a JavaScript stand-in.

Next: Test-Driven Development in the Debugger