Tools09 – Reusing & Composing Tests with Traits

18
Reusing & Composing Tests with Traits Stéphane Ducasse RMod Inria Lille Nord Europe & University of Lille 1 Damien Pollet & University of Lille 1 Alexandre Bergel Damien Cassou Phoenix University of Bordeaux 1

description

The slides for the presentation of our paper on reusing protocol tests by organizing them in traits.

Transcript of Tools09 – Reusing & Composing Tests with Traits

Page 1: Tools09 – Reusing & Composing Tests with Traits

Reusing & ComposingTests with Traits

Stéphane Ducasse RMod Inria Lille Nord Europe& University of Lille 1

Damien Pollet

RMod Inria Lille Nord Europe& University of Lille 1

Alexandre Bergel

RMod Inria Lille Nord Europe& University of Lille 1

Damien Cassou Phoenix University of Bordeaux 1

Page 2: Tools09 – Reusing & Composing Tests with Traits

Motivation redesigning the Smalltalk collections

Problem few tests, similar protocols

Case study can we benefit from traits in tests?

Results write one test, 4 for free

Perspective even more reuse!

Page 3: Tools09 – Reusing & Composing Tests with Traits

Smalltalk collections

Collection

String

Symbol

Object

Set

Dictionary

Array Text

Bag

OrderedCollection

SortedCollection

LinkedList

ArrayedCollectionInterval

SequenceableCollection

PluggableSet

PluggableDictionary

IdentityDictionary

ByteString

Page 4: Tools09 – Reusing & Composing Tests with Traits

Smalltalk collections

Rich, versatile library

Dynamic typing: “if it quacks like a duck…”highly polymorphic

many common protocols

pluggable behaviors

Page 5: Tools09 – Reusing & Composing Tests with Traits

Common Protocolsaccessing  size    capacity    at:    at:put:

testing  isEmpty    occurrencesOf: includes:    contains:

adding,removing

 add:    addAll: remove:   removeAll:   remove:ifAbsent:

enumerating  do:   collect:   select:   reject: inject:into:    detect:    detect:ifNone:

converting  asBag   asSet   asArray   asOrderedCollection asSortedCollection    asSortedCollection:

creating  with:    withAll:

Page 6: Tools09 – Reusing & Composing Tests with Traits

Existing Tests

“Test-by-Use™”

No systematic testingfeatures

limit conditions

Duplicated test methods

Tests for ad hoc behavior

: (

Page 7: Tools09 – Reusing & Composing Tests with Traits

Phot

o fr

om h

ttp:

//ww

w.fl

ickr

.com

/pho

tos/

coba

lt/ (

Cre

ativ

e C

omm

ons

BY-N

C-S

A 2

.0)

Page 8: Tools09 – Reusing & Composing Tests with Traits

Hypothesis

A protocol should:behave similarly across its implementors

thus, be testable by similar code

But: one test class = one fixturefixture reuse => multiple inheritance

Page 9: Tools09 – Reusing & Composing Tests with Traits

Other ApproachesInheritance-based (xUnit)

superclass provides test methods

subclasses provide fixtures

Parameterization (JUnit 4)fixture classes, passed to the tests

impractical to adapt

empty

OrdCollPutTest

testAtPut

PutTest

empty

OrdCollPutTest

testAtPut

PutTest

testAtPut

PutTest

empty

OrdCollPutTest

TestCase

testAtPut

PutTest

empty

OrdCollPutTest

testAtPut

PutTest

testAtPut

PutTest

testAtPut

PutTest

TestCase

testAtPut

PutTest

empty

OrdCollPutTest

empty

OrdCollPutTest

empty

OrdCollPutTestempty

OrdCollFixture

: (

: (

Page 10: Tools09 – Reusing & Composing Tests with Traits

Quick Primer on TraitsClasses are schizophrenic!

units of creation vs. units of reuse

Traits: Units of composable behavior (no state)multiple implementation inheritance

composer is in control

resolve conflicts via ignore / alias

E.g.: Magnitude, or Ruby’s Comparable

given the total order relation,

provide the comparison operators ≤, <, >, ≥…

Page 11: Tools09 – Reusing & Composing Tests with Traits

Object

Component Geometric

RectangleWidget

setX1(...)setY1(...)

RectangleShape

setX1(...)setY1(...)

TRectangle

TColor

x1, y1, x2, y2 point1, point2

Class =

Superclass+ State+ Traits+ Glue Methods

Page 12: Tools09 – Reusing & Composing Tests with Traits

Phot

o fr

om h

ttp:

//ww

w.fl

ickr

.com

/pho

tos/

coba

lt/ (

Cre

ativ

e C

omm

ons

BY-N

C-S

A 2

.0)

Test Traits

Page 13: Tools09 – Reusing & Composing Tests with Traits

Test Traits

One test trait per protocolrequires accessors to a fixture

provides systematic domain-level tests

Test classescompose test traits

define the fixture

define additional specific tests

Test Class =

Superclass (TestCase)

+ fixture+ test traits+ glue methods

Page 14: Tools09 – Reusing & Composing Tests with Traits

TPutTest >> testAtPut  self nonEmpty at: self anIndex put: self aValue.   self assert:    (self nonEmpty at: self anIndex) == self aValue. 

TPutTest >> testAtPutOutOfBounds  self    should: [self empty at: self anIndex put: self aValue]    raise: Error.

TPutTest >> testAtPutTwoValues  self nonEmpty at: self anIndex put: self aValue.   self nonEmpty at: self anIndex put: self anotherValue.   self assert:    (self nonEmpty at: self anIndex) == self anotherValue.

Page 15: Tools09 – Reusing & Composing Tests with Traits

For TPutTest, the fixture must provide:

empty nonEmpty: instances of the collectionanIndex: integer or dictionary key or…aValue anotherValue: legal for the collection

Each test class:

controls which test traits to compose (and how)

provides ad-hoc tests

groups all test code for a domain class : )

Page 16: Tools09 – Reusing & Composing Tests with Traits

Results

27 test traits

150 tests written

29 fixture req.

test runner reports: 765 runs

Page 17: Tools09 – Reusing & Composing Tests with Traits

Results

One test written, ~4.7 runaverage on a wide subset of the collections classes

still 1.8 / 1 when counting all methods

Balances to strike:tests & fixtures: explicit vs. generic

inheritance, pre-composed traits…

: )

Page 18: Tools09 – Reusing & Composing Tests with Traits

Test Traits

More extensive testsmore classes => further reuse

Identified protocol (a)symmetriesinter-dialect standard

insight for future redesign

http://pharo-project.org