Chap 6. Test Models and Strategies

Similar documents
Assertions, pre/postconditions

n Specifying what each method does q Specify it in a comment before method's header n Precondition q Caller obligation n Postcondition

Introduction to Software Testing Chapter 2, Sec#: 2.5 Graph Coverage for Specifications

Part I: Preliminaries 24

Chapter 4 Defining Classes I

STATE MACHINES. Figure 1: State Machines

The Java Modeling Language JML

Behavioral Requirements

The experimental guide: comparing automatic test generation with manual test generation from UML state machine diagrams of a vending machine program

Motivation State Machines

Programming Languages Third Edition

EXAMINATIONS 2009 END-OF-YEAR. COMP 202 / SWEN 202 Formal Methods of Computer Science / Formal Foundations of Software Engineering

SOFTWARE DESIGN COSC 4353 / Dr. Raj Singh

UNIT-4 Behavioral Diagrams

Formal Methods for Software Development

Logic Coverage. Moonzoo Kim School of Computing KAIST. The original slides are taken from Chap. 8 of Intro. to SW Testing 2 nd ed by Ammann and Offutt

Testing, Debugging, Program Verification

Introduction to Software Testing Chapter 3, Sec# 1 & 2 Logic Coverage

Prelim 1 SOLUTION. CS 2110, September 29, 2016, 7:30 PM Total Question Name Loop invariants. Recursion OO Short answer

Motivation was to facilitate development of systems software, especially OS development.

Program Verification (6EC version only)

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

MIDTERM EXAM (Solutions)

CHAPTER 5 ARCHITECTURAL DESIGN SE211 SOFTWARE DESIGN

Formal Methods. CITS5501 Software Testing and Quality Assurance

EINDHOVEN UNIVERSITY OF TECHNOLOGY

State-Based Testing Part B Error Identification. Generating test cases for complex behaviour

Motivation was to facilitate development of systems software, especially OS development.

Introduction to Software Testing Chapter 4 Input Space Partition Testing

Semantics. There is no single widely acceptable notation or formalism for describing semantics Operational Semantics

16. Specification-based inspection

CPS122 Lecture: Detailed Design and Implementation

Formal Specification and Verification

Object-Oriented Design Lecture 3 CSU 370 Fall 2007 (Pucella) Friday, Sep 14, 2007

UC Santa Barbara. CS189A - Capstone. Christopher Kruegel Department of Computer Science UC Santa Barbara

ESC/Java 2. Checker for Java 2. Extended. Static. B y K ats man Andrey S oftware E ngineering S em inar

Semantic Analysis. CSE 307 Principles of Programming Languages Stony Brook University

Master of Science in Embedded and Intelligent Systems MASTER THESIS. Generating Test Adapters for ModelJunit. Ardalan Hashemi Aghdam

22c:181 / 55:181 Formal Methods in Software Engineering

Syntax of Eiffel: a Brief Overview

Commands, and Queries, and Features. Syntax of Eiffel: a Brief Overview. Escape Sequences. Naming Conventions

The Rule of Constancy(Derived Frame Rule)

Axiomatic Rules. Lecture 18: Axiomatic Semantics & Type Safety. Correctness using Axioms & Rules. Axiomatic Rules. Steps in Proof

Software Service Engineering

NAME CHSM-Java Concurrent, Hierarchical, Finite State Machine specification language for Java

Testing & Symbolic Execution

EXAMINATIONS 2009 MID-TERM TEST. COMP 202 / SWEN 202 Formal Methods of Computer Science / Formal Foundations of Software Engineering WITH ANSWERS

Introduction to Software Testing Chapter 3.1, 3.2 Logic Coverage Paul Ammann & Jeff Offutt

Introduction to Software Testing Chapter 3.1, 3.2 Logic Coverage Paul Ammann & Jeff Offutt

Introduction to Axiomatic Semantics

In One Slide. Outline. LR Parsing. Table Construction

Static program checking and verification

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Specification-based Testing

Chapter 3. Describing Syntax and Semantics ISBN

Catching Defects: Design or Implementation Phase? Design-by-Contract (Dbc) Test-Driven Development (TDD) Motivation of this Course

10. Methods. Methods. Example Cookie Calculator. Cookie Calculator Additional Requirements

Chapter 3 (part 3) Describing Syntax and Semantics

Inner Oracles: Input- Specific Assertions on Internal States

CS111: PROGRAMMING LANGUAGE II. Lecture 1: Introduction to classes

Introduction to Axiomatic Semantics (1/2)

Fondamenti della Programmazione: Metodi Evoluti. Lezione 5: Invariants and Logic

Testing, Fuzzing, & Symbolic Execution

11. Methods. Educational Objectives. Example Cookie Calculator. Methods

Homework 1. Automa-c Test Genera-on. Automated Test Genera-on Idea: The Problem. Pre- & Post- Condi-ons 2/8/17. Due Thursday (Feb 16, 9 AM)

Chapter 3. Semantics. Topics. Introduction. Introduction. Introduction. Introduction

Formale Entwicklung objektorientierter Software

Chapter 1: Programming Principles

Why Design by Contract! CS 619 Introduction to OO Design and Development. Design by Contract. Fall 2012

Java Modeling Language (JML)

Model Checking of Statecharts using Automatic White Box Test Generation

F1 A Java program. Ch 1 in PPIJ. Introduction to the course. The computer and its workings The algorithm concept

Defining Classes and Methods. Objectives. Objectives 6/27/2014. Chapter 5

Software Engineering: A Practitioner s s Approach, 6/e Roger Pressman. Chapter 28 Formal Methods

Classes, interfaces, & documentation. Review of basic building blocks

State Machine Specification Directly in Java and C++ Alexander Sakharov

Hoare logic. A proof system for separation logic. Introduction. Separation logic

Ingegneria del Software Corso di Laurea in Informatica per il Management

Self-review Questions

Inheritance and Polymorphism

Chapter 1: Principles of Programming and Software Engineering

Q Body of techniques supported by. R precise mathematics. R powerful analysis tools. Q Rigorous, effective mechanisms for system.

Exercise Unit 2: Modeling Paradigms - RT-UML. UML: The Unified Modeling Language. Statecharts. RT-UML in AnyLogic

Finite State Machines and Statecharts

JUnit Testing Framework Architecture

CSE115 Introduction to Computer Science I Coding Exercise #7 Retrospective Fall 2017

Principles of Object Oriented Programming. Lecture 4

SBML Event Semantics

An introduction to formal specifications and JML. Invariant properties

Lecture 11 Lecture 11 Nov 5, 2014

Defining Classes. Chap 1 introduced many concepts informally, in this chapter we will be more formal in defining

Foundations, Reasoning About Algorithms, and Design By Contract CMPSC 122

Documentation Requirements Computer Science 2334 Spring 2016

UML for Real-Time Overview

Equality for Abstract Data Types

Objectives. Defining Classes and Methods. Objectives. Class and Method Definitions: Outline 7/13/09

In-Class Exercises. ETH Zurich. ETH students recently designed a special kind of oven for cooking potatoes. Here are some facts about such an oven:

a correct statement? You need to know what the statement is supposed to do.

GENERATING COST-EFFECTIVE CRITERIA-BASED TESTS FROM BEHAVIORAL MODELS

LL(k) Parsing. Predictive Parsers. LL(k) Parser Structure. Sample Parse Table. LL(1) Parsing Algorithm. Push RHS in Reverse Order 10/17/2012

Transcription:

Chap 6. Test Models and Strategies 6.2 State-based Testing 1. Introduction 2. State Transition Diagram 3. Transition Test Strategy 1

1. Introduction -State-based testing relies on the construction of a finite-state machine (FSM) or state-transition diagram that describes the dynamic behavior of the system under testing. -However, conventional FSM models are not ready for test case generation, because in general they are ambiguously defined. Effective test data generation requires making the FSM model test-ready by defining precisely the underlying meaning. -The FSM model can be derived from the program specification, or extracted from the code through reverse-engineering. -State-based models can be used both for unit and integration testing, and for functional as well as structural testing. 2

-Many state-based test strategies have been proposed in the research literature. In this chapter, we ll illustrate state-based testing by studying the transition-based testing strategy. -The transition-based test strategy rigorously defines the semantics of the basic features of a state diagram (e.g., state, event etc.) as invariants or predicates, and analyzes these predicates using domain matrix in order to generate suitable test cases. -The advantage of the transition test strategy compared with classical domain analysis is that it encompasses implicitly a test oracle; so it removes the need to define explicitly expected outputs, which can be extremely difficult to derive in many cases. 3

2. State-Transition Diagram -Use cases and scenarios provide a way to describe system behavior, that is the interactions between objects in the system. -UML state diagram is used to model the dynamic behavior of any modeling element, such as a class, a use case or an entire system. It allows the modeling of the behavior inside a single object. It shows the sequence of states of an object during its lifetime. It shows the events or messages that cause a transition from one state to another, and the actions that result from a state change. It is created only for classes with significant dynamic behavior, like control classes. So it is appropriate for developing test cases at the class level. -The main building blocks of a state transition diagram are states and transitions. 4

State: a situation during the life of an object when it satisfies some condition, performs some action, or waits for an event found by examining the attributes and links defined for the object represented as a rectangle with rounded corners Transition: represent a change from an originating state to a successor state (that may be the same as the originating state). may have an action and/or a guard condition associated with it, and may also trigger an event. 5

Example: Lighting System Specify a lighting system with two lamps and one button functioning as follows: 1. When the light is off, pushing the button causes the first lamp to go on. 2. Pushing the button again causes the second lamp to go on and the first to go off. 3. Pushing the button yet again causes both lamps to go on, and pushing it once more switches both lamps off. Class Diagram Lighting - lamp1_status: {on,off} - lamp2_status: {on,off} - button_status: {high, low} + push() - switch() 6

State Diagram Lighting_State push()/switch() Light_Off Light_On push()/switch() Lamp1_On push()/switch() All_Lamps_On Lamp2_On push()/switch() 7

Possible Implementation public class Lighting { private static boolean on = true; private static boolean off = false; private static boolean high = true; private static boolean low = false; private boolean lamp1status, lamp2status, buttonstatus; public Lighting() {lamp1status=false; lamp2status=false; buttonstatus=true;} public Lighting(boolean l1,boolean l2, boolean b) { lamp1status=l1; lamp2status=l2; buttonstatus=b; } } public void push() { if ((lamp1status==false) & (lamp2status==false)) lamp1status = true; elseif ((lamp1status == true) & (lamp2status==false)) { lamp1status = false; lamp2status = true; } elseif ((lamp1status == false) & (lamp2status==true)) lamp1status=true; else { lamp1status = false; lamp2status = false; } switch(); } private void switch () {buttonstatus =! buttonstatus;} 8

3. Transition Test Strategy -A (UML) state diagram describes the sequence of states through which an object evolves during its lifetime, as well as the sequence of messages it sends and/or receives. The messages exchanged during that lifetime correspond to method calls, i.e., events, and are associated to transitions between states. Hence, the actual execution of a method is closely related to the firing of a corresponding transition in the statechart specifying the class behavior. Since a method can be executed under various circumstances, several transitions can be associated with a method, each corresponding to a particular circumstance. -The transition test strategy identifies the collection of transitions associated with a method and uses these transitions as basis for test case generation. It requires the definition of precise semantics for the state model. 9

Semantics Definition -Possible semantics for state model may consist of defining predicate expressions for basic states, actions, and guard conditions. Guard condition: the predicate corresponds to the expression of the condition. Basic state: the predicate represents an abstraction of corresponding situation. Action: the predicate corresponds to an abstraction of the output or result of the action -State predicates may be function of only state variables (i.e., attributes or instance variables), while conditions and actions predicates may be function of both state variables and method parameters. 10

-Example: Semantics Definition for Lighting System State Predicates Pred(Light_Off) = (lamp1_status = off) (lamp2_status = off) Pred(Lamp1_On) = (lamp1_status = on) (lamp2_status = off) Pred(Lamp2_On) = (lamp1_status = off) (lamp2_status = on) Pred(All_Lamps_On) = (lamp1_status = on) (lamp2_status = on) Action Predicates Pred(switch) = (button_status = button_status) Condition Predicates There is no guard condition associated with the transitions (in this example), so no condition predicate. 11

Definition of Method Pre/Post-Conditions -The activation of a transition involves two predicates. The first predicate defining the enabling condition for the transition is associated with the source state and the guard condition. The second predicate defines the outcome of firing the transition, and as such it is related to the target state and the resulting action. -This pair of predicates can be used to define partial pre-post condition pair for the transition. S1 m() [C]/A pre_m= pred(s1) pred(c) post_m=pred(s2) pred(a) S2 The global pre/post-condition for a method corresponds to the combination of the pre/post condition pairs associated with all the transitions involving that method. 12

Example: Pre/Postconditions Pairs for Lighting System Four transitions are associated with method push(), which correspond to 4 (partial) pre/postconditions pairs. t1: Light_Off Lamp1_On; t2: Lamp1_On Lamp2_On; t3: Lamp2_On All_Lamps_On; t4: All_Lamps_On Light_Off Partial Pre/postcondition pair for t1: pre_push1 = pred(light_off) pred (C1) = (lamp1_status = off) (lamp2_status = off) post_push1= pred(lamp1_on) pred (switch) = (lamp1_status = on) (lamp2_status = off) (button_status = button_status) 13

Test Data Generation -The pre/post-conditions should be converted into executable test assertions by refining them. The preconditions are broken into disjunctive normal forms (DNF), by eliminating the disjunction ( ) operator from their expressions. Example: Decomposition of (a b) (p q) two disjoint cases: (a b) and (p q). -Each DNF expression is analyzed separately using the domain testing technique in order to generate the test cases, which can be collected using a domain matrix. 14

Example: Test data generation for lighting system Partial Pre/postcondition pair for t1: pre_push1 = pred(light_off) pred (C1) = (lamp1_status = off) (lamp2_status = off) post_push1= pred(lamp1_on) pred (switch) = (lamp1_status = on) (lamp2_status = off) (button_status = button_status) Conversion of the precondition pre_push1 into DNF: -No conversion is required because the predicate is already in a DNF format consisting of two conjuncts: (conj1: lamp1_status = off) and (conj2: lamp2_status = off) Refinement into executable test assertion: consists of bringing the predicate expressions into adequacy with the implementation language used (e.g., Java, C etc.). -We need to derive adequate Java assertions that can be used to test the target implementation given previously. - In this case, we simply need to harmonize the variables and operators involved in the expressions to fit the target program. Doing so lead to the following Java assertions: conj1 = (lamp1status == false); conj2 = (lamp2status == false) post_push1= (lamp1status ==true) & (lamp2status ==false) & (buttonstatus ==! buttonstatus) 15

Test Case Generation Using Domain Matrix: Variable Condition Type Test cases 1 2 3 4 lamp1status lamp1status On False =false Off True Typical In False False lamp2status lamp2status On False =false Off True Typical In False False buttonstatus Typical In False True True False Expected Result True Undefined True Undefined -True for the expected result means that IUT should accept the test input and produce correct results, which here corresponds to the outcome of the evaluation of the postcondition. -When the precondition is false, the postcondition can be anything (true or false); so the test case is considered undefined in this particular case. -Finally, for this partial pre/postcondition pair, we obtain only 2 valid test cases: 1 & 3. 16

Test Execution -Test execution can be done by generating appropriate test harness and/or using a test execution tool such as JUnit or Rational Tester. -Basic steps for test execution may include the following: 1. Creating a fresh object, and setting its source state using the test values generated. 2. Then, the method under testing is executed, and the outcome of the target state is observed. Object state can be set and observed by using mutator and accessor methods (e.g. set/get methods). 3. Evaluate the execution outcome and report the result. 4. Class testing is conducted by testing all the methods involved in the class. Example: Test execution for the lighting system Consider test case 1 from the domain matrix: lamp1 Status > false tc1 > input : lamp2 Status > false, output : post > true buttonstatus > false 17

Test case execution code: public boolean execute() { boolean l1, l2, b; //instance variables for class under testing boolean result = false; // test result } // Create a fresh object, and set its source state using the test values generated#tc1 Lighting l = new Lighting(false,false,false); //Execute the method under testing and observe the outcome l.push(); l1 = l.getlamp1status(); l2 = l.getlamp2status(); b = l.getbuttonstatus(); //Evaluate the outcome of the execution using the postcondition if( (l1 ==true) && (l2==false) && (b == true)) then result = true; return result; //Alternative code l.push(); Assert (l1 ==true) && (l2==false) && (b == true)); } Issue: -Compilation: can you observe the state of object l (i.e., code getxxx )? 18