getting existing code under tests
DESCRIPTION
How to get high test coverage in a very short period of timeTRANSCRIPT
![Page 1: Getting existing code under tests](https://reader033.vdocuments.site/reader033/viewer/2022052909/559887881a28ab5b398b45e5/html5/thumbnails/1.jpg)
![Page 2: Getting existing code under tests](https://reader033.vdocuments.site/reader033/viewer/2022052909/559887881a28ab5b398b45e5/html5/thumbnails/2.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022052909/559887881a28ab5b398b45e5/html5/thumbnails/3.jpg)
90% Coverage in 1 Hour
![Page 4: Getting existing code under tests](https://reader033.vdocuments.site/reader033/viewer/2022052909/559887881a28ab5b398b45e5/html5/thumbnails/4.jpg)
1. Lock simple code2. Lock complex code3. Lock system configuration
![Page 5: Getting existing code under tests](https://reader033.vdocuments.site/reader033/viewer/2022052909/559887881a28ab5b398b45e5/html5/thumbnails/5.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022052909/559887881a28ab5b398b45e5/html5/thumbnails/6.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022052909/559887881a28ab5b398b45e5/html5/thumbnails/7.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022052909/559887881a28ab5b398b45e5/html5/thumbnails/8.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022052909/559887881a28ab5b398b45e5/html5/thumbnails/9.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022052909/559887881a28ab5b398b45e5/html5/thumbnails/10.jpg)
OnlyPretendToCloseConnection()
![Page 11: Getting existing code under tests](https://reader033.vdocuments.site/reader033/viewer/2022052909/559887881a28ab5b398b45e5/html5/thumbnails/11.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022052909/559887881a28ab5b398b45e5/html5/thumbnails/12.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022052909/559887881a28ab5b398b45e5/html5/thumbnails/13.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022052909/559887881a28ab5b398b45e5/html5/thumbnails/14.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022052909/559887881a28ab5b398b45e5/html5/thumbnails/15.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022052909/559887881a28ab5b398b45e5/html5/thumbnails/16.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022052909/559887881a28ab5b398b45e5/html5/thumbnails/17.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022052909/559887881a28ab5b398b45e5/html5/thumbnails/18.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022052909/559887881a28ab5b398b45e5/html5/thumbnails/19.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022052909/559887881a28ab5b398b45e5/html5/thumbnails/20.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022052909/559887881a28ab5b398b45e5/html5/thumbnails/21.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022052909/559887881a28ab5b398b45e5/html5/thumbnails/22.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022052909/559887881a28ab5b398b45e5/html5/thumbnails/23.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022052909/559887881a28ab5b398b45e5/html5/thumbnails/24.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022052909/559887881a28ab5b398b45e5/html5/thumbnails/25.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022052909/559887881a28ab5b398b45e5/html5/thumbnails/26.jpg)
ORIGINAL
REFACTORED
![Page 27: Getting existing code under tests](https://reader033.vdocuments.site/reader033/viewer/2022052909/559887881a28ab5b398b45e5/html5/thumbnails/27.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022052909/559887881a28ab5b398b45e5/html5/thumbnails/28.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022052909/559887881a28ab5b398b45e5/html5/thumbnails/29.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022052909/559887881a28ab5b398b45e5/html5/thumbnails/30.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022052909/559887881a28ab5b398b45e5/html5/thumbnails/31.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022052909/559887881a28ab5b398b45e5/html5/thumbnails/32.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022052909/559887881a28ab5b398b45e5/html5/thumbnails/33.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022052909/559887881a28ab5b398b45e5/html5/thumbnails/34.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022052909/559887881a28ab5b398b45e5/html5/thumbnails/35.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022052909/559887881a28ab5b398b45e5/html5/thumbnails/36.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022052909/559887881a28ab5b398b45e5/html5/thumbnails/37.jpg)
Functional(referential transparency)
Deterministic
All inputs in
All results out
![Page 38: Getting existing code under tests](https://reader033.vdocuments.site/reader033/viewer/2022052909/559887881a28ab5b398b45e5/html5/thumbnails/38.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022052909/559887881a28ab5b398b45e5/html5/thumbnails/39.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022052909/559887881a28ab5b398b45e5/html5/thumbnails/40.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022052909/559887881a28ab5b398b45e5/html5/thumbnails/41.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022052909/559887881a28ab5b398b45e5/html5/thumbnails/42.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022052909/559887881a28ab5b398b45e5/html5/thumbnails/43.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022052909/559887881a28ab5b398b45e5/html5/thumbnails/44.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022052909/559887881a28ab5b398b45e5/html5/thumbnails/45.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022052909/559887881a28ab5b398b45e5/html5/thumbnails/46.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022052909/559887881a28ab5b398b45e5/html5/thumbnails/47.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022052909/559887881a28ab5b398b45e5/html5/thumbnails/48.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022052909/559887881a28ab5b398b45e5/html5/thumbnails/49.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022052909/559887881a28ab5b398b45e5/html5/thumbnails/50.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022052909/559887881a28ab5b398b45e5/html5/thumbnails/51.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022052909/559887881a28ab5b398b45e5/html5/thumbnails/52.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022052909/559887881a28ab5b398b45e5/html5/thumbnails/53.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022052909/559887881a28ab5b398b45e5/html5/thumbnails/54.jpg)
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