Model-based testing (MBT)

Model-based testing (MBT) is an approach in which test cases are derived, usually by a tool, from a model of how the system is expected to behave, instead of being written one at a time by hand. The model may be a state machine, a decision table or an activity diagram, and it becomes the thing the team maintains.

The longer version

The ISTQB glossary's definition is six words long: testing based on or involving models. In practice the phrase means something narrower. Somebody describes the expected behaviour of the system in a form a machine can read, and test cases are produced from the description. A state machine suits behaviour that depends on history, a decision table suits rules that combine conditions, and an activity diagram suits a process with branches and loops. State transition testing and decision table testing use the same kinds of model, built by a tester who then derives the cases by hand. Model-based testing takes the second half of that work away from the tester. A generator walks the model, and each walk becomes a test: the sequence of events supplies the steps, and the states the model says the system should pass through supply the expected results. The model therefore serves as the test oracle as well as the source of inputs.

Take a parcel locker with five states: empty, reserved, loaded, overdue and out of service. Events such as reserve, deposit, collect and time out move it between them, and the whole model fits on a page. From it a generator can produce one test for every transition, a longer set covering every pair of consecutive transitions, or random walks of a hundred steps. The team chooses among these by naming a coverage criterion and not by writing cases, and it has to choose, since a model with a loop in it allows endlessly many walks and some rule must say when to stop. Generated tests are abstract. Deposit in the model has to be turned into a door sensor reading or an API call, and that translation layer, usually called an adapter, is written and maintained by hand. When a rule changes, for instance a parcel left for three days now goes back to the sender, the tester edits one transition and regenerates the suite. Nobody searches forty scripts for the ones that assumed the old rule.

The costs fall in different places from those of hand-written tests. Building the model takes skill and time before a single test runs, and a state machine is harder for a product owner to review than a list of test cases. A model drawn from the code repeats whatever the code gets wrong, so it has to come from the requirements. Writing it that way tends to expose questions the requirements never answered, a benefit that arrives before any test is generated. A wrong model is worse than a wrong test, since every test generated from it carries the same mistake. The model also has to be kept in step with the product, and one that falls behind produces failures nobody trusts. A generator will readily produce more tests than can be run, which brings back the selection problem the model was meant to remove. The approach pays where behaviour is rich in states and changes often, and costs more than it returns on a simple form.

Generation can happen before the run or during it. In offline model-based testing the generator writes out a complete set of test sequences first, and they are reviewed, stored and run like any other suite. In online model-based testing the generator picks each next step while the system is running, which suits behaviour that cannot be fully predicted in advance. GraphWalker, an open-source tool that reads models as directed graphs, offers both, a command line for offline batch generation and online services for live execution. Its stop conditions show how the coverage choice is written down in practice: edge or vertex coverage, reaching a named element, a time limit, a path length or requirement coverage, alone or combined. On a state machine, vertex coverage is state coverage and edge coverage is transition coverage, and the two are far apart. In Abstracta's worked example of a light switch, a single test visits every state, while covering every transition takes three. A further kind of model, the Markov chain, weights each transition by how likely it is, so the generated tests can follow the journeys users make most often.

Common questions

What is model-based testing?

Model-based testing is an approach in which test cases are generated from a model of the system's expected behaviour. The team describes the behaviour once, as a state machine, a decision table or a similar diagram, and a tool derives the sequences to run and the results to expect. The ISTQB glossary defines it as testing based on or involving models, and abbreviates it MBT.

What kinds of models are used in model-based testing?

State machines suit behaviour that depends on what has already happened. Decision tables model rules that combine several conditions, activity diagrams and flowcharts model processes with branches and loops, and a grammar can model the structure of valid input. The ISTQB glossary calls any model used in model-based testing an MBT model. One system may need more than one of them.

How is model-based testing different from state transition testing?

State transition testing is a design technique: a tester draws the states and events and derives the cases from the drawing by hand. Model-based testing is a way of working in which the drawing is kept in a form a tool can read and the cases are generated from it, then regenerated whenever it changes. The first can be done on paper for one feature, and the second treats the model as a maintained asset.

What are the disadvantages of model-based testing?

The model costs effort before any test exists, and it needs people who can build and read it. An error in the model is copied into every generated test. An adapter must translate the model's abstract steps into real actions and has to be maintained beside the product. A generator can also emit far more tests than there is time to run, so a coverage rule still has to be chosen.

When is model-based testing worth using?

The approach earns its cost where behaviour has many states or rule combinations, where it changes often, and where the same logic must be tested through several interfaces, since one model can serve each of them through its own adapter. Protocols, device controllers, booking flows and payment flows are typical subjects. For a stable feature with a handful of cases, writing the tests directly is quicker than modelling it.

What is the difference between online and offline model-based testing?

Offline model-based testing generates the whole set of test sequences before anything runs, so the suite can be reviewed, versioned and replayed. Online model-based testing generates each step during execution, choosing the next move while connected to the running system. GraphWalker supports both: a command line for offline batch generation, and services over REST and WebSocket for online, live execution.

What tools are used for model-based testing?

GraphWalker is an open-source tool that generates paths from directed-graph models, with generators such as random walks and A* and stop conditions such as full edge coverage or a time limit. BrowserStack's list of model-based testing tools also names Conformiq, Tricentis Tosca and IBM Rational Test Workbench among others. Whatever the tool, the adapter that turns abstract steps into real actions is written by the team.

Bug reports that arrive ready to triage.

Collect replay, console, network and browser context with the Free plan.

<script src="…/widget.js" data-key="pb_pub_…">