State-based testing
Operation of many automatic machines can be described in the form of states and state transitions.
An automatic coffee machine has a certain state in which it waits for a button to be pressed by the user. While being in this state, e.g. the text "Ready" is shown on the display.
As soon as the user presses the relevant button, a certain amount of coffee beans is ground and put into the brewing unit. Finally, hot water is pumped through and the cup is filled. Once a certain amount of water is extracted, used coffee is put into the tray. Ultimately, the coffee machine is ready for brewing another cup of coffee. Pressing of a button is a transition from one state to another, since the coffee machine leaves a certain state and brews a cup of coffee.
A transition from one state to another also takes place when the transition is from state A without a detour via state B directly to state A.
In the simplest case, one could say that an automatic coffee machine has the states "Ready" and "In Progress". You can also switch the coffee machine on and off so that there is a third status, i.e. "Off".
According to ISTQB, it is recommended to design at least as many test cases as possible so that each state and each state transition occur at least in each test case.
In this case, there is a loop leading from "Ready" to "In Progress" and back to "Ready". This loop could be tested several times since the end user may want to brew more than one cup of coffee between switching the machine on and off.
In order to run a comprehensive test of the coffee machine, a chain made of states and state transitions would have to be tested. In this simple example it is not possible due to loops.
The ISTQB syllabus recommends in this case that n-Switch-Coverage be used as the criterion for managing the number of necessary tests. The quotient of tested n-Switches and the number of possible n-Switches is specified and presented as percentage. In this case n stands for a numerical value that can be 0 or greater than 0. The lower value n has, the lesser test effort is needed.
A 0-Switch is each possible path from a start state via state transition to an end state, with the reservation that the start state and the end state can also be identical. This coffee machine has exactly five 0‑Switches:
-
Off –switch–on→ Ready
-
Ready –switch–off→ Off
-
Ready –press–the–select–button→ In Progress
-
In Progress –processing–finished→ Ready
-
In Progress –switch–off→ Off
A 1-Switch is every possible path from start state via state transition to an intermediary transition and from there via a state transition to an end state. Here, the states and state transitions may be the same and all possible combinations can be created. Eight 1‑switches can be found for this automatic coffee machine:
-
Off –switch–on→ Ready –switch–off→ Off
-
Off –switch–on→ Ready –press–the–select–button→ In Progress
-
Ready –switch–off→ Off –switch–on→ Ready
-
Ready –press–the–select–button→ In Progress –processing–finished→ Ready
-
Ready –press–the–select–button→ In Progress –switch–off→ Off
-
In Progress –processing–finished→ Ready –switch–off→ Off
-
In Progress –processing–finished→ Ready –press–the–select–button→ In Progress
-
In Progress –switch–off→ Off –switch–on→ Ready
It can also be said, in general, that an n‑Switch is a path from a start state to an end state via n intermediate states with the reservation that only defined state transitions can be used for the state transition. It means that there can be no path in which the "Off" state is followed directly by the "In Progress" state.
Analogously, 13 2‑switches, 21 3‑switches and 34 4‑switches can be derived from the state diagram. It is clear that the number can increase quickly with intensity being always dependent on specific state machine.
Depending on what kind of system it is, the test analyst should select the right coverage and analogously create test cases.