unit testing and python

19
Unit Testing and Python Pat Pannuto / Marcus Darden

Upload: hatuyen

Post on 03-Jan-2017

237 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Unit Testing and Python

UnitTestingandPython

PatPannuto/MarcusDarden

Page 2: Unit Testing and Python

QuickupdateonHW'sFromthe git homework,only~40%madea .gitignore file?

Mediantimetofinishwas90minutes

Thetarget,butonthelongendSomefolkssaidashighas3hours,askforhelpwhenyougetstuck!

Page 3: Unit Testing and Python

TestDrivenDevelopment

Page 4: Unit Testing and Python

"Strictlyspeaking"

1. Addatest2. Runthetestsuite

Note:Thisshouldfail!3. Writetheminimumcodetopass

tests4. Runtestsuite5. Refactor&repeat

Thepragmatist'sview:

AddtestsRuntestsWrite/fixcode

TestDrivenDevelopment

Page 5: Unit Testing and Python

"Strictlyspeaking"

1. Addatest2. Runthetestsuite

Note:Thisshouldfail!3. Writetheminimumcodetopass

tests4. Runtestsuite5. Refactor&repeat

Thepragmatist'sview:

AddtestsRuntestsWrite/fixcode

TestDrivenDevelopment

TDDcanunfairlyfocuson"micro-tests"Moretests!=bettertests,anddomeanmoremaintenance

Page 6: Unit Testing and Python

WritingunittestsinPythonPython??

Page 7: Unit Testing and Python

Gettingstarted,createrpn.py#!/usr/bin/envpython3

defcalculate(string):

pass

defmain():

whileTrue:

calculate(input("rpncalc>"))

if__name__=='__main__':#Notethat's"underscoreunderscorename..."

main()

$python3rpn.py

rpncalc>typeanythinghereandhitenter

rpncalc>

Page 8: Unit Testing and Python

QuickrefresheronRPNcalculatorsAlsoa"stack-based"calculatorrpncalc>11+

2.0

rpncalc>11+2*

4.0

rpncalc>123+

Error:Malformedexpression

Page 9: Unit Testing and Python

Createtest_rpn.pyimportunittest

importrpn

classTestBasics(unittest.TestCase):

deftest_add(self):

result=rpn.calculate("11+")

self.assertEqual(2,result)

Thenamematters!Notethat test_rpn.py tests rpn.py

$python3-munittest

F

======================================================================

FAIL:test_add(test_rpn.TestBasics)

----------------------------------------------------------------------

Traceback(mostrecentcalllast):

File"/Users/ppannuto/Dropbox/school/c4cs/lectures/tdd/test_rpn.py",line8,

intest_add

self.assertEqual(2,result)

AssertionError:2!=None

Page 10: Unit Testing and Python

Don'tforgetgit !$wc-l*py

11rpn.py

8test_rpn.py

19total

#Thisis19linesofqualitycodehere!

Yes,we'recommittingbeforeanythingworks

ThestructureisgoodThetestharnessworks

Page 11: Unit Testing and Python

Andlet'snotforgetmake whilewe'reatitBecausewhytype19letterswhenyoucouldtype4?

test:

python3-munittest

.PHONY:test

Page 12: Unit Testing and Python

LivecodingPLEASEstopmeandaskquestionsifyou'reconfused

PLEASEyellatmetoslowdownifIgotoofast

ImplementaddNeedastackforthecalculatorNeedtotokenizetheinputNeedtoprocesstokens

Page 13: Unit Testing and Python

LivecodingPLEASEstopmeandaskquestionsifyou'reconfused

PLEASEyellatmetoslowdownifIgotoofast

Implementadd

NeedastackforthecalculatorNeedtotokenizetheinputNeedtoprocesstokens

Addtestforsubtract

Page 14: Unit Testing and Python

LivecodingPLEASEstopmeandaskquestionsifyou'reconfused

PLEASEyellatmetoslowdownifIgotoofast

Implementadd

NeedastackforthecalculatorNeedtotokenizetheinputNeedtoprocesstokens

Addtestforsubtract

Implementsubtract

Page 15: Unit Testing and Python

LivecodingPLEASEstopmeandaskquestionsifyou'reconfused

PLEASEyellatmetoslowdownifIgotoofast

Implementadd

NeedastackforthecalculatorNeedtotokenizetheinputNeedtoprocesstokens

Addtestforsubtract

Implementsubtract

Testscanexpectfailure:malformedinput

Page 16: Unit Testing and Python

LivecodingPLEASEstopmeandaskquestionsifyou'reconfused

PLEASEyellatmetoslowdownifIgotoofast

Implementadd

NeedastackforthecalculatorNeedtotokenizetheinputNeedtoprocesstokens

Addtestforsubtract

Implementsubtract

Testscanexpectfailure:malformedinput

Onyourown:Testsandimplementationformultiply,divide

Page 17: Unit Testing and Python

SomefancyPythonandthebigrefactorMotivation:Unwieldyif-elsechaingoing

GetsworseasmoreoperandsareaddedAmodulardesignwillallowflexibility

Page 18: Unit Testing and Python

SomefancyPythonandthebigrefactorMotivation:Unwieldyif-elsechaingoing

GetsworseasmoreoperandsareaddedAmodulardesignwillallowflexibility

Goal:SimplifyparsercodeIsitanumber?ThenaddtostackElselookupoperatorandexecute

Page 19: Unit Testing and Python

Attendance:Pushyourcodetogitlab1. Gotohttps://gitlab.eecs.umich.edu2. Click"NewProject"3. Nameyourprojectexactly: c4cs-f16-rpn4. Setyourprojecttopublicallyvisible

5. ScrolldownandfollowthedirectionsforexistingfolderorGitrepository

Youshouldn'tneedtocreatearepo(wealreadydidthat)Makesureyou'vecommittedallyourchanges!gitremoteadd.....

gitpush-uoriginmaster

Yourusernameisyouruniqname,andpasswordisyourMichiganpassword