tools09 – reusing & composing tests with traits
Post on 20-May-2015
502 Views
Preview:
DESCRIPTION
TRANSCRIPT
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
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!
Smalltalk collections
Collection
String
Symbol
Object
Set
Dictionary
Array Text
Bag
OrderedCollection
SortedCollection
LinkedList
ArrayedCollectionInterval
SequenceableCollection
PluggableSet
PluggableDictionary
IdentityDictionary
ByteString
Smalltalk collections
Rich, versatile library
Dynamic typing: “if it quacks like a duck…”highly polymorphic
many common protocols
pluggable behaviors
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:
Existing Tests
“Test-by-Use™”
No systematic testingfeatures
limit conditions
Duplicated test methods
Tests for ad hoc behavior
: (
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)
Hypothesis
A protocol should:behave similarly across its implementors
thus, be testable by similar code
But: one test class = one fixturefixture reuse => multiple inheritance
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
: (
: (
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 ≤, <, >, ≥…
Object
Component Geometric
RectangleWidget
setX1(...)setY1(...)
RectangleShape
setX1(...)setY1(...)
TRectangle
TColor
x1, y1, x2, y2 point1, point2
Class =
Superclass+ State+ Traits+ Glue Methods
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
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
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.
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 : )
Results
27 test traits
150 tests written
29 fixture req.
test runner reports: 765 runs
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…
: )
Test Traits
More extensive testsmore classes => further reuse
Identified protocol (a)symmetriesinter-dialect standard
insight for future redesign
http://pharo-project.org
top related