tests
play

Tests Why Testing is Important Damien Cassou, Stphane Ducasse and - PowerPoint PPT Presentation

Tests Why Testing is Important Damien Cassou, Stphane Ducasse and Luc Fabresse WXSYY http://www.pharo.org Goal why tests are important? what are their advantages? what are the techniques to write good tests? WXSYY 2 / 24 Pros


  1. Tests Why Testing is Important Damien Cassou, Stéphane Ducasse and Luc Fabresse WXSYY http://www.pharo.org

  2. Goal � why tests are important? � what are their advantages? � what are the techniques to write good tests? WXSYY 2 / 24

  3. Pros � Finding problems � Understanding code � Increase trust � Collateral pros WXSYY 3 / 24

  4. Finding Problems: Pros � find bugs when they appear � improve customer trust � reproduce complex scenari � check contracts of super types � guarantee old bugs won’t come back � isolate the problem WXSYY 4 / 24

  5. Finding Problems: Characteristics of a Good Test Suite � check extreme cases (e.g., null, 0 and empty) � check complex cases (e.g., exceptions, network pbs) � 1 test for each bug (at least) � good coverage � check abstractions � check units independently WXSYY 5 / 24

  6. Understanding Code convert | s | s := '#000000' copy. s at: 2 put: (Character digitValue: ((rgb bitShif: − 6 − RedShif) bitAnd: 15)). s at: 3 put: (Character digitValue: ((rgb bitShif: − 2 − RedShif) bitAnd: 15)). s at: 4 put: (Character digitValue: ((rgb bitShif: − 6 − GreenShif) bitAnd: 15)). s at: 5 put: (Character digitValue: ((rgb bitShif: − 2 − GreenShif) bitAnd: 15)). s at: 6 put: (Character digitValue: ((rgb bitShif: − 6 − BlueShif) bitAnd: 15)). s at: 7 put: (Character digitValue: ((rgb bitShif: − 2 − BlueShif) bitAnd: 15)). ^ s WXSYY 6 / 24

  7. Understanding Code testConvert self assert: Color white convert = '#FFFFFF'. self assert: Color red convert = '#FF0000'. self assert: Color black convert = '#000000' WXSYY 7 / 24

  8. Understanding Code testConvert2 | table aColorString | table := #('0' '1' '2' '3' '4' '5' '6' '7' '8' '9' 'A' 'B' 'C' 'D' 'E' 'F'). table do: [ :each | aColorString := '#', each, each, '0000'. self assert: ((Color fromString: aColorString) convert sameAs: aColorString)]. table do: [ :each | aColorString := '#', '00', each, each, '00'. self assert: ((Color fromString: aColorString) convert sameAs: aColorString)]. table do: [ :each | aColorString := '#', '0000', each, each. self assert: ((Color fromString: aColorString) convert sameAs: aColorString)]. WXSYY 8 / 24

  9. Understanding Code testBitShif self assert: (2r11 bitShif: 2) equals: 2r1100. self assert: (2r1011 bitShif: − 2) equals: 2r10. testShifOneLefThenRight "Shif 1 bit lef then right and test for 1" 1 to: 100 do: [:i | self assert: ((1 bitShif: i) bitShif: i negated) = 1]. WXSYY 9 / 24

  10. Understanding Code: Pros � give simple and reproducible examples � explain an API � give up-to-date documentation � check conformity of new code � offer a first client to new code � force a modular design WXSYY 10 / 24

  11. Understanding Code: Characteristics of a Good Test Suite � deterministic � automatic � self-explained � simple � unit WXSYY 11 / 24

  12. Increasing Trust: Pros � accelerate bug detection � accelerate new code checking � ease refactorings � prevent regressions WXSYY 12 / 24

  13. Increasing Trust: Characteristics of a Good Test Suite � change less frequently than the rest � good code coverage � deterministic WXSYY 13 / 24

  14. Collateral Pros � improve feeling of customers who care � allow for automatic bug fixing � improve type inference � provide examples to variable values WXSYY 14 / 24

  15. Testing Abstractions How do you test contracts of abstract types? WXSYY 15 / 24

  16. Testing Abstractions How do you test that one and only one state is active at any time? WXSYY 16 / 24

  17. Testing Abstractions testOnlyOneValidStateAtEachMoment | action | action := self createAction. self assert: action isReady. self deny: action isInProgress. self deny: action isFinished. [ action isFinished ] whileFalse: [ action doStep. self deny: action isReady. self assert: action isFinished = action isInProgress not ]. self deny: action isReady. self deny: action isInProgress. self assert: action isFinished WXSYY 17 / 24

  18. Testing Abstractions � parallel hierarchies � test must be in the highest abstraction � factory method WXSYY 18 / 24

  19. Mocking How do you test that a questionnaire only accepts compatible answers from the user? WXSYY 19 / 24

  20. Mocking How do you test that a questionnaire only accepts compatible answers from the user? WXSYY 20 / 24

  21. Mocking readAnswerAsLongAsItIsNotCompatible | nbRejectsBeforeAccept question ui | nbRejectsBeforeAccept := 3. question := MockQuestion new nbRejects: nbRejectsBeforeAccept. ui := MockQuestionnaireUI new. self assert: ui nbReadAnswers equals: 0. self assert: question nbAcceptAnswerCalls equals: 0. questionnaire runQuestion: question on: ui. self assert: ui nbReadAnswers equals: nbRejectsBeforeAccept + 1. self assert: question nbAcceptAnswerCalls equals: WXSYY 21 / 24 nbRejectsBeforeAccept + 1.

  22. Mocking � mocks are reusable across tests � mocks can be generated with mocking frameworks WXSYY 22 / 24

  23. Different Testing Levels http://martinfowler.com/bliki/TestPyramid.html WXSYY 23 / 24

  24. A course by and in collaboration with Inria 2016 Except where otherwise noted, this work is licensed under CC BY-NC-ND 3.0 France https://creativecommons.org/licenses/by-nc-nd/3.0/fr/

Recommend


More recommend