Getting existing code under tests

Post on 05-Jul-2015

177 views 1 download

description

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

Transcript of 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

90% Coverage in 1 Hour

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

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

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

public void closeConnection(){

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

try{

con.close();}catch{

//ignore}

}}

What’s Wrong With this code?

Close Connection

doesn’t

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

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

OnlyPretendToCloseConnection()

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

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!

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

public double calculate(double amount)

{

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

double step2 = step1 * 1.5;

return step2;

}

Original Code

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?

1 => 3

ORIGINALREFACTORED

1 => 3

-1 => -3

4 => 12

3.2 => 9.6

-1 => -3

4 => 12

3.2 => 9

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?

public double calculate(double amount)

{

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

double step2 = step1 * 1.5;

return step2;

}

Original Code

RoundsDown

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?

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?

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?

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?

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.

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

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?

ORIGINAL

REFACTORED

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?

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.

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.

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?

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

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

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 :

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

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

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?

Functional(referential transparency)

Deterministic

All inputs in

All results out

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

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?

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?

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?

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?

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

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

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

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

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

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)

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)

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)

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)

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

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

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