|
| SUnit |
Introduction
SUnit is the system of first choice for automatic testing. It is dialect neutral, so that it will run on most Smalltalks. The idea is from XP. Important is that the tests are arbitrarily repeatable, that the developer does not have to intervene during the test run, that each test examines a certain aspect of the code and thereby also documents it and that changes at the code rarely require changes at the tests.
Attention
This Tutorial was tested in Squeak 3.4 with SUnit 3.1 from SqueakMap. Consider the comments with the installation of SUnit please.
Important Messages
- TestCase
- setUp
- run
- assert:
- deny:
- should:raise:
- shouldnt:raise:
Example
If you want to test your class, then write a new class of the form:
TestCase subclass: #MyTestCase
instanceVariableNames: 'myTestVariable'
classVariableNames: ''
poolDictionaries: ''
category: 'SUnitTests'
Before you will start a test, your test class usually must be prepared for it. Create the method setUp. Let's say, we want to test basic operations of arithmetic on Integers. We first need a number.
MyTestCase>>setUp
myTestVariable := 2.
Now we want to test whether addition works.
MyTestCase>>testAddition
self assert: (myTestVariable + 1 = 3).
self deny: (myTestVariable + 1 = 2).
Thus we test whether a positive example works and whether a negative example fails. assert: and deny: expect thereby Boolean as answer. If this should not be the case, then in the result an error is noted. Should assert: receive false, or deny: true, then the test is considered as failed. Is there an error you might have written wrong code. If it fails the code doesn't do what you expect. Both can be a bug.
Further we want to test, whether an exception is thrown correctly, e.g. with division by zero.
MyTestCase>>testDivisionByZero
self should: [ myTestVariable // 0 ] raise: TestResult error.
self shouldnt: [ myTestVariable // 2 ] raise: TestResult error.
It is helpful to provoke an error and inspect the Result of the test.
User interface
Naturally there is a user interface. It can be opened with TestRunner open. Our class should be listed there, if not, you should try to refresh the TestRunner. You can start the test with 'run' or 'run all'. Have fun!!! |
|
|