Section 10.34

Model-based testing

Definition

Model-based testing (MBT) is a testing method that derives tests from a model of the intended behavior: a simplified description of the system that a computer can run. The model is often a finite-state machine §8.33, a set of states plus rules that say, for each state and input, what the system answers and which state comes next. The model supplies both the input sequences and the test oracle §10.5, the check that decides whether each observed response is correct. Hand-written example tests instead fix a few sequences and their expected results.

The idea

Many defects live in sequences, not in single messages. Take a small upload protocol with four commands: BEGIN opens an upload, DATA adds bytes to it, COMMIT stores the bytes, and ABORT throws them away. Each command on its own is easy to parse. The questions that break implementations are about order: what happens on DATA before any BEGIN, on a second BEGIN, on COMMIT after ABORT, or when the connection drops halfway. A person writing example tests checks the orders that person thinks of. The forgotten orders go untested.

The core idea is to write down once what the system should do in every situation, and to let a program enumerate the situations. The model is a map of the intended behavior. A test generator walks the map step by step, and after each step the test checks that the real system answered as the map predicts. A model with two states and four commands answers 2×4=82 \times 4 = 8 questions of the form “in this state, on this command, what happens?”, and a generator can combine those answers into as many sequences as time allows. The picture stops in one respect: a person draws the map. A model that repeats the implementer’s misunderstanding agrees with the faulty implementation, and the test passes.

How it works

A model-based test setup has four parts.

A test runs in these steps.

  1. Write the model from the requirements, not from the code.

  2. Generate input sequences. Offline generation produces whole sequences before any of them runs. Online generation, also called on-the-fly testing, chooses each next input after seeing the last response; it is needed when the model allows several correct responses and the next step depends on which one the system chose.

  3. Reset the system under test to the state that corresponds to the model’s initial state.

  4. For each step, the adapter applies the input and reports the abstract output.

  5. The oracle compares that output with the model. A step fails when the system gives an output the model does not allow for that state and input. Accepting an input the model says must be refused is such a failure, and so is refusing an input the model says must be accepted.

  6. On a failure, report the whole sequence that led to it. That sequence is a counterexample, and delta debugging §10.39 can shorten it.

The comparison happens after every step, not only at the end. A sequence can end in the right final state after passing through a wrong one, and a check of the final result alone misses that. When the model serves as the specification and the question is whether the implementation meets it, model-based testing is a form of conformance testing, which means testing that an implementation conforms to a specification. The Hypothesis library §12.33 for Python §12.23 supports the method under the name stateful testing.

Worked example

The upload protocol has two states, Idle and Receiving. The model keeps two pieces of data: the buffer, which holds the bytes of the upload in progress, and the store, which holds the bytes of the last committed upload. Its eight transitions are these.

A transition tour is one sequence that uses every transition at least once. Starting in Idle, this tour of 9 steps covers all 8 transitions.

  1. DATA(a): expect ERR.

  2. COMMIT: expect ERR.

  3. ABORT: expect ERR.

  4. BEGIN: expect OK; now in Receiving.

  5. BEGIN: expect ERR.

  6. DATA(b): expect OK; the buffer holds b.

  7. ABORT: expect OK; the buffer is empty; now in Idle.

  8. BEGIN: expect OK; now in Receiving again.

  9. COMMIT: expect OK; the store holds an empty upload; now in Idle.

Steps 1 to 3 use the three refusals in Idle, and steps 4, 5, 6, 7 and 9 use the other five transitions, so 3+5=83 + 5 = 8 transitions are covered in 9 steps; step 8 repeats step 4 only to get back to Receiving.

Run the tour against two faulty implementations. Fault 1: the implementation has no guard on DATA, so in Idle it appends the bytes and answers OK. Step 1 fails at once: the model expects ERR and the system answers OK. A test of the message parser alone never sees this fault, because DATA(a) is a well-formed message. Fault 2: ABORT moves to Idle but does not empty the buffer. Every answer in the tour is still right. The fault shows only at step 9, and only if the oracle reads the store: the model expects an empty upload, while the system stores b, the bytes of the aborted upload. An oracle that compares answer codes alone passes this implementation.

The shared mistake. Suppose the model’s author read the code instead of the requirements and wrote “in Idle, DATA answers OK”. The model and Fault 1 now agree, step 1 passes, and the test reports nothing. Generating more sequences does not help, because the generator and the oracle both come from the wrong model.

Stated precisely

Model the intended behavior as a deterministic Mealy machine, a finite-state machine whose output depends on the current state and the current input. It has a finite set of states SS with an initial state s0s_0, a set of inputs II, a set of outputs OO, and two functions: δ(s,x)\delta(s, x) gives the next state and λ(s,x)\lambda(s, x) gives the output for state ss and input xx. For an input sequence w=x1x2…xkw = x_1 x_2 \dots x_k, let λ*(s0,w)\lambda^*(s_0, w) be the sequence of kk outputs the model gives when it starts in s0s_0 and reads ww. Let obs(w)\mathrm{obs}(w) be the sequence of kk abstract outputs the adapter reports when the system under test starts from its reset state and receives ww. The system passes a test suite TT, a finite set of input sequences, when obs(w)=λ*(s0,w)for every w∈T.\mathrm{obs}(w) = \lambda^*(s_0, w) \quad \text{for every } w \in T.

Conformance demands this equality for every input sequence, and there are infinitely many. A finite suite can establish conformance only under an extra assumption about the implementation. Chow’s W-method (1978) builds such a suite [495]. Assume the model is deterministic, defines a transition for every state and input, can reach every state, and has no two states with identical behavior; assume also that the implementation behaves as a deterministic machine with at most mm states and can be reset reliably. Then the implementation passes the suite exactly when it conforms. The suite grows exponentially in m−nm - n, where nn is the number of model states, so mm is a bound the tester assumes, not a fact the tests establish.

When the model allows several correct outputs for the same input, equality is too strict. The input-output conformance relation (ioco) of Jan Tretmans then requires: after every sequence of inputs and outputs that the model allows, every output the implementation can produce next is one the model allows at that point, where producing no output counts as an output. Every pass verdict, in either form, also rests on two things the tests cannot check: the adapter maps concrete events to abstract inputs and outputs correctly, and the model states the behavior that is actually wanted.

Limits and common mistakes

A model and an implementation written from the same misunderstanding agree, as the shared mistake in the example shows. This is a common-mode error §19.2.13: one cause produces the same mistake in two places that were meant to check each other. Model-based testing finds only disagreements, never a requirement that both got wrong.

The model is an abstraction, and what it leaves out is not tested. A model in which every storage write either happens completely or not at all cannot detect a torn write §8.19, a write that a power loss cuts off halfway so that old and new data are mixed. A model without time cannot detect a missed deadline. A model with one client cannot detect a race condition §9.2, an error that depends on how the actions of two clients interleave.

The oracle sees only what the adapter observes. If the adapter treats “the storage function returned success” as “the data is stored”, an implementation that reports success without storing anything passes; the adapter should read the stored data back. A fault that corrupts internal state without changing any observed output stays hidden.

Once a model carries data such as counters and buffers, its number of states grows so fast that no test suite reaches them all; this is state explosion §3.26. The generator then samples sequences, and a coverage figure counts the model’s transitions, not the implementation’s code. A fault that needs a particular history of several steps is found only if some generated sequence contains that history. The tour above found Fault 2 only because it happened to contain DATA, ABORT, BEGIN and COMMIT in that order.

Testing the implementation against the model is verification: it asks whether the system was built right with respect to the model. Checking the model against the requirements is validation: it asks whether the model is the right model. Passing the first gives no evidence for the second.

Questions answered

How is the model validated independently of the implementation?

The model is validated by checking it against the requirements and against independently known cases, using evidence that does not come from the implementation under test. Model validation means checking that the model describes the behavior that is wanted. Testing the implementation against the model cannot do this, because a model that copies the implementation’s mistake agrees with it. The usual methods are these.

Finally, the model’s omissions are written down, such as time, persistent storage, power loss, concurrency §9.1 and a malicious peer, so that a passing result is not read as covering them.

Related entries: Finite-state machine (§8.33), State transition guard (§8.34), Test oracle (§10.5), Fault injection (§10.28), Protocol state machine (§7.37), Model checking (§3.30), Mutation testing (§10.45), Semantic oracle independence and common-mode errors (§19.2.13). Further reading: [495].