getting existing code under tests

54

Upload: llewellyn-falco

Post on 05-Jul-2015

182 views

Category:

Software


1 download

DESCRIPTION

How to get high test coverage in a very short period of time

TRANSCRIPT

Page 1: Getting existing code under tests
Page 2: Getting existing code under tests

Confidential and Proprietary and belongs to Mitchell. This document, or its contents, is NOT to be shared or

redistributed without the express consent of Mitchell International. ©2014 Mitchell International, Inc.

Getting Existing Code Under Tests

@LlewellynFalco

Page 3: Getting existing code under tests

90% Coverage in 1 Hour

Page 4: Getting existing code under tests

1. Lock simple code2. Lock complex code3. Lock system configuration

Page 5: Getting existing code under tests

Confidential and Proprietary and belongs to Mitchell. This document, or its contents, is NOT to be shared or

redistributed without the express consent of Mitchell International. ©2014 Mitchell International, Inc.

POP QUIZ

Page 6: Getting existing code under tests

Confidential and Proprietary and belongs to Mitchell. This document, or its contents, is NOT to be shared or

redistributed without the express consent of Mitchell International. ©2014 Mitchell International, Inc.

QUESTION 1

Page 7: Getting existing code under tests

public void closeConnection(){

var con = getConnectionForThread();if (con == null){

try{

con.close();}catch{

//ignore}

}}

What’s Wrong With this code?

Close Connection

doesn’t

Page 8: Getting existing code under tests

Confidential and Proprietary and belongs to Mitchell. This document, or its contents, is NOT to be shared or

redistributed without the express consent of Mitchell International. ©2014 Mitchell International, Inc.

POSSIBLE FIXES:

1) CHANGE CODE

Page 9: Getting existing code under tests

Confidential and Proprietary and belongs to Mitchell. This document, or its contents, is NOT to be shared or

redistributed without the express consent of Mitchell International. ©2014 Mitchell International, Inc.

POSSIBLE FIXES:

1) CHANGE CODE2) CHANGE NAME

Page 10: Getting existing code under tests

OnlyPretendToCloseConnection()

Page 11: Getting existing code under tests

Confidential and Proprietary and belongs to Mitchell. This document, or its contents, is NOT to be shared or

redistributed without the express consent of Mitchell International. ©2014 Mitchell International, Inc.

2 WRONGS != RIGHT

Page 12: Getting existing code under tests

Confidential and Proprietary and belongs to Mitchell. This document, or its contents, is NOT to be shared or

redistributed without the express consent of Mitchell International. ©2014 Mitchell International, Inc.

But 3 Lefts Do!

Page 13: Getting existing code under tests

Confidential and Proprietary and belongs to Mitchell. This document, or its contents, is NOT to be shared or

redistributed without the express consent of Mitchell International. ©2014 Mitchell International, Inc.

QUESTION 2

Page 14: Getting existing code under tests

public double calculate(double amount)

{

int step1 = (int) (amount * 2);

double step2 = step1 * 1.5;

return step2;

}

Original Code

Page 15: Getting existing code under tests

public double calculate(double

amount)

{

int step1 = (int) (amount * 2);

double step2 = step1 * 1.5;

return step2;

}

public double calculate(double

amount)

{

return amount * 3;

}

Is this ok?

Page 16: Getting existing code under tests

1 => 3

ORIGINALREFACTORED

1 => 3

-1 => -3

4 => 12

3.2 => 9.6

-1 => -3

4 => 12

3.2 => 9

Page 17: Getting existing code under tests

Confidential and Proprietary and belongs to Mitchell. This document, or its contents, is NOT to be shared or

redistributed without the express consent of Mitchell International. ©2014 Mitchell International, Inc.

WHY?

Page 18: Getting existing code under tests

public double calculate(double amount)

{

int step1 = (int) (amount * 2);

double step2 = step1 * 1.5;

return step2;

}

Original Code

RoundsDown

Page 19: Getting existing code under tests

Confidential and Proprietary and belongs to Mitchell. This document, or its contents, is NOT to be shared or

redistributed without the express consent of Mitchell International. ©2014 Mitchell International, Inc.

HOWIMPORTANT IS

0.6?

Page 20: Getting existing code under tests

Confidential and Proprietary and belongs to Mitchell. This document, or its contents, is NOT to be shared or

redistributed without the express consent of Mitchell International. ©2014 Mitchell International, Inc.

FONT SIZE?

Page 21: Getting existing code under tests

Confidential and Proprietary and belongs to Mitchell. This document, or its contents, is NOT to be shared or

redistributed without the express consent of Mitchell International. ©2014 Mitchell International, Inc.

BANKING?

Page 22: Getting existing code under tests

Confidential and Proprietary and belongs to Mitchell. This document, or its contents, is NOT to be shared or

redistributed without the express consent of Mitchell International. ©2014 Mitchell International, Inc.

MEDICATION?

Page 23: Getting existing code under tests

Confidential and Proprietary and belongs to Mitchell. This document, or its contents, is NOT to be shared or

redistributed without the express consent of Mitchell International. ©2014 Mitchell International, Inc.

Page 24: Getting existing code under tests

Confidential and Proprietary and belongs to Mitchell. This document, or its contents, is NOT to be shared or

redistributed without the express consent of Mitchell International. ©2014 Mitchell International, Inc.

QUESTION 3

Page 25: Getting existing code under tests

Confidential and Proprietary and belongs to Mitchell. This document, or its contents, is NOT to be shared or

redistributed without the express consent of Mitchell International. ©2014 Mitchell International, Inc.

THESE 415 LINES HAVE BEEN REFACTORED TO THESE 213 LINES.

IS THERE A BUG?

Page 26: Getting existing code under tests

ORIGINAL

REFACTORED

Page 27: Getting existing code under tests

Confidential and Proprietary and belongs to Mitchell. This document, or its contents, is NOT to be shared or

redistributed without the express consent of Mitchell International. ©2014 Mitchell International, Inc.

ANSWER:

ARE THE TESTS STILLPASSING?

Page 28: Getting existing code under tests

Confidential and Proprietary and belongs to Mitchell. This document, or its contents, is NOT to be shared or

redistributed without the express consent of Mitchell International. ©2014 Mitchell International, Inc.

Page 29: Getting existing code under tests

Confidential and Proprietary and belongs to Mitchell. This document, or its contents, is NOT to be shared or

redistributed without the express consent of Mitchell International. ©2014 Mitchell International, Inc.

Page 30: Getting existing code under tests

Confidential and Proprietary and belongs to Mitchell. This document, or its contents, is NOT to be shared or

redistributed without the express consent of Mitchell International. ©2014 Mitchell International, Inc.

HOW?

Page 31: Getting existing code under tests

Confidential and Proprietary and belongs to Mitchell. This document, or its contents, is NOT to be shared or

redistributed without the express consent of Mitchell International. ©2014 Mitchell International, Inc.

Benefits of Unit Tests

1) Specification

2) Feedback

3) Regression

4) Granularity

Page 32: Getting existing code under tests

Confidential and Proprietary and belongs to Mitchell. This document, or its contents, is NOT to be shared or

redistributed without the express consent of Mitchell International. ©2014 Mitchell International, Inc.

Benefits of Unit Tests

1) Specification

2) Feedback

3) Regression

4) Granularity

Page 33: Getting existing code under tests

Confidential and Proprietary and belongs to Mitchell. This document, or its contents, is NOT to be shared or

redistributed without the express consent of Mitchell International. ©2014 Mitchell International, Inc.

Regression

If I used to get :

Then I still get :

Page 34: Getting existing code under tests

Confidential and Proprietary and belongs to Mitchell. This document, or its contents, is NOT to be shared or

redistributed without the express consent of Mitchell International. ©2014 Mitchell International, Inc.

LOCKINGTEST

Page 35: Getting existing code under tests

Confidential and Proprietary and belongs to Mitchell. This document, or its contents, is NOT to be shared or

redistributed without the express consent of Mitchell International. ©2014 Mitchell International, Inc.

FUNCTIONALIS EASY

Page 36: Getting existing code under tests

Confidential and Proprietary and belongs to Mitchell. This document, or its contents, is NOT to be shared or

redistributed without the express consent of Mitchell International. ©2014 Mitchell International, Inc.

WHAT DOES THAT MEAN?

Page 37: Getting existing code under tests

Functional(referential transparency)

Deterministic

All inputs in

All results out

Page 38: Getting existing code under tests

Confidential and Proprietary and belongs to Mitchell. This document, or its contents, is NOT to be shared or

redistributed without the express consent of Mitchell International. ©2014 Mitchell International, Inc.

public double calculate(double amount){

int step1 = (int) (amount * 2);double step2 = step1 * 1.5;return step2;

}

All results out

All inputs in

Deterministic

Page 39: Getting existing code under tests

Confidential and Proprietary and belongs to Mitchell. This document, or its contents, is NOT to be shared or

redistributed without the express consent of Mitchell International. ©2014 Mitchell International, Inc.

CAN I RUN THE CODE?

DOES IT ALWAYS PRODUCETHE SAME THING?

Page 40: Getting existing code under tests

Confidential and Proprietary and belongs to Mitchell. This document, or its contents, is NOT to be shared or

redistributed without the express consent of Mitchell International. ©2014 Mitchell International, Inc.

public int Advance(){

return steps++;}

All inputs in?

Page 41: Getting existing code under tests

Confidential and Proprietary and belongs to Mitchell. This document, or its contents, is NOT to be shared or

redistributed without the express consent of Mitchell International. ©2014 Mitchell International, Inc.

public int age(DateTime birthDate){

var timeSpan = DateTime.Now -birthDate;

var age = DateTime.MinValue +timeSpan;

return age.Year-1;}

All inputs in?

Deterministic?

Page 42: Getting existing code under tests

Confidential and Proprietary and belongs to Mitchell. This document, or its contents, is NOT to be shared or

redistributed without the express consent of Mitchell International. ©2014 Mitchell International, Inc.

public void saveFile(Person info, string fileName){

File.WriteAllText(fileName, info.ToString());}

All results out?

Page 43: Getting existing code under tests

Confidential and Proprietary and belongs to Mitchell. This document, or its contents, is NOT to be shared or

redistributed without the express consent of Mitchell International. ©2014 Mitchell International, Inc.

DEMO:

LOCKING FUNCTIONAL

Page 44: Getting existing code under tests

Confidential and Proprietary and belongs to Mitchell. This document, or its contents, is NOT to be shared or

redistributed without the express consent of Mitchell International. ©2014 Mitchell International, Inc.

POKE

Page 45: Getting existing code under tests

Confidential and Proprietary and belongs to Mitchell. This document, or its contents, is NOT to be shared or

redistributed without the express consent of Mitchell International. ©2014 Mitchell International, Inc.

LOCKINGNON-FUNCTIONAL

Page 46: Getting existing code under tests

Confidential and Proprietary and belongs to Mitchell. This document, or its contents, is NOT to be shared or

redistributed without the express consent of Mitchell International. ©2014 Mitchell International, Inc.

NON-FUNCTIONALIS HARD

Page 47: Getting existing code under tests

Confidential and Proprietary and belongs to Mitchell. This document, or its contents, is NOT to be shared or

redistributed without the express consent of Mitchell International. ©2014 Mitchell International, Inc.

Reduce To Functional

Easy

Hard

Yes

No

Functional

?

Reduce to

Functional

Page 48: Getting existing code under tests

Confidential and Proprietary and belongs to Mitchell. This document, or its contents, is NOT to be shared or

redistributed without the express consent of Mitchell International. ©2014 Mitchell International, Inc.

LoggingLog

Easy

Capture of

behavior

Hard

public Logs LegacyCode(Inputs[] i)

Page 49: Getting existing code under tests

Confidential and Proprietary and belongs to Mitchell. This document, or its contents, is NOT to be shared or

redistributed without the express consent of Mitchell International. ©2014 Mitchell International, Inc.

Files(name & size is usually enough)

FileA 34,368FileB 15,632FileC 28,453

public FileListing LegacyCode(Inputs[] i)

Page 50: Getting existing code under tests

Confidential and Proprietary and belongs to Mitchell. This document, or its contents, is NOT to be shared or

redistributed without the express consent of Mitchell International. ©2014 Mitchell International, Inc.

Databases✔

public SqlStatements LegacyCode(Inputs[] i)

Page 51: Getting existing code under tests

Confidential and Proprietary and belongs to Mitchell. This document, or its contents, is NOT to be shared or

redistributed without the express consent of Mitchell International. ©2014 Mitchell International, Inc.

System & Runtime

Configuration

Start

Log

Easy Capture

of

Configuration

public RuntimeConfig LegacyCode(Inputs[] i)

Page 52: Getting existing code under tests

Confidential and Proprietary and belongs to Mitchell. This document, or its contents, is NOT to be shared or

redistributed without the express consent of Mitchell International. ©2014 Mitchell International, Inc.

www.ApprovalTests.com

21 episode YouTube series

Page 53: Getting existing code under tests

Confidential and Proprietary and belongs to Mitchell. This document, or its contents, is NOT to be shared or

redistributed without the express consent of Mitchell International. ©2014 Mitchell International, Inc.

pluralsight.com/kids

Page 54: Getting existing code under tests

Confidential and Proprietary and belongs to Mitchell. This document, or its contents, is NOT to be shared or

redistributed without the express consent of Mitchell International. ©2014 Mitchell International, Inc.

Contact Information

@LlewellynFalco

http://LlewellynFalco.Blogspot.com

http://www.approvaltests.com

http://lfal.co/ExtraResources