SUnit: Unit Tests in Pharo

Damien Cassou, Stéphane Ducasse and Luc Fabresse http://stephane.ducasse.free.fr

Agile Programming

Pharo programming culture adheres to agile programming

SUnit

A Test

In a test, we

Set TestCase

TestCase subclass: # SetTestCase
    ...
SetTestCase >> testAdd 
  | empty |
  empty := Set new.   "Context"
  empty add: 5.   "Stimulus"
  empty add: 5.
  self assert: empty size = 1.   "Check"

SetTestCase run: #testAdd

In a Subclass of TestCase

Each method starting with test*:

The results of the test are collected in a TestResult object

Another Example

testAdjacentRunsWithEqualsAttributesAreMerged
  "this demonstrates that adjancent runs with equal attributes are merged. "
  | runArray |
  runArray := RunArray new.
  runArray
    addLast: TextEmphasis normal times: 5;
    addLast: TextEmphasis bold times: 5;
    addLast: TextEmphasis bold times: 5.
  self assert: (runArray runs size = 2).

Failures and Errors

To Test That an Error Must Be Raised

SetTestCase >> removeElementNotInSet
  self 
    should: [ Set new remove: 1 ] 
    raise: NotFound

To Test That an Error Must Not Be Raised

SetTestCase >> removeElementNotInSet
  self 
    shouldnt: [ Set new add: 1 ] 
    raise: NotFound

Duplicating the Context

SetTestCase >> testOccurrences
  | empty |
  empty := Set new.
  self 
    assert: (empty occurrencesOf: 0) 
    equals: 0.
  empty add: 5; add: 5.
  self 
    assert: (empty occurrencesOf: 5) 
    equals: 1

setUp and tearDown Messages

Defining a setUp Method

SetTestCase >> setUp
  empty := Set new

setUp is executed for you before any test execution

SetTestCase >> testOccurrences
  self 
    assert: (empty occurrencesOf: 0) 
    equals: 0.
  empty add: 5; add: 5.
  self 
    assert: (empty occurrencesOf: 5) 
    equals: 1

SUnit Core

TestSuite, TestCase and TestResult

Test Resources

What You Should Know

Summary

/