Generating Readable Unit Tests for Guava Ermira Daka, Jos Campos, - - PowerPoint PPT Presentation

generating readable unit
SMART_READER_LITE
LIVE PREVIEW

Generating Readable Unit Tests for Guava Ermira Daka, Jos Campos, - - PowerPoint PPT Presentation

Generating Readable Unit Tests for Guava Ermira Daka, Jos Campos, and Gordon Fraser University of Sheffield Jonathan Dorn and Westley Weimer University of Virginia ESEC/FSE 2015 [Developers] read tests [] 77% of the total time they


slide-1
SLIDE 1

Generating Readable Unit Tests for Guava

Ermira Daka, José Campos, and Gordon Fraser University of Sheffield Jonathan Dorn and Westley Weimer University of Virginia ESEC/FSE 2015

slide-2
SLIDE 2

“[Developers] read tests […] 77% of the total time they spend in them”

Moritz Beller, Georgios Gousios, Annibale Panichella, and Andy Zaidman. When, How, and Why Developers (Do Not) Test in Their IDEs. FSE 2015

slide-3
SLIDE 3

public final class MoreExecutors { private MoreExecutors() {} … public static Executor directExecutor() { return DirectExecutor.INSTANCE; } … public static ListeningExecutorService listeningDecorator( ExecutorService delegate) { return (delegate instanceof ListeningExecutorService) ? (ListeningExecutorService) delegate : (delegate instanceof ScheduledExecutorService) ? new ScheduledListeningDecorator((ScheduledExecutorService) delegate) : new ListeningDecorator(delegate); } … }

slide-4
SLIDE 4

public class MoreExecutors_ESTest extends MoreExecutors_ESTest_scaffolding { @Test public void test0() throws Throwable { Executor executor0 = MoreExecutors.directExecutor(); MockThread mockThread0 = new MockThread(); int int0 = 0; ScheduledThreadPoolExecutor scheduledThreadPoolExecutor0 = new ScheduledThreadPoolExecutor(int0); ListeningScheduledExecutorService listeningScheduledExecutorService0 = MoreExecutors.listeningDecorator((ScheduledExecutorService) scheduledThreadPoolExecutor0); long long0 = 1L; TimeUnit timeUnit0 = TimeUnit.MILLISECONDS; int int1 = (-774); LinkedBlockingDeque<Runnable> linkedBlockingDeque0 = new LinkedBlockingDeque<Runnable>(); ThreadPoolExecutor.DiscardOldestPolicy threadPoolExecutor_DiscardOldestPolicy0 = new ThreadPoolExecutor.DiscardOldestPolicy(); int int2 = 0; int int3 = 431; TimeUnit timeUnit1 = TimeUnit.MINUTES; ThreadPoolExecutor threadPoolExecutor0 = new ThreadPoolExecutor(int0, int3, long0, timeUnit1, (BlockingQueue<Runnable>) linkedBlockingDeque0, (RejectedExecutionHandler) threadPoolExecutor_DiscardOldestPolicy0); BlockingQueue<Runnable> blockingQueue0 = threadPoolExecutor0.getQueue(); ThreadFactory threadFactory0 = scheduledThreadPoolExecutor0.getThreadFactory(); TimeUnit timeUnit2 = TimeUnit.MINUTES; int int4 = (-188); TimeUnit timeUnit3 = TimeUnit.HOURS; ThreadPoolExecutor threadPoolExecutor1 = null; try { threadPoolExecutor1 = new ThreadPoolExecutor(int0, int4, (long) int0, timeUnit3, (BlockingQueue<Runnable>) linkedBlockingDeque0, threadFactory0); } catch(IllegalArgumentException e) { // // no message in exception (getMessage() returned null) // } } }

slide-5
SLIDE 5
slide-6
SLIDE 6

ESEC FSE 2015

slide-7
SLIDE 7

Test readability judgments

7

slide-8
SLIDE 8

Test readability judgments

  • 15,669 human judgments of readability
slide-9
SLIDE 9

9

public void test0() throws Throwable { String string0 = ""; String string1 = "]"; MessageDigestHashFunction messageDigestHashFunction0 = null; try { messageDigestHashFunction0 = new MessageDigestHashFunction(string0, string1); } catch(AssertionError e) { // // java.security.NoSuchAlgorithmException: MessageDigest not available // } } Test Length = 10 Unique Identifiers = 3 Max Line Length = 77 Has Exception = True Constructors = 1 Assertions = 0 String Length = 1

… 99 other features

slide-10
SLIDE 10

Feature predictive power

10

0.1 0.2 0.3 0.4 0.5 0.6

has assertions method ratio avg arithmetic operators max characters avg branches total assertions has exceptions identifier ration avg nulls avg loops total numbers avg string length max nulls total floats token entropy total distict methods total identifiers total unique identifers max identifier length total line length total constructor calls avg identifier length total identifier length max line length

slide-11
SLIDE 11

Readable test case

  • Readability as secondary objective
  • Individuals with same coverage based

fitness are ranked by readability score

  • Readability part of multi-objective algorithm
  • NSGA-II including readability score as fitness
slide-12
SLIDE 12

Class selection for test readability evaluation

GUAVA

base.Splitter math.DoubleMath net.PercentEscaper primitives.UnsignedBytes Util.concurrent.MoreExecutors Optimize Test Cases for Readability

slide-13
SLIDE 13

Readability improvement

10 20 30 40 50 60 70 80

base.Splitter math.DoubleMath net.PercentageEscaper primitives.UnsignedBytes util.conurrent.MoreExecutors

Readability as SO Readbility as MO

slide-14
SLIDE 14

Readability improvement

  • 2
  • 1

1 2 3 4

base.Splitter math.DoubleMath net.PercentageEscaper primitives.UnsignedBytes util.conurrent.MoreExecutors

Readability as SO Readbility as MO

slide-15
SLIDE 15

Do users agree with optimization?

slide-16
SLIDE 16
slide-17
SLIDE 17

5 10 15 20 25 30 35 40 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50

Optimized Default

Do users agree with optimization?

slide-18
SLIDE 18

Readability post-processing

  • For a test t = <s1, s2,…, si> with coverage goal c we:
  • iterate over the statements in the test from the last to the first statement
  • For each statement we determine the possible set of replacement statements
  • For each candidate replacement t’ we determine if it still satisfies c
  • Tests are then sorted based on their readability score

Foo foo = new Foo(); Bar bar = new Bar(“Some parameter”, 17); foo.setBar(bar); assertTrue(foo.isBar()); Bar bar = new Bar(); assertFalse(foo.isBar()) Foo foo = new Foo(); Bar bar = new Bar(); foo.setBar(bar); assertTrue(foo.isBar());

slide-19
SLIDE 19

Test Suite Generation

  • Test suites for all 359 top-level, public classes in Guava

2.25

slide-20
SLIDE 20