Mental hacks to master Unit Testing

A presentation at c’t webdev in February 2020 in Cologne, Germany by Daniela Valero

Slide 1

Slide 1

Mental hacks to master Unit Testing

Slide 2

Slide 2

• The vicious circle Agenda • Break the loop • Getting the time • Mental Hacks (Using the time wisely)

Slide 3

Slide 3

The vicious circle

Slide 4

Slide 4

What today is… …is Tomorrow No time to Crazy regressions unit test & unexpected bugs

Slide 5

Slide 5

What today is… …is Tomorrow Lack of No meaningful practice unit tests

Slide 6

Slide 6

Break the loop

Slide 7

Slide 7

Getting the time

Slide 8

Slide 8

Selling points ARGUMENTS to convert your boss a Unit Testing fan %

Slide 9

Slide 9

Argument to get time What is unit testing …in non techies words

Slide 10

Slide 10

Argument to get time: Pyramid of testing: UI - Service - Unit

Slide 11

Slide 11

Argument to get time The goal of unit testing is to sustain project growth by preventing software entropy

Slide 12

Slide 12

Argument to get time Solid unit tested code increases time to market by minimizing regressions

Slide 13

Slide 13

Argument to get time Writing unit tests today reduces the cost of the feature of tomorrow

Slide 14

Slide 14

Once you get the time… you need to be aware of…

Slide 15

Slide 15

Time stealers SNEAKY things that can fill your time

Slide 16

Slide 16

Time stealers Time running unit tests

Slide 17

Slide 17

Time stealers Time spent reading unit tests

Slide 18

Slide 18

Time stealers Time spent rewriting tests

Slide 19

Slide 19

Time stealers Time dealing with false alarms

Slide 20

Slide 20

Writing the cool test

Slide 21

Slide 21

Step back Set the concepts STRAIGHT

Slide 22

Slide 22

Concepts to get straight Test Accuracy …what meaningful or cool actually stand for

Slide 23

Slide 23

Concepts to get straight Functionality Test Result Correct Pass Fail ! # False Positive Resistance to Refactoring Broken ” False Negative ! Protection against regressions

Slide 24

Slide 24

Concepts to get straight Pillars of Unit Testing

Slide 25

Slide 25

Concepts to get straight Resistance to refactoring Focus Focus Maintainability Protection against regressions Choose Choose Fast Feedback

Slide 26

Slide 26

Mental hacks WRITING the test that helps

Slide 27

Slide 27

Mental hack: Black box testing Test WHAT happens, not HOW it happens

Slide 28

Slide 28

Mental hack: Anti Example

Slide 29

Slide 29

Mental hack: Example

Slide 30

Slide 30

Mental hack: Test what matters Focus on: important or complex code

Slide 31

Slide 31

Mental hack: Test what matters Shopping Cart in E-Commerce Validations in Form Heavy pages

Slide 32

Slide 32

Mental hack: Test what matters Complex / Important Complex or Important Over complicated Test Split No. of Dependencies or side effects Trivial Controllers Ignore Optional

Slide 33

Slide 33

Mental hacks The STRUCTURE of the test that helps

Slide 34

Slide 34

Mental Hack: Test Anatomy Given — When — Then

Slide 35

Slide 35

Mental Hack: Your test shall tell 2 Function — Scenario — Expected behavior

Slide 36

Slide 36

Mental Hack: Anti Example

Slide 37

Slide 37

Mental Hack: Example

Slide 38

Slide 38

Mental Hack: Your test aint’ wrong… 3 …your code is!

Slide 39

Slide 39

4 Mental Hack: Read the docs Get familiarized with your testing framework API

Slide 40

Slide 40

• The vicious circle Recap • Break the loop • Getting the time • Mental Hacks (Using the time wisely)

Slide 41

Slide 41

Spoiler alerts • WANT: a more scalable and flexible codebase • PRICE: the beginner struggle, but keep going • NEW YOU: you’ll write more testable code from start

Slide 42

Slide 42

Thank You Where to find me… DanielaValero.com