19
BW12 Session 6/8/16 4:15 PM Identify and Exploit Behavioral Boundaries for Unit Testing Presented by: Rob Myers Agile Institute Brought to you by: 350 Corporate Way, Suite 400, Orange Park, FL 32073 888---268---8770 ·· 904---278---0524 - [email protected] - http://www.techwell.com/

Identify and Exploit Behavioral Boundaries for Unit Testing

Embed Size (px)

Citation preview

Page 1: Identify and Exploit Behavioral Boundaries for Unit Testing

BW12Session6/8/164:15PM

IdentifyandExploitBehavioralBoundariesforUnitTesting

Presentedby:

RobMyers

AgileInstitute

Broughttoyouby:

350CorporateWay,Suite400,OrangePark,FL32073888---268---8770··[email protected]://www.techwell.com/

Page 2: Identify and Exploit Behavioral Boundaries for Unit Testing

RobMyersAgileInstituteThefounderofAgileInstitute,RobMyersteachescoursesthatarealwaysablendoffunandpracticalhands-onlabs,“TrainingFromtheBackoftheRoom”learningtechniques,andrelevantfirst-personstoriesfrombothsuccessfulandnot-so-successfulagileimplementations.Withthirtyyearsofprofessionalexperiencewithsoftwaredevelopmentteams,RobhasbeentrainingandcoachingorganizationsinScrumandExtremeProgrammingsince1998.Hecurrentlyworkswithtinystart-upsandhugeFortune100multinationals,helpingthemwithculturalchangeandessentialpracticesfromScrum,kanban,XP,andlean.

Page 3: Identify and Exploit Behavioral Boundaries for Unit Testing

6/5/16

1

5 June 2016 © Agile Institute 2008-2016 1

Identify & Exploit Behavioral Boundaries

for Unit Testing Rob Myers

@agilecoach

Better Software West 08 June 2016

Café!

5 June 2016 © Agile Institute 2008-2016 2

Unit Test A test of one smallest bit of behavior. Also known these days as a microtest.

Page 4: Identify and Exploit Behavioral Boundaries for Unit Testing

6/5/16

2

5 June 2016 © Agile Institute 2008-2016 3

5 June 2016 © Agile Institute 2008-2016 4

Page 5: Identify and Exploit Behavioral Boundaries for Unit Testing

6/5/16

3

5 June 2016 © Agile Institute 2008-2016 5

5 June 2016 © Agile Institute 2008-2016 6

“As the tests get more specific, the code gets more generic.”

– Bob Martin

Page 6: Identify and Exploit Behavioral Boundaries for Unit Testing

6/5/16

4

5 June 2016 © Agile Institute 2008-2016 7

“The more code you write without testing the behavioral

boundaries, the harder they are

to find & test later.”

– me

triangulation

5 June 2016 © Agile Institute 2008-2016 8

Page 7: Identify and Exploit Behavioral Boundaries for Unit Testing

6/5/16

5

5 June 2016 © Agile Institute 2008-2016 9

5 June 2016 © Agile Institute 2008-2016 10

dial setting

perc

ent

man

ufac

ture

r m

axim

um

Page 8: Identify and Exploit Behavioral Boundaries for Unit Testing

6/5/16

6

Pretend java.util.Set is BROKEN!

We need our own Set class with the capabilities of creating unions & intersections; checking if a set is empty; and also if it is equal to, a superset of, or a subset of another set.

5 June 2016 © Agile Institute 2008-2016 11

5 June 2016 © Agile Institute 2008-2016 12

empty?

Page 9: Identify and Exploit Behavioral Boundaries for Unit Testing

6/5/16

7

5 June 2016 © Agile Institute 2008-2016 13

@TestpublicvoidisEmptyWhenConstructed(){

Setset=newSet();assertThat(set.isEmpty(),is(true));

}

5 June 2016 © Agile Institute 2008-2016 14

not empty

3

Page 10: Identify and Exploit Behavioral Boundaries for Unit Testing

6/5/16

8

5 June 2016 © Agile Institute 2008-2016 15

@TestpublicvoidisNotEmptyWhenItemAdded(){

Setset=newSet();set.addElement(3);assertThat(set.isEmpty(),is(false));

}@TestpublicvoidisEmptyWhenConstructed(){

Setset=newSet();assertThat(set.isEmpty(),is(true));

}

5 June 2016 © Agile Institute 2008-2016 16

@Test(expected=IllegalArgumentException.class)publicvoidshouldNotAcceptNegativeNumbers(){

Setset=newSet();set.addElement(-1);

}

-1

Page 11: Identify and Exploit Behavioral Boundaries for Unit Testing

6/5/16

9

5 June 2016 © Agile Institute 2008-2016 17

A B

1 2

3 5

8 4

6 7

9

5 June 2016 © Agile Institute 2008-2016 18

Page 12: Identify and Exploit Behavioral Boundaries for Unit Testing

6/5/16

10

5 June 2016 © Agile Institute 2008-2016 19

booleansubsetOf(Setother){

returnsize()<=other.size();}booleansupersetOf(Setother){

returnsize()>=other.size();}

5 June 2016 © Agile Institute 2008-2016 20

A B

1 2

3 5

8 4

6 7

9

C

10 11

12

Page 13: Identify and Exploit Behavioral Boundaries for Unit Testing

6/5/16

11

5 June 2016 © Agile Institute 2008-2016 21

Hungover Intern Principle

We write the microtests that will warn a hung-over intern six months

from now as to why the implementation is the way it is—

however simple or complex, efficient or slow it may be.

*Protects against mistakes, not sabotage.

*

In order to be an acceptable password, a string must: q Have a length greater than 7 characters. q Contain at least one alphabetic character. q Contain at least one digit. q Contain a special character. q First character must be special or digit. 5 June 2016 © Agile Institute 2008-2016 22

Password Strength Checker

Page 14: Identify and Exploit Behavioral Boundaries for Unit Testing

6/5/16

12

5 June 2016 © Agile Institute 2008-2016 23

12345678

Good Bad

1234567

1234567B 12345678

Rule

Length > 7

Has alpha

passw0rd password Has digit

passw0rd? passw0rd Has special

?passw0rd passw0rd? 1st special/digit 4password?

5 June 2016 © Agile Institute 2008-2016 24

?passw0rd

Good Bad

?passw0

?passw0rd

?1234567

Rule

Length > 7

Has alpha

?passw0rd

?password Has digit

?passw0rd passw0rd Has special

?passw0rd passw0rd? 1st special/digit 4password?

Page 15: Identify and Exploit Behavioral Boundaries for Unit Testing

6/5/16

13

combinations

5 June 2016 © Agile Institute 2008-2016 25

X Y Z

A C E

A D E

A C F

A D F

B C E

B D E

B C F

B D F

pair-wise testing

5 June 2016 © Agile Institute 2008-2016 26

X Y Z

A C E

A D E

A C F

A D F

B C E

B D E

B C F

B D F

X Y Z

A C E

A D F

B D E

B C F

Page 16: Identify and Exploit Behavioral Boundaries for Unit Testing

6/5/16

14

5 June 2016 © Agile Institute 2008-2016 27

Listen to the Microtests

If you have difficulty unit-testing something,

this is indicating an opportunity to improve the design.

5 June 2016 © Agile Institute 2008-2016 28

Rule

+ bool passes(string pw)

PasswordStrengthChecker

+ bool isStrong( string password)

LengthGT7 HasDigit HasAlpha

HasSpecial FirstSpecialOrDigit

Page 17: Identify and Exploit Behavioral Boundaries for Unit Testing

6/5/16

15

5 June 2016 © Agile Institute 2008-2016 29

12345678

Good Bad

1234567

A3, 3A, 3A3

3

Rule

Length > 7

Has alpha

3A, A3, A3A

A Has digit

AB$, A$B A Has special

$A, 3A A$, A3 1st special/digit

5 June 2016 © Agile Institute 2008-2016 30

Rule

+ bool passes(string pw)

PasswordStrengthChecker

+ bool isStrong( string password)

LengthGT7 HasDigit HasAlpha

HasSpecial FirstSpecialOrDigit

Page 18: Identify and Exploit Behavioral Boundaries for Unit Testing

6/5/16

16

5 June 2016 © Agile Institute 2008-2016 31

Rule

+ bool passes(string pw)

PasswordStrengthChecker

+ bool isStrong( string password)

MockRule

+ bool passes(string pw) + void setPassesTo(bool p) + bool passesWasCalled()

LengthGT7 HasAlpha

HasSpecial

5 June 2016 © Agile Institute 2008-2016 32

Examples Boundary type

True / False Valid / Invalid Happy / Unhappy

Conditional

Iterative

Comparative Greater Than Less Than Before / After

Zero / One / Many Subtype

*

* Examples! Not intended as a comprehensive list.

Page 19: Identify and Exploit Behavioral Boundaries for Unit Testing

6/5/16

17

5 June 2016 © Agile Institute 2008-2016 33

5 June 2016 © Agile Institute 2008-2016 34

[email protected]

https://www.linkedin.com/in/robmyers64

@agilecoach