Tools09 – Reusing & Composing Tests with Traits

download Tools09 – Reusing & Composing Tests with Traits

of 18

  • date post

    20-May-2015
  • Category

    Technology

  • view

    501
  • download

    2

Embed Size (px)

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

  • 1. Reusing & ComposingTests with Traits Stphane DucasseRMod Inria Lille Nord Europe & University of Lille 1 Damien Pollet Alexandre Bergel Damien CassouPhoenix University of Bordeaux 1

2. Motivationredesigning the Smalltalk collectionsProblem few tests, similar protocolsCase study can we benet from traits in tests?Results write one test, 4 for freePerspectiveeven more reuse! 3. Smalltalk collections ObjectCollection SequenceableCollection BagSet LinkedList PluggableSet Interval ArrayedCollection Dictionary OrderedCollection Array String TextIdentityDictionary SortedCollection ByteString SymbolPluggableDictionary 4. Smalltalk collections Rich, versatile library Dynamic typing: if it quacks like a duck highly polymorphic many common protocols pluggable behaviors 5. Common Protocols accessing sizecapacityat:at:put:isEmptyoccurrencesOf:testing includes:contains:adding, add:addAll: removing remove:removeAll:remove:ifAbsent:do:collect:select:reject: enumeratinginject:into:detect:detect:ifNone: asBagasSetasArrayasOrderedCollectionconvertingasSortedCollectionasSortedCollection: creating with:withAll: 6. Existing Tests Test-by-Use No systematic testing:( features limit conditions Duplicated test methods Tests for ad hoc behavior 7. Photo from http://www.ickr.com/photos/cobalt/ (Creative Commons BY-NC-SA 2.0) 8. Hypothesis A protocol should: behave similarly across its implementors thus, be testable by similar codeBut: one test class = one xture xture reuse => multiple inheritance 9. Other Approaches TestCase Inheritance-based (xUnit)PutTestPutTest superclass provides test methodsPutTestPutTest testAtPut testAtPut testAtPut testAtPut subclasses provide xtures:( OrdCollPutTest OrdCollPutTestOrdCollPutTest OrdCollPutTestemptyempty empty empty Parameterization (JUnit 4) xture classes, passed to the tests impractical to adaptTestCase OrdCollFixtureOrdCollPutTest OrdCollPutTest:( PutTestOrdCollPutTestemptyPutTestemptyempty empty PutTesttestAtPutPutTest testAtPuttestAtPut testAtPut 10. Quick Primer on Traits Classes are schizophrenic! units of creation vs. units of reuseTraits: Units of composable behavior (no state) multiple implementation inheritance composer is in control resolve conicts via ignore / alias able E.g.: Magn itude, or Rubys Comparlation, given the total order rearison operators , , provide the comp 11. Superclass + State Class = + Traits + Glue MethodsObject ComponentGeometric RectangleWidget TColor RectangleShape x1, y1, x2, y2TRectangle point1, point2 setX1(...) setX1(...) setY1(...) setY1(...) 12. Test Traits Photo from http://www.ickr.com/photos/cobalt/ (Creative Commons BY-NC-SA 2.0) 13. Test Traits One test trait per protocol requires accessors to a xture provides systematic domain-level tests Test classes compose test traits dene the xture dene additional specic tests Superclass (TestCase) + xtureTest Class = + test traits + glue methods 14. TPutTest>>testAtPut selfnonEmptyat:selfanIndexput:selfaValue. selfassert: (selfnonEmptyat:selfanIndex)==selfaValue. TPutTest>>testAtPutOutOfBounds self should:[selfemptyat:selfanIndexput:selfaValue] raise:Error. TPutTest>>testAtPutTwoValues selfnonEmptyat:selfanIndexput:selfaValue. selfnonEmptyat:selfanIndexput:selfanotherValue. selfassert: (selfnonEmptyat:selfanIndex)==selfanotherValue. 15. For TPutTest, the xture must provide: emptynonEmpty: instances of the collection anIndex: integer or dictionary key or aValueanotherValue: 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 16. Results 27 test traits 150 tests written 29 xture req. test runner reports: 765 runs 17. Results One test written, ~4.7 run:) average on a wide subset of the collections classes still 1.8 / 1 when counting all methodsBalances to strike: tests & xtures: explicit vs. generic inheritance, pre-composed traits 18. Test TraitsMore extensive tests more classes => further reuse Identied protocol (a)symmetries inter-dialect standard insight for future redesign http://pharo-project.org