LSR 1 Jartege : a Tool for Random Generation of Unit Tests for Java Classes Catherine Oriat LSR/IMAG, Grenoble, France (presented by Yves Ledru) SOQUA’05,

Slides:



Advertisements
Similar presentations
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 18 Program Correctness To treat programming.
Advertisements

Writing specifications for object-oriented programs K. Rustan M. Leino Microsoft Research, Redmond, WA, USA 21 Jan 2005 Invited talk, AIOOL 2005 Paris,
Design by Contract.
Design By Contract Using JMSAssert.
Exceptions CSE301 University of Sunderland Harry Erwin, PhD.
J-Unit Framework.
CMSC 202 Exceptions 2 nd Lecture. Aug 7, Methods may fail for multiple reasons public class BankAccount { private int balance = 0, minDeposit =
1 Design by Contract Building Reliable Software. 2 Software Correctness Correctness is a relative notion  A program is correct with respect to its specification.
Dept. of Computer Science A Runtime Assertion Checker for the Java Modeling Language (JML) Yoonsik Cheon and Gary T. Leavens SERP 2002, June 24-27, 2002.
Object Oriented Design An object combines data and operations on that data (object is an instance of class) data: class variables operations: methods Three.
Well-behaved objects 4.0 Testing. 2 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main concepts to.
An overview of JML tools and applications Lilian Burdy Gemplus Yoonsik Cheon, Gary Leavens Iowa Univ. David Cok Kodak Michael Ernst MIT Rustan Leino Microsoft.
JUnit. What is unit testing? A unit is the smallest testable part of an application. A unit test automatically verifies the correctness of the unit. There.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 18 Program Correctness To treat programming.
Software Testing and Quality Assurance
OOP #10: Correctness Fritz Henglein. Wrap-up: Types A type is a collection of objects with common behavior (operations and properties). (Abstract) types.
Using JML Runtime Assertion Checking to Automate Metamorphic Testing in Applications without Test Oracles Christian Murphy, Kuang Shen, Gail Kaiser Columbia.
Software Quality: Testing and Verification II. 2 1.A failure is an unacceptable behaviour exhibited by a system — The frequency of failures measures software.
Testing an individual module
Chair of Software Engineering Automatic Verification of Computer Programs.
1/23/2003University of Virginia1 Korat: Automated Testing Based on Java Predicates CS751 Presentation by Radu Stoleru C.Boyapaty, S.Khurshid, D.Marinov.
1 Functional Testing Motivation Example Basic Methods Timing: 30 minutes.
1 Genericity Parameterizing by Type. 2 Generic Class One that is parameterized by type  Works when feature semantics is common to a set of types On object.
Ranga Rodrigo. Class is central to object oriented programming.
Unit Testing & Defensive Programming. F-22 Raptor Fighter.
System/Software Testing
JUnit The framework. Goal of the presentation showing the design and construction of JUnit, a piece of software with proven value.
Lecture 6 Software Testing and jUnit CS140 Dick Steflik.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
Testing. What is Testing? Definition: exercising a program under controlled conditions and verifying the results Purpose is to detect program defects.
1 CSC241: Object Oriented Programming Lecture No 27.
A Safety-Critical Java Technology Compatibility Kit Hans Søndergaard Stephan Korsholm VIA University College, Horsens, Denmark & Anders P. Ravn Aalborg.
Software Engineering Prof. Dr. Bertrand Meyer March 2007 – June 2007 Chair of Software Engineering Static program checking and verification Slides: Based.
1 Debugging and Testing Overview Defensive Programming The goal is to prevent failures Debugging The goal is to find cause of failures and fix it Testing.
CMSC 202 Exceptions. Aug 7, Error Handling In the ideal world, all errors would occur when your code is compiled. That won’t happen. Errors which.
Coverage – “Systematic” Testing Chapter 20. Dividing the input space for failure search Testing requires selecting inputs to try on the program, but how.
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
CSC Programming I Lecture 8 September 9, 2002.
1 Assertions. 2 assertions communicate assumptions about the state of the program, and stop processing if they turn out to be false very often comments.
Tammy Dahlgren with Tom Epperly, Scott Kohn, and Gary Kumfert Center for Applied Scientific Computing Common Component Architecture Working Group October.
A Novel Approach to Unit Test: The Aspect-Oriented Way Guoqing Xu and Zongyuan Yang Software Engineering Lab (SEL) East China Normal University
6. Testing and Verification
Code Contracts Parameterized Unit Tests Tao Xie. Example Unit Test Case = ? Outputs Expected Outputs Program + Test inputs Test Oracles 2 void addTest()
A Survey on Java Modeling Languages Gergely Kovásznai,Eszterházy Károly College Wolfgang Schreiner,Johannes Kepler University Gábor Kusper,Eszterházy Károly.
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
CS Data Structures I Chapter 2 Principles of Programming & Software Engineering.
ISP RAS Java Specification Extension for Automated Test Development Igor B. Bourdonov, Alexei V. Demakov, Andrei A. Jarov, Alexander S. Kossatchev, Victor.
LSR Test purposes: adapting the notion of specification to testing Yves Ledru, L. du Bousquet, P. Bontron, O. Maury, C. Oriat, M.-L. Potet LSR/IMAG Grenoble,
Class Design I Class Contracts Readings: 2 nd Ed: Section 9.5, Advanced Topic nd Ed: Section 8.5, Advanced Topic 8.2 Some ideas come from: “Practical.
L13: Design by Contract Definition Reliability Correctness Pre- and post-condition Asserts and Exceptions Weak & Strong Conditions Class invariants Conditions.
S Ramakrishnan1 Systems V & V, Quality and Standards Dr Sita Ramakrishnan School CSSE Monash University.
SOFTWARE TESTING. Introduction Software Testing is the process of executing a program or system with the intent of finding errors. It involves any activity.
PROGRAMMING TESTING B MODULE 2: SOFTWARE SYSTEMS 22 NOVEMBER 2013.
PROGRAMMING PRE- AND POSTCONDITIONS, INVARIANTS AND METHOD CONTRACTS B MODULE 2: SOFTWARE SYSTEMS 13 NOVEMBER 2013.
Static Techniques for V&V. Hierarchy of V&V techniques Static Analysis V&V Dynamic Techniques Model Checking Simulation Symbolic Execution Testing Informal.
Unit Testing. F-22 Raptor Fighter Manufactured by Lockheed Martin & Boeing How many parts does the F-22 have?
DBC NOTES. Design By Contract l A contract carries mutual obligations and benefits. l The client should only call a routine when the routine’s pre-condition.
Object Design More Design Patterns Object Constraint Language Object Design Specifying Interfaces Review Exam 2 CEN 4010 Class 18 – 11/03.
Chapter 1 The Phases of Software Development. Software Development Phases ● Specification of the task ● Design of a solution ● Implementation of solution.
Random Test Generation of Unit Tests: Randoop Experience
Chapter 3 Implementing Classes
CMSC 202 Exceptions 2nd Lecture.
CMSC 202 Exceptions 2nd Lecture.
Software Verification and Validation
Software Verification and Validation
Java Modeling Language (JML)
Assertions References: internet notes; Bertrand Meyer, Object-Oriented Software Construction; 4/25/2019.
Software Verification and Validation
CSE 1020:Software Development
Programming Languages 2nd edition Tucker and Noonan
Presentation transcript:

LSR 1 Jartege : a Tool for Random Generation of Unit Tests for Java Classes Catherine Oriat LSR/IMAG, Grenoble, France (presented by Yves Ledru) SOQUA’05, Erfurt, Germany, Sep. 22nd 2005

LSR 2 The need for automatic test generation Testing usually estimated to 40% of the total development cost Agile methods favour continuous testing  The need for a large number of tests  The need for automatic generation

LSR 3 Sources of test generation Automatic generation can be systematic: –From the structure of the code (white box structural testing) –From the structure of the specification (black box functional testing) –From knowledge on the input (combinatorial testing) Automatic generation can be random –Usually presented as the poorest approach for selecting test data [Myers94] –But cheap and able to detect a large number of errors

LSR 4 Conformance testing Testing to compare an implementation to a reference specification We focus on –Java programs –JML specifications

LSR 5 Java Modelling Language Several verification tools (testing tools, static checkers, proof tools) JML specifications: –Based on the « Design by Contract TM » principle –Executable specifications –Automated oracle for the tests JML-Junit : –A combinatorial testing tool –Yoonsik Cheon and Gary T. Leavens. A Simple and Practical Approach to Unit Testing: The JML and JUnit Way. In ECOOP 2002 Proceedings. Vol of LNCS, Springer, 2002.

LSR 6 A case study : bank accounts Account balance min credit(amount : int) debit(amount : int) cancel() History balance hist prec The balance must always be greater than the minimum The history is the list of successive balances The minimum may be changed setMin(min : int)

LSR 7 /* The balance of this account. */ public pure */ int getBalance( ) { return balance; } /* The history list of this account. */ public pure */ History getHist( ) { return hist; } /* The minimum balance of this account. */ public pure */ int getMin( ) { return min; } JML specification of accounts The invariant is a property that must be true on entry and exit of all methods of the class public class Account { public invariant getBalance( ) >= getMin( ); */ private int balance; // The balance of this account private int min; // The minimum balance private History hist; // The history list of this // account Pure methods may not modify the object

LSR 8 JML specification of accounts (2) /* Constructs an account with the specified balance and * minimum balance. */ requires balance >= min; */ public Account (int balance, int min) { this.balance = balance; this.min = min; this.hist = null; } /* Sets the minimum balance to the specified value. */ requires getBalance ( ) >= min; */ public void setMin (int min) { this.min = min; } Requires expresses a pre-condition, i.e. a condition that must be true at the entry of the method If the precondition is false, the method should not be called

LSR 9 Credit method /* Credits this account with the specified amount. */ requires amount >= 0; ensures getBalance() == \old (getBalance()) + amount && \fresh (getHist()) && getHist().getBalance() == \old (getBalance()) && getHist().getPrec () == \old (getHist ()); signals (Exception e) false; */ public void credit(int amount) { hist = new History (balance, getHist ( )); balance = balance + amount; } You may only credit positive amounts The balance is updatedThis operation may not raise exceptions Ensures expresses the post-condition, i.e. a predicate that will be true after the execution \old(expr) refers to the value of « expr » at the start of the operation The history is updated

LSR 10 The contract The contract expressed in the pre- and post-conditions says that: –Provided the pre-condition hold –The program will satisfy the post-condition. But if the program is called outside its precondition, anything may happen… Exception: IllegalWaterFlow! This computer is used outside its precondition.

LSR 11 Cancel method /* Cancels the last credit or debit operation. */ requires getHist() != null; ensures getHist() == \old (getHist().getPrec()) && getBalance() == \old (getHist().getBalance()); signals (Exception e) false; */ public void cancel ( ) { balance = hist.getBalance ( ); hist = hist.getPrec ( ); } } // End of class Account The previous balance is restored The last record is deleted from the history Cancel only makes sense if there is some history

LSR 12 History public class History { private int balance; // The balance of this history. private History prec; // The preceding history. /* Constructs a history with the specified balance and preceding history. */ public History (int balance, History prec) { this.balance = balance; this.prec = prec; } /* The balance of this history. */ public pure */ int getBalance ( ) { return balance; } /* The preceding history. */ public pure */ History getPrec ( ) { return prec; } } No JML assertion here!

LSR 13 Jartege Java framework for random test generation Mainly unit tests Principles –Discovers the methods of the class using Java introspection/reflection –Randomly generates objects and parameters for the method –Builds sequences of calls –Takes advantage of the JML specification: Pre-conditions filter irrelevant calls Invariant and post-conditions as test oracle

LSR 14 Jartege in practice /** Jartege test cases generator for classes Account and History. */ class TestGen { public static void main (String[] args){ ClassTester t = new ClassTester(); // Creates a class tester t.addClass ("Account"); // Adds the specified classes t.addClass ("History"); // to the set of classes // under test // Generates a test class TestBank, // made of 100 test cases. // For each test case, // the tool tries to generate 50 method calls. t.generate ("TestBank", 100, 50); }}

LSR 15 A typical Jartege test case // Test case number 1 public void test1 ( ) throws Exception { try { Account ob1 = new Account ( , ); ob1.debit ( ); History ob2 = new History( ,(History)null); History ob3 = new History ( , ob2); History ob4 = ob2.getPrec ( ); int ob5 = ob3.getBalance ( ); ob1.cancel ( ); //... } catch (Throwable except) { error ( except, 1);} } Discovers the methods of the class using Java introspection/reflection Randomly generates objects and parameters for the method Builds sequences of calls Takes advantage of the JML specification: Pre-conditions filter irrelevant calls Invariant and post-conditions as test oracle Here cancel appears when its precondition is true!

LSR 16 Execution of the test suite 1) Error detected in class TestBank by method test2: JMLInvariantError: By method of class ”Account” for assertions specified at Account.java:11:32 [...] at TestBank.test2(TestBank.java:138) [...] Number of tests: 100 Number of errors: 71 Number of inconclusive tests: 0 The test suite includes 100 test cases 71 tests ended with an error During test case number 2 At the exit of method « credit » The invariant was broken

LSR 17 Controlling random generation « if we leave everything to chance, Jartege might not produce interesting sequences of calls » A typical problem: how to handle strong preconditions? e.g. a random debit will not satisfy the pre- condition if balance is close to min. Jartege features several mechanisms to define an « operational profile »

LSR 18 Controlling the creation of objects When a method call needs an object, we can –Either create a new one –Or reuse an existing one A creation probability function controls the creation of objects: –F(0) = 1 –F(n)  [0,1]  n > 0 For example: –It does not make sense to create multiple bank accounts in our case study –F(0) = 1, F(n) = 0  n > 0 t.changeCreationProbability("Account", new ThresholdProbability(1));

LSR 19 Instead of using the full range of a parameter, we can provide our own generation function. Parameter generation public class JRT_Account { private Account theAccount; // The current account /* Constructor. */ public JRT_Account (Account theAccount) { this.theAccount = theAccount; } /** Generator for the first parameter of operation debit (int). */ public int JRT_debit_int_1 ( ) { return RandomValue.intValue (0, theAccount.getBalance() - theAccount.getMin()); }} The parameter of debit is generated with respect to the current values of balance and min. It is more likely to meet the precondition!

LSR 20 Other features Weights –On the choice of classes –On the choice of methods –(allows to forbid the test of a given method) Test fixtures (like JUnit) –Additional attributes for the test class –setUp and tearDown methods

LSR 21 public void test1 ( ) { Account ob1 = new Account ( , 0); ob1.credit ( ); // Produces a negative balance, } // below the minimum balance. Errors found on the case study 71 test sequences ended with a failure. Analysis of these failures leads to two basic errors The test cases have been reduced manually to the minimal sequence that reveals the error The errors were unknown from us. First error: Credit produces an overflow of balance which becomes a negative value under min

LSR 22 Error #2 The precondition of setMin only takes into account the current balance. Restoring a previous balance with cancel may lead to it to be under the new minimum public void test11 ( ) { Account ob1 = new Account (-50, -100); ob1.credit (100); ob1.setMin (0); ob1.cancel ( ); // Restores the balance to a value } // inferior to the minimum balance.

LSR 23 Conclusion Summary: –A framework for random testing –Of java programs –Specified in JML –Using operational profiles The tool has been applied to several case studies: –Small banking application from Gemplus (ASE’04) –Jartege itself (use your own medecine!) Experiments confirmed the expected advantages of the tool: –Low cost of test generation –The tool helps findings errors

LSR 24 Future work Generation of method parameters –Currently done by hand –Could be generated from preconditions –Corresponds to the extraction of range constraints Once a test case reveals a failure –Identification of the smallest sequence that leads to an error –Currently manual, could be automated Operational profiles may help to evaluate reliability of a given software.