Statechart unit testing

About unit testing

Like any executable software artefacts, statecharts can and should be tested during their development. Like most other software libraries, the execution of Sismic can be checked with unit tests, including the execution of statecharts with Sismic.

There are many unit testing frameworks in Python. Internally, Sismic relies on pytest to check its own implementation as well as the execution of the statecharts provided as examples in the documentation.

Sismic API (especially the interpreter API) is open enough to make advanced unit testing possible. To ease the writing of unit tests, Sismic is bundled with sismic.testing module providing a set of common test primitives that can be used independently of a specific test framework.

Of course, these primitives do not cover all use cases, but cover the most frequent assertions that can be expected when the execution of a statechart has to be checked. Also, because some primitives are very easy to implement using the existing API, they are not included with this module.

For example:

  • State ‘name’ is active: 'name' in interpreter.configuration;

  • Variable ‘x’ has value y: interpreter.context['x'] = y;

  • Statechart is in a final configuration: interpreter.final;

Primitives for unit testing

sismic.testing.state_is_entered(steps, name)

Holds if state was entered during given steps.

Parameters
  • steps (Union[MacroStep, List[MacroStep]]) – a macrostep or list of macrosteps

  • name (str) – name of a state

Return type

bool

Returns

given state was entered

sismic.testing.state_is_exited(steps, name)

Holds if state was exited during given steps.

Parameters
  • steps (Union[MacroStep, List[MacroStep]]) – a macrostep or list of macrosteps

  • name (str) – name of a state

Return type

bool

Returns

given state was exited

sismic.testing.event_is_fired(steps, name, parameters=None)

Holds if an event was fired during given steps.

If name is None, this function looks for any event. If parameters are provided, their values are compared with the respective attribute of the event. Not all parameters have to be provided, as only the ones that are provided are actually compared.

Parameters
  • steps (Union[MacroStep, List[MacroStep]]) – a macrostep or list of macrosteps

  • name (Optional[str]) – name of an event

  • parameters (Optional[Mapping[str, Any]]) – additional parameters

Return type

bool

Returns

event was fired

sismic.testing.event_is_consumed(steps, name, parameters=None)

Holds if an event was consumed during given steps.

If name is None, this function looks for any event. If parameters are provided, their values are compared with the respective attribute of the event. Not all parameters have to be provided, as only the ones that are provided are actually compared.

Parameters
  • steps (Union[MacroStep, List[MacroStep]]) – a macrostep or list of macrosteps

  • name (Optional[str]) – name of an event

  • parameters (Optional[Mapping[str, Any]]) – additional parameters

Return type

bool

Returns

event was consumed

sismic.testing.transition_is_processed(steps, transition=None)

Holds if a transition was processed during given steps.

If no transition is provided, this function looks for any transition.

Parameters
  • steps (Union[MacroStep, List[MacroStep]]) – a macrostep or list of macrosteps

  • transition (Optional[Transition]) – a transition

Return type

bool

Returns

transition was processed

sismic.testing.expression_holds(interpreter, expression)

Holds if given expression holds.

Parameters
  • interpreter (Interpreter) – current interpreter

  • expression (str) – expression to evaluate

Return type

bool

Returns

expression holds