Jartege : a Tool for Random Generation of Unit Tests for Java - - PowerPoint PPT Presentation

jartege a tool for random generation of unit tests for
SMART_READER_LITE
LIVE PREVIEW

Jartege : a Tool for Random Generation of Unit Tests for Java - - PowerPoint PPT Presentation

LSR Jartege : a Tool for Random Generation of Unit Tests for Java Classes Catherine Oriat LSR/IMAG, Grenoble, France (presented by Yves Ledru) SOQUA05, Erfurt, Germany, Sep. 22nd 2005 1 The need for automatic test generation LSR


slide-1
SLIDE 1

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

slide-2
SLIDE 2

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

slide-3
SLIDE 3

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

slide-4
SLIDE 4

LSR

4

Conformance testing

  • Testing to compare an implementation to

a reference specification

  • We focus on

– Java programs – JML specifications

slide-5
SLIDE 5

LSR

5

Java Modelling Language

  • www.jmlspecs.org
  • Several verification tools

(testing tools, static checkers, proof tools)

  • JML specifications:

– Based on the « Design by ContractTM » 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. 2374 of LNCS, Springer, 2002.

slide-6
SLIDE 6

LSR

6

A case study : bank accounts

Account balance min credit(amount : int) debit(amount : int) cancel() History balance 0..1 0..1 +hist 0..1 +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)

slide-7
SLIDE 7

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 propert y t hat must be t rue

  • n ent ry and exit of all met hods of t he 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 met hods may not modif y t he obj ect

slide-8
SLIDE 8

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-condit ion, i.e. a condit ion t hat must be t rue at t he ent ry of t he met hod I f t he precondit ion is f alse, t he met hod should not be called

slide-9
SLIDE 9

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 posit ive amount s The balance is updat ed This operat ion may not raise except ions Ensures expresses t he post -condit ion, i.e. a predicat e t hat will be t rue af t er t he execut ion \ old(expr) ref ers t o t he value of « expr » at t he st art of t he operat ion The hist ory is updat ed

slide-10
SLIDE 10

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 comput er is used out side it s precondit ion.

slide-11
SLIDE 11

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 rest ored The last record is delet ed f rom t he hist ory Cancel only makes sense if t here is some hist ory

slide-12
SLIDE 12

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 J ML assert ion here!

slide-13
SLIDE 13

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
slide-14
SLIDE 14

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); }}

slide-15
SLIDE 15

LSR

15

A typical Jartege test case

// Test case number 1 public void test1 ( ) throws Exception { try { Account ob1 = new Account (1023296578, 223978640);

  • b1.debit (152022897);

History ob2 = new History(1661966075,(History)null); History ob3 = new History (-350589348, ob2); History ob4 = ob2.getPrec ( ); int ob5 = ob3.getBalance ( );

  • b1.cancel ( );

// ... } catch (Throwable except) { error ( except, 1);} }

Discover s t he met hods of t he class using J ava int rospect ion/ r ef lect ion Randomly generat es obj ect s and paramet ers f or t he met hod Builds sequences of calls Takes advant age of t he J ML specif icat ion:

  • Pre-condit ions f ilt er irrelevant calls
  • I nvariant and post -condit ions as t est oracle

Here cancel appears when it s precondit ion is t rue!

slide-16
SLIDE 16

LSR

16

E xecution of the test suite

1) Error detected in class TestBank by method test2: JMLInvariantError: By method ”credit@posthAccount.java:79:18i” 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 t est case number 2 At t he exit of met hod « credit » The invariant was broken

slide-17
SLIDE 17

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 »

slide-18
SLIDE 18

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));

slide-19
SLIDE 19

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

  • f operation debit (int). */

public int JRT_debit_int_1 ( ) { return RandomValue.intValue (0, theAccount.getBalance() - theAccount.getMin()); }}

The paramet er of debit is generat ed wit h respect t o t he current values

  • f balance and min.

I t is more likely t o meet t he precondit ion!

slide-20
SLIDE 20

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

slide-21
SLIDE 21

LSR

21

public void test1 ( ) { Account ob1 = new Account (250000000, 0);

  • b1.credit (2000000000); // Produces a negative balance,

} // below the minimum balance.

E rrors 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 overf low of balance which becomes a negat ive value under min

slide-22
SLIDE 22

LSR

22

E rror # 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);

  • b1.credit (100);
  • b1.setMin (0);
  • b1.cancel ( ); // Restores the balance to a value

} // inferior to the minimum balance.

slide-23
SLIDE 23

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

slide-24
SLIDE 24

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.