Upgrade to Pro — share decks privately, control downloads, hide ads and more …

The Test Smell Before Christmas

omissis
December 15, 2018

The Test Smell Before Christmas

A small presentation about test smells I gave to open the Drupal Community Day 2018 in Turin, Italy.

omissis

December 15, 2018
Tweet

More Decks by omissis

Other Decks in Programming

Transcript

  1. T H E
    T E S T S M E L L
    B E F O R E
    C H R I S T M A S

    View Slide

  2. T E S T S S M E L L
    B U T T H A T O F F E R S G O O D I N S I G H T S O N T H E C O D E
    Code smells
    Behaviour smells
    Project smells

    View Slide

  3. H A R D T O T E S T
    C O D E
    SYMPTOMS
    • some code is inherently
    difficult to test (GUI,
    threads, remote services)
    • tight coupling
    • private logic
    • lots of dependencies

    View Slide

  4. H A R D T O T E S T
    C O D E
    IMPACT
    • testing becomes very hard
    and time consuming
    • less tests will be written
    • does not scale

    View Slide

  5. H A R D T O T E S T
    C O D E
    POSSIBLE SOLUTIONS
    • learn to decouple code
    • separate logic from the
    async mechanism
    • keep test logic
    unconditional

    View Slide

  6. View Slide

  7. O B S C U R E
    T E S T S
    SYMPTOMS
    • no communication of
    intent
    • hard to understand
    • hard to maintain
    • can hardly be used as docs

    View Slide

  8. O B S C U R E
    T E S T S
    CAUSES
    • too much information (eg:
    Eager test, Irrelevant
    Information)
    • too little information (eg:
    Mystery Guest)
    • lack of attention and care

    View Slide

  9. O B S C U R E
    T E S T S
    EXAMPLE: EAGER TEST
    • symptom: verifies too much
    functionality in a single test
    • cause: “mocks” a human
    doing a test, chaining lots
    of actions together
    • solution: create multiple,
    single-condition tests for
    better Defect Localisation

    View Slide

  10. O B S C U R E
    T E S T S
    MYSTERY GUEST
    • symptom: hard to see
    cause-effect relationship
    between input and
    expected outcome
    • cause: tests become harder
    to follow and more fragile
    • solution: setup inline, aided
    by helpers/builders

    View Slide

  11. View Slide

  12. C O N D I T I O N A L
    T E S T L O G I C
    SYMPTOMS
    • presence of control
    structures in tests
    • some test code doesn’t
    get executed

    View Slide

  13. C O N D I T I O N A L
    T E S T L O G I C
    IMPACT
    • harder to write correct
    tests
    • maintenance overhead
    • decreased confidence in
    tests
    • increased chance of bugs
    in tests

    View Slide

  14. C O N D I T I O N A L
    T E S T L O G I C
    CAUSES
    • handling failure conditions
    • testing collections
    • testing conditional logic
    • optimising setup/teardown
    logic

    View Slide

  15. C O N D I T I O N A L
    T E S T L O G I C
    POSSIBLE SOLUTIONS
    • support substitutable deps
    • add guard assertions
    • define test-specific equality
    • enumerate inputs and
    outputs

    View Slide

  16. C O N D I T I O N A L T E S T L O G I C
    C O D E E X A M P L E

    View Slide

  17. H A P P Y T E S T I N G !

    View Slide

  18. T H A N K S !
    @ O M I S S I S

    View Slide