Lecture 5 Model-Based Testing What is model-based testing? • Model-based testing can be conducted as part of model-based development • Model-based development = describing software using accurate modeling languages e.g. UML • If a model describes the software we can use this model to construct test cases. • C.f. reference models in lecture 4. Modeling Maturity Level • Level 0: No specification: software specifications are only in the heads of developers • Level 1: Textual: The software specifications are written down in informal natural language documents • Level 2: Models with text: a set of models (diagrams or text with well defined meanings). Natural language is used to motivate, explain and detail models. Transition from model to code is manual. Model synching with code is difficult. • Level 4, Precise models: code can be generated from models, and modified for special requirements. Model has a precise meaning independent of natural language. Natural language still used. • Level 5, Models only: model is like a high-level programming language. Model-to-code generation automatic (compiler). Generated code used without changes. UML Statecharts • A UML statechart, is an object-based variant of Harel’s statechart language. • Statecharts overcome limitations of finite state machines, without losing benefits. • Combine aspects of Moore and Mealy machines • New concepts: – – – – Hierarchically nested states Orthogonal regions Extended actions History Classical automata input X clock Internal state Z output Y Moore- + Mealy automata=finite state machines (FSMs) Next state Z+ computed by function Output computed by function • Moore-automata: Y = (Z); Z+ = (X, Z) • Mealy-automata Y = (X, Z); Z+ = (X, Z) Z0 0 e=1 Z3 3 e=1 Z1 1 e=1 Z2 e=1 2 UML 2.4 • Two kinds of state machines. • Behavioral state machines are used to model the behavior of individual entities (e.g., class instances) • Protocol state machines are used to express usage protocols and can be used to specify the legal usage scenarios of classifiers, interfaces, and ports. • Behavioral state machine is subclassed by protocol state machine. Behavioral Machines • Behavior is modeled as traversal of a graph of state nodes connected by transitions, tiggered by a series of events. Behavioural States • Model a a situation during which some (usually implicit) invariant condition holds. The invariant may represent a static situation such as an object waiting for some external event to occur. • Can also model dynamic conditions such as the process of performing some behavior Simple States • A simple state is a state that does not have substates • Rectangle with rounded corners and the state name inside the rectangle. • Simple state may have compartments. • Name compartment holds the (optional) name of the state, as a string. States without names are called anonymous states and are all considered distinct (different) states. • Internal activities compartment holds a list of internal actions or state activities performed while the element is in the state. Activity label identifies the circumstances under which the behavior specified by the activity expression will be invoked. The behavior expression may use any attributes and association ends that are in the scope of the owning entity. For list items where the expression is empty, the backslash separator is optional. • Several labels are reserved for special purposes and cannot be used as event names. • entry (behavior performed upon entry to the state) • do (ongoing behavior, performed as long as the element is in the state) • exit (behavior performed upon exit from the state) • E.g. • has name and activity compartments Composite State • A state that has substates (nested states). Substates can be sequential (disjoint, “or”) or concurrent (orthogonal, “and”) only. • UML 2.4 defines composite state as the state which contains one ore more regions. • Simple composite state contains just one region. 1 sequential state with 2 substates • Orthogonal composite state has more than one regions. Each region has a set of mutually exclusive disjoint subvertices and a set of transitions. • Any state enclosed within a region of a composite state is called a substate of that composite state. • It is direct when not contained by any other state; otherwise, it is indirect. • Each region of a composite state may have an initial pseudostate and a final state. • Composite state may have compartments. The compartments of the state are: • name • internal activities • internal transitions • decomposition • Above indicates a complex decomposition found in another diagram. Pseudostates • • • • • • • Initial Terminate Entry Exit Choice Join Fork Junction Shallow history Deep history • An initial pseudostate represents a default vertex that is the source for a single transition to the default state of a composite state. • At most one • Behavior, but no trigger or guard • Terminate pseudostate means the execution of this state machine by means of its context object is terminated. • Doesn’t exit states or perform exit actions. • equivalent to invoking a DestroyObjectAction. • Entry point pseudostate is an entry point of a state machine or composite state. • Has at most a single transition to a vertex within the same region • Exit point pseudostate is an exit point of a state machine or composite state. • Entering an exit point within any region of the composite state or state machine referenced by a submachine state implies • (1) the exit of this composite state or submachine state and, • (2) the triggering of the transition that has this exit point as source in the state machine enclosing the submachine or composite state. Exit point • Choice pseudostate realizes a dynamic conditional branch. • Evaluates the guards of the triggers of its outgoing transitions • Selects only one outgoing transition. • The decision on which path to take may be a function of the results of prior actions performed in the same run-to-completion step. Choice pseudostate (2 styles) • If more than one guard is true, nondeterminism arises. • If no guard is true model is ill-formed. • Fix with “else” guard • Fork pseudostate vertices split an incoming transition into two or more, terminating on orthogonal target vertices (i.e., vertices in different regions of a composite state). • The segments outgoing from a fork vertex must not have guards or triggers. • Join pseudostate merges several transitions originating from source vertices in different orthogonal regions. • The transitions entering a join vertex cannot have guards or triggers. • Junction pseudostate is used to chain together multiple transitions. • Used to construct compound transition paths between states. • E.g. converge multiple incoming transitions into a single outgoing transition (merge). • E.g. split an incoming transition into multiple outgoing transitions with different guard conditions. This realizes a static conditional branch. • Represented by a small black circle. • Final state is a special kind of pseudostate signifying that the enclosing region is completed. • If enclosing region is directly contained in a state machine and all other regions in the state machine also are completed, then it means entire state machine is completed. History pseudostate •For input m, S enters the state it was in before S was left (can be A, B, C, D, or E). If S is entered for the very first time, the default mechanism applies. •History and default mechanisms can be used hierarchically. Behavioral Transitions • A transition is a directed relationship between a source and target vertex. • may be part of a compound transition • UML 2.4 • behavioral-transition ::= [ triggers ] [ '[' guardconstraint ']' ] [ '/' behavior-expression ] triggers ::= trigger [ ',' trigger ]* Trigger list • (optional) trigger list specifies events that may induce transition • Event satisfies trigger if it matches event of trigger • Satisfaction is necessary but not sufficient to take transition (more than one match!) Guard constraints • The guard-constraint is a Boolean expression written in terms of parameters of the triggering event. May also involve • tests of orthogonal states of the current state machine, • explicitly designated states of some reachable object (for example, "in Active State") Guard evaluation • In simple transition, guard is evaluated before transition is triggered. • In compound transitions (multiple guards), all guards are evaluated before transition is triggered, unless there are choice points along one or more of the paths. • The order in which guards are evaluated is not defined. Guards should not include expressions causing side effects Behavior expression • behavior-expression is executed when the transition fires. • May be written in terms of operations, attributes, and links of the context object and the parameters of triggering event, or any other features visible in its scope. • behavior expression may be an action sequence Transition Example • left-mouse-down(coordinates) [coordinates in active_window] / link:=selectlink(coordinates);link.follow() • When left-mouse-down event happens (trigger) and click coordinates are in active_window (guard), link will be selected and followed (behavior-expression), and transition fired. Leaving and entering states • Transitions originating from composite states are called high-level transitions. • If triggered, they result in exiting all substates of the composite state executing their exit activities starting with the innermost states. • A compound transition represents a "semantically complete" path made of one or more transitions, originating from a set of states (as opposed to pseudo-state) and targeting a set of states. Leaving and entering states (cont) • An internal transition executes without exiting or re-entering the state in which it is defined. This is true even if the state machine is in a nested state within this state. Water Phase Diagram Execution order • UML specifies that taking a state transition executes the following actions in the following sequence 1. Evaluate the guard condition associated with the transition and perform the following steps only if the guard evaluates to TRUE. 2. Exit the source state configuration. 3. Execute the actions associated with the transition. 4. Enter the target state configuration. Least Common Ancestor • In 2 is not obvious what states are exited • exit all nested states from the current active state (direct or indirect substates of the main source state) up to, but not including, the least common ancestor (LCA) • LCA is the lowest state that is a superstate of both the source and the target states Taking T1 causes the evaluation of guard g(); Exit of s11, s1, Action sequence a(); b(); t(); c(); d(); and e(); Entry of s2, s21 (assuming guard g() evaluates to TRUE) Exercise: microwave oven • We wish to model a basic oven with a door and a « start » button • Once a meal has been put in the oven, the user pushes the « start » button. Once pushed, the meal is heated for 30 seconds. Then, the oven stops and plays a bell sound • Alternatively, the user may open the door while the oven is heating. In that case, the heating process stops and the 30s delay is set to 0