lessons from testing

Preview:

DESCRIPTION

In this talk I review snippets of test code. The snippets all come from coding dojos performed on http://cyber-dojo.org. I see the same test anti-patterns time after time after time. Do some of your tests exhibit the same same anti-patterns?

Citation preview

Lessons from Testing

1

examples takenfrom

http://cyber-dojo.org@JonJagger

jon@jaggersoft.com

Thursday, 23 October 14

2

Avoid unnecessary burning of calories!

Thursday, 23 October 14

FizzBuzz

3

Thursday, 23 October 14

E09E8C snake 4

what do I notice?

Thursday, 23 October 14

5

Thursday, 23 October 14

6

Suppose we measure the average cyclomatic complexity of the code under test and the test

code.

Thursday, 23 October 14

7

Higher number means more complex.

Thursday, 23 October 14

codetests

8

3.7 2.1>

Thursday, 23 October 14

codetests

9

3.7 2.1

teststests

4.3 >>

Thursday, 23 October 14

codetests

10

3.7 2.1

teststests

4.3NO!>>

Thursday, 23 October 14

codetests

11

2.1 2.1==

Thursday, 23 October 14

codetests

12

2.1 2.1==NO!Thursday, 23 October 14

codetests

13

2.0 2.1<

Thursday, 23 October 14

codetests

14

2.0 2.1<NO!Thursday, 23 October 14

codetests

1.0 2.1

15

<<<

Thursday, 23 October 14

codetests

1.0 2.1

16

YES<<<

Thursday, 23 October 14

17E09E8C wolf

~> Simple1.0

Thursday, 23 October 14

18

what do I notice most?

Thursday, 23 October 14

19

Duplication!

Thursday, 23 October 14

20

Duplication usually indicates

a missing abstraction

Thursday, 23 October 14

21

what do I notice now?

Thursday, 23 October 14

22

Duplication!

Thursday, 23 October 14

23

Thursday, 23 October 14

24

what do I notice now?

Thursday, 23 October 14

25

Duplication!

Thursday, 23 October 14

26

Thursday, 23 October 14

27

what do I notice now?

Thursday, 23 October 14

28

missing examples?

Thursday, 23 October 14

29

Thursday, 23 October 14

30

what do I notice now?

Thursday, 23 October 14

31

better test names?

Thursday, 23 October 14

32

Thursday, 23 October 14

33

what do I notice now?

Thursday, 23 October 14

34

different_identifier_style?differentIdentifierStyle?

Thursday, 23 October 14

35

that's ok!

Thursday, 23 October 14

36

what do I notice now?

Thursday, 23 October 14

37

duplication?

Thursday, 23 October 14

38

this is worse!

Thursday, 23 October 14

39

how about this?

Thursday, 23 October 14

40

Thursday, 23 October 14

41

1.0

Specific ?

21F1F3 alligator

Thursday, 23 October 14

42

!Thursday, 23 October 14

43

Specific ?

Thursday, 23 October 14

44

Specific :-)

Thursday, 23 October 14

45

Repetition ?

?

Thursday, 23 October 14

46

Repetition can indicate a missing

abstraction

Thursday, 23 October 14

47

Specific

Thursday, 23 October 14

48

Specific

Thursday, 23 October 14

49

Thursday, 23 October 14

50

Recently Used List

Thursday, 23 October 14

51

e.g. Recent Files...

Thursday, 23 October 14

52DE3BF3 buffalo

what do I notice?

Thursday, 23 October 14

53

Duplication!

Thursday, 23 October 14

54

What's left?

WTF?redbluegreen

Thursday, 23 October 14

55

e.g. Files!

Thursday, 23 October 14

56BAC947 snake

better!

Thursday, 23 October 14

57

better still

Thursday, 23 October 14

58

What do I notice now?

Thursday, 23 October 14

59

Duplication!

Thursday, 23 October 14

60

What does assertEquals()

print when it fails?

Thursday, 23 October 14

61

Thursday, 23 October 14

62

Do yourdiagnostics diagnose?

Thursday, 23 October 14

63

What diagnostic

do you want?

Thursday, 23 October 14

64

refactor

write a test fornew

functionalitywrite a test fornew functionality

make it pass

ship it!

Thursday, 23 October 14

65

write a test fornew

functionalitywrite a test fornew functionality

make it pass

ship it!

refactor

Thursday, 23 October 14

66

refactor

write a test fornew

functionalitywrite a test fornew functionality

make it pass

ship it!

refactor the diagnostic!

Thursday, 23 October 14

67

refactor

write a test fornew

functionalitywrite a test fornew functionality

make it pass

ship it!

refactor the diagnostic!

Thursday, 23 October 14

68

How about this?

Thursday, 23 October 14

69

Make it so!

Thursday, 23 October 14

70

Thursday, 23 October 14

71

A

B C

D E F

G

external external

Thursday, 23 October 14

72

A

B C

tAmockist style

B' C'

D E F

G

Thursday, 23 October 14

73

A

B C

D E F

G

mockist style

tB

E'D'

C'

Thursday, 23 October 14

74

A

B C

D E F

G

mockist styleG'

F'

B'

tC

Thursday, 23 October 14

75

code tests

mockist style

Thursday, 23 October 14

76

A

B C

tA

D E F

G

classic style

D' F'

Thursday, 23 October 14

77

classic style

code tests

Thursday, 23 October 14

78

the.law.of.demeter

Thursday, 23 October 14

79

Thursday, 23 October 14

80

which is more important?

the code or the tests?

Thursday, 23 October 14

81

both

Thursday, 23 October 14

82

You cannot determine the character or nature of a system within itself. Attempts to do so lead to confusion and disorder.

Thursday, 23 October 14

83

code and testsco-evolve

Thursday, 23 October 14

84

evolution is always co-evolution

Thursday, 23 October 14

85

Thursday, 23 October 14

86

Thursday, 23 October 14

87

testing

time

Thursday, 23 October 14

88

debugging

timeThursday, 23 October 14

89

debugging = removing what you don't want

Thursday, 23 October 14

90

if you get rid of what you don't want, will you be left with what you do want?

Thursday, 23 October 14

91

?Thursday, 23 October 14

92

testing

time

Thursday, 23 October 14

93

specification

time

Thursday, 23 October 14

94

specific.........

time

Thursday, 23 October 14

95

SummaryBurning calories - a metaphor for understandabilityTests should be simple, linear, low complexityTests should be specificLong_specific_test_names_are_ok Duplication usually means a missing abstractionRepetition can indicate a missing abstraction tooDo your diagnostics diagnose? Custom assertionsDon't cheat on test data - "String1" vs "red"Beware overusing Mockist styleCode and tests co-evolveTest at the end is often debuggingTest at the beginning as specification

Thursday, 23 October 14

96

Thursday, 23 October 14

97

Thursday, 23 October 14

98

Thursday, 23 October 14

99

Thursday, 23 October 14

100

code with no testsFB75B3

Thursday, 23 October 14

101

Thursday, 23 October 14

102

Lesson ONE

test code must be much simpler than the code it

tests

Thursday, 23 October 14

Recommended