27.07.2013 Views

2 Why We Need Model-Based Testing

2 Why We Need Model-Based Testing

2 Why We Need Model-Based Testing

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

128 Structuring <strong>Model</strong> Programs with Features and Composition<br />

Now we introduce another style, the FSM model program. Figure 7.12 shows<br />

M1 coded in this style. It also uses C#, but instead of coding state variables and<br />

action methods, we code the FSM as an array of strings where each string describes<br />

one transistion, in the format "t(current state, action, next state)". The<br />

current state in the first transition is the initial state. In the factory method, the array<br />

of strings is passed to the FSM.Create method, which returns an instance of the FSM<br />

class. Another method in this class can be used to identify the accepting states, if<br />

any. The FSM instance is passed to the constructor for the Fsm<strong>Model</strong>Program class,<br />

which is also derived from <strong>Model</strong>Program. An FSM model program is compiled and<br />

displayed in exactly the same way as a library model program.<br />

Now we introduce a third style, the FSM text file, which is the most compact<br />

(Figure 7.13). This style dispenses with C# and assemblies altogether and just puts<br />

the FSM in a text file, in a parenthesized format. The first element inside the outer<br />

parentheses is the initial state. The second element is a sequence of accepting states<br />

(which is left empty here, which indicates there are no accepting states). It is not<br />

necessary to compile an FSM file. To display the FSM in the file, invoke mpv with<br />

the /fsm option and name the file:<br />

mpv /fsm:M1.txt<br />

In practice, we rarely write an FSM model program, we just write an FSM text<br />

file instead. When you invoke a tool with the /fsm option, it reads the FSM from<br />

the file and constructs a Fsm<strong>Model</strong>Program instance in much same way as shown in<br />

Figure 7.12. The tools do require you to write a factory method for library model<br />

programs (rather than just invoking the constructor automatically) because this is<br />

where you identify the features you want to include.<br />

The tools do not depend on the source form of model programs. They access every<br />

model program in a uniform way through an instance of the <strong>Model</strong>Program class,<br />

which always provides certain properties and methods, including InitialState,<br />

GetActions, GetTargetState, and so on. (Chapter 6, Section 6.2.4). As a result,<br />

every style of model program can be composed, explored, viewed, and used for<br />

testing in the same way. From the tools’ point of view, the differences between<br />

model program styles are hidden by derived types such as Library<strong>Model</strong>Program and<br />

Fsm<strong>Model</strong>Program. It is possible to support model programs written in other styles<br />

and languages by coding additional types derived from <strong>Model</strong>Program. For example,<br />

you could support model programs that are expressed as diagrams rather than text.<br />

7.3.3 Scenario model programs<br />

Now that we understand composition and know how to write scenario model programs<br />

easily, we are ready to use composition for scenario control.<br />

more free ebooks download links at:<br />

http://www.ebook-x.com

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!