Skip to content
Tutorials Center
My learning

Topics

Lesson 35 of 38 ยท Projects and Practice

Project: Build a Mini SUnit

Build a small xUnit framework step by step, the way Kent Beck did in Smalltalk: TestCase, TestResult, TestSuite, setUp and tearDown, with a runnable JavaScript implementation and the matching Smalltalk shape.

The best way to understand a testing framework is to write one. Kent Beck's original SUnit is tiny, and building it shows why setUp, fresh test instances and separate failures and errors exist. In this project you build a working mini-framework in four small steps. The Smalltalk shape of each class is shown read-only, because Smalltalk does not run on this site; the real implementation you run is JavaScript.

Before you start, read Testing with SUnit for the concepts. You will end with a runner that discovers tests, runs them in isolation and prints a report, and you can extend it with the exercises at the end.

The design

Four classes do all the work:

  • TestCase: one test method plus the fixture methods setUp and tearDown.
  • TestResult: collects passes, failures and errors.
  • TestSuite: a collection of tests that run together.
  • A runner: builds a suite from a class and prints the result.

The Smalltalk skeleton of TestCase looks like this (read-only):

SMALLTALK
Object subclass: #TestCase
    instanceVariableNames: 'selector'
    package: 'MiniSUnit'.

TestCase class >> selector: aSymbol
    ^ self new setSelector: aSymbol

TestCase >> run: aResult
    [ self setUp.
      self perform: selector.
      aResult passed: self ]
        on: TestFailure do: [ :ex | aResult failed: self with: ex ]
        on: Error do: [ :ex | aResult errored: self with: ex ].
    self tearDown

Notice that perform: selector is the reflective message from Reflection and Introspection. The framework finds tests by listing selectors that begin with test.

Step 1: TestCase and assertions

Start with the base class and the assertion methods. A failed assertion throws a dedicated exception type, so the runner can tell a wrong answer from a crash.

JS Step 1: assertions

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

No output.

Step 2: TestResult

The result stores outcomes, keeping failures and errors apart.

JS Step 2: collecting results

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

No output.

Step 3: running one test

Now wire run: build a fresh instance, call setUp, run the method, classify what happens, and always call tearDown. Discovery uses reflection over the prototype.

JS Step 3: run one test, discover many

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

No output.

testIsolated proves the key design choice: each test has its own instance and its own setUp, so testPush does not leave an element behind.

Step 4: a suite and a report

Finally, group several test classes and print a report with a final line a human (or a CI server) can read.

JS Step 4: suites and a report

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

No output.

Because TestSuite also has a run(result) method, a suite can contain other suites: that is the Composite pattern from design patterns, and polymorphism makes it work without any type checks.

classDiagram
  class TestCase { +selector +setUp() +tearDown() +run(result) }
  class TestSuite { +tests +add(test) +run(result) }
  class TestResult { +passes +failures +errors }
  TestSuite o-- TestCase : contains
  TestSuite o-- TestSuite : contains
  TestCase ..> TestResult : reports to
  TestSuite ..> TestResult : reports to

Cases and suites answer the same message, so suites nest.

Extensions to try

  1. Add assertCloseTo(actual, expected, tolerance) for floating-point values.
  2. Add a TestResource that is set up once per suite and torn down after it.
  3. Make the runner print the time each test took, and mark slow tests.
  4. Support skip() that records a test as skipped instead of passed.
  5. Add setUpClass and tearDownClass hooks, and discuss when they hurt isolation.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why does the framework create a new TestCase instance for each test?

    A fresh instance plus setUp gives each test an independent fixture.
  2. 2. How does the runner tell a wrong answer from a crash?

    TestFailure marks a false assertion; other exceptions become errors.
  3. 3. Which pattern lets a TestSuite contain other suites?

    Cases and suites share the run: message, so a suite treats both alike.
  4. 4. Why call tearDown in a finally block?

    Resources should be released regardless of the test outcome.

Summary

  • A test framework needs four things: cases, results, suites and a runner.
  • Reflection discovers test* methods; a fresh instance per test isolates state.
  • Failures (wrong answers) and errors (crashes) are different outcomes and are reported separately.
  • Suites are composites: they answer run just like cases.
  • The Smalltalk skeleton is read-only; the JavaScript code is a working implementation.

Next: Project: A Text Adventure Engine