Skip to content

Testing multiple state machines

SCUnit supports unit testing of multiple state machines. A SCTUnit test is defined for a state machine type which is the test instance of the unit test.

If the statechart defines variables which refer to other statechart types then the test environment will care about setting up the test scenario which includes statechart instances of the involved statechart types. Here the same instantiation strategy as described for the simulation is applied.

If the statechart under test does not control the life cycle of the referenced state machines, then this can simply be done by the test case itself.

An simple test case for the traffic control example could look like this.

testclass TwoWayTrafficControl for statechart TwoWayTrafficControl {
	@Test operation testStartUp() {
		enter  
		trafficLightA.^enter
		trafficLightB.^enter
		assert active (main.Off)
		assert trafficLightA.isStateActive(TrafficLightStates.Off)
		assert trafficLightB.isStateActive(TrafficLightStates.Off)
	}
}

In SCTUnit state machines must be activated explicitly. this is also true for the state machine under test which is a TwoWayTrafficControl state machine instance. The machine under test is activated by the enter statement while the two TrafficLight state machine instances are activated by calling the enter operation on the reference.

The following assertions check if the initial states were activated. This is done by using the built-in function active for the machine under test while the referenced machines are tested using the isStateActive operations.

To be able to call the triggerWithoutEvent operation for EventDriven statecharts a variable have to be defined for the statechart with the type of Statemachine. After that through this variable, every operation defined by the statechart type will be accessible.

Use the SCTUnit built-in functions for the state machine under test and use the operations defined by the state machine types for the referenced state machines.