next up previous contents index
Next: Bibliography Up: 4 Style Guide Previous: 4.5 Type-Annotating Expressions   Contents   Index

4.6 Writing Test Cases

Slate includes a port of Smalltalk's SUnit unit-testing framework (implemented in src/lib/test.slate), as well as a collection of unit tests (in tests/*.slate). Three namespaces are provided in which users may place their TestCases:

testing UnitTests
should contain unit-test cases;
testing RegressionTests
is for test cases covering bug fixes, for detecting regressions; and
testing BenchmarkTests
is to contain test cases for measuring the speed of the system.
There is a method testing runAllTests which runs all TestCases found in a recursive search of the UnitTests and RegressionsTests namespaces.

To write your own test cases, add a prototype derived from TestCase to the namespace you've chosen, within the appropriate container - either UnitTests or RegressionTests. For example, suppose you are writing test cases covering Ranges; you might write

UnitTests addPrototype: #Range derivedFrom: {TestCase}.
Once your prototype has been constructed, add tests to it. Test methods are unary methods with selectors beginning with test. For instance,

tc@(UnitTests Range traits) testInclusion1

"Verify that all elements of an Range are found in that Range."

[| range |

  range: (25 to: 50 by: 1).

  tc assert: (range allSatisfy: [| :item | range includes: item]).

].

The important assertion methods are

assert: Boolean

deny: Boolean

should: Method

should: Method raise: Condition

shouldnt: Method

shouldnt: Method raise: Condition

and variants with an additional description: String argument.

Once all your test methods are defined, a suite method should be defined that constructs a TestSuite for exercising the TestCase:

t@(UnitTests Range traits) suite

[t suiteForSelectors: {

  #testInclusion1.

  ``... etc. ...''

}].

See also the method TestCase suiteForSelectors:. The suite method is called by runSuite, which in turn is called by runAllTests.

At this point, invoking testing runAllTests will exercise your new code.


next up previous contents index
Next: Bibliography Up: 4 Style Guide Previous: 4.5 Type-Annotating Expressions   Contents   Index
Brian Rice 2005-11-21