5 SUnit: The framework

Now that you see that writing a test is easy, we will take the time to put into perspective the different aspects of SUnit. SUnit is a framework in the sense that it proposes an architecture that can be adapted. We will not cover the techniques to extend the framework but we will present the key classes and discuss some important points.

5.1 Understanding the framework

What we saw in the previous chapter is that a test method defines a test. Now there is a catch:

5.2 During test execution

Figure 5.1 shows the steps during the execution run of a test:

During  run  execution.
Figure 5.1 During run execution.

In fact, the framework ensures that any test method is always surrounded by the execution of the setUp and tearDown methods. Figure 5.2 illustrates this point. It ensures that the fixture is always in a correct state and avoids dependencies between test execution.

setUp and teardDown in action.
Figure 5.2 setUp and teardDown in action.

5.3 The framework in a nutshell

SUnit consists of four main classes: TestCase, TestSuite, TestResult, and TestResource, as shown in Figure 5.3.

The four classes representing the core of SUnit.
Figure 5.3 The four classes representing the core of SUnit.

TestCase

TestCase is an abstract class that is designed to be subclassed. Each of its subclasses represents a group of tests that share a common context: such a group is called a test suite.

Each test is run by creating a new instance of a subclass of TestCase, running setUp to initialize the test fixture, running the test method itself, and then sending the tearDown to clean up the test fixture.

The fixture is specified by instance variables of the subclass and by the specialization of the method setUp, which initializes those instance variables. Subclasses of TestCase can also override method tearDown, which is invoked after the execution of each test, and can be used to release any objects allocated during setUp.

TestSuite

Instances of the class TestSuite contain a collection of test cases. An instance of TestSuite contains tests and other test suites. That is, a test suite contains sub-instances of TestCase and TestSuite. Both individual test cases and test suites understand the same protocol, so they can be treated in the same way (for example, both can be run). This is in fact an application of the Composite pattern in which TestSuite is the composite and the test cases are the leaves.

TestResult

The class TestResult represents the results of a TestSuite execution. It records the number of tests passed, the number of tests failed, and the number of errors signaled.

TestResources

The notion of a test resource represents a resource that is expensive to set-up but which can be used by a whole series of tests. A TestResource specifies a setUp method that is executed just once before a full suite of tests; this is in distinction to the TestCase >> setUp method, which is executed before each test. The class TestResource represents resources that can be shared among a test suite: Subclasses of TestResource are associated with test case classes and this means that all the suite of tests represented by the test case class (See Chapter 6).

5.4 Test states

As we mentioned earlier, the result of a test execution is mainly: passed (meaning that it succeeded), fails (that an assertion is not valid) or error (that an unexpected problem occurred).

Failures vs. Error

In fact, this is really important to understand the difference between a failure and an error. A failure is something that you plan and you check whether is happened or not. When it happens your tests pass, or else it fails.

Removing an element that is not in a set from that set raises an error. If we write the following test, the test will fail.

MyExampleSetTest >> testRemoveNonexistentElement
	empty remove: 5.
Listing 5.1 Testing removal of a non-existing element.

Now using the message should:raise: we make sure that we check that the expression effectively raises an error. It means that our test can either pass (i.e., it means that the test fulfills our requirement here that it should raise an error when removing an unknown element) or fail. We planned such behavior. It can fail or pass.

MyExampleSetTest >> testRemoveNonexistentElement

	self should: [ empty remove: 5 ] raise: NotFound
Listing 5.2 Testing removal of a non-existing element.

For an error this is different, an error occurs in an unplanned manner. The execution of the first version of testRemoveNonexistentElement would lead to an error.

Expected Failures and Skip

In addition, a test can be in two other states: skipped and expected failures. Such states are handy during development.

The idea is that if you write a complex test and suddenly you realize that you will have more work than expected to make your test pass, you can mark it as skipped.

MyExampleSetTest >> testToBeRevisitedLater

	self skip.
	...
Listing 5.3 An example of a skipped test.

In fact, it is much better to use the message skip than to use a guard. Why because the framework can report clearly that your test is skipped. Using a guard as below is the best way to confuse yourself because the framework will return that the test passes while it does NOT.

MyExampleSetTest >> testToBeRevisitedLater

	true ifTrue: [^ self ].
	...
Listing 5.4 A not so nice example of a skipped test.

You can also tag your test with the <expectedFailure> pragma to indicate to the framework that you expect your test to fail. This is to be taken into account by the framework when running and reporting all your tests. Hence you will be aware that some of your tests did not run correctly. Here is an example from Pharo.

testFromClassWhichTraitIsExtendedButNotItself
	"I'm tagging this expected failure because I'm not sure now if it is appropriate to keep 
	 or not with the removal of the 'traits-as-multiple-inheritance' stuff."
	<expectedFailure>

	self queryFromScope: ClyClassScope of: ClyClassWithTraits.
	self assert: resultItems size equals: 0

5.5 Glossary

5.6 Chapter summary

In this chapter we gave an overview of the core of the SUnit framework by presenting the classes TestCase, TestResult, TestSuite and TestResources. We described the setUp/tearDown logic and the different states a test can be in.