Post on 14-Jul-2020
UnitTestingandPython
PatPannuto/MarcusDarden
QuickupdateonHW'sFromthe git homework,only~40%madea .gitignore file?
Mediantimetofinishwas90minutes
Thetarget,butonthelongendSomefolkssaidashighas3hours,askforhelpwhenyougetstuck!
TestDrivenDevelopment
"Strictlyspeaking"
1. Addatest2. Runthetestsuite
Note:Thisshouldfail!3. Writetheminimumcodetopass
tests4. Runtestsuite5. Refactor&repeat
Thepragmatist'sview:
AddtestsRuntestsWrite/fixcode
TestDrivenDevelopment
"Strictlyspeaking"
1. Addatest2. Runthetestsuite
Note:Thisshouldfail!3. Writetheminimumcodetopass
tests4. Runtestsuite5. Refactor&repeat
Thepragmatist'sview:
AddtestsRuntestsWrite/fixcode
TestDrivenDevelopment
TDDcanunfairlyfocuson"micro-tests"Moretests!=bettertests,anddomeanmoremaintenance
WritingunittestsinPythonPython??
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>
QuickrefresheronRPNcalculatorsAlsoa"stack-based"calculatorrpncalc>11+
2.0
rpncalc>11+2*
4.0
rpncalc>123+
Error:Malformedexpression
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
Don'tforgetgit !$wc-l*py
11rpn.py
8test_rpn.py
19total
#Thisis19linesofqualitycodehere!
Yes,we'recommittingbeforeanythingworks
ThestructureisgoodThetestharnessworks
Andlet'snotforgetmake whilewe'reatitBecausewhytype19letterswhenyoucouldtype4?
test:
python3-munittest
.PHONY:test
LivecodingPLEASEstopmeandaskquestionsifyou'reconfused
PLEASEyellatmetoslowdownifIgotoofast
ImplementaddNeedastackforthecalculatorNeedtotokenizetheinputNeedtoprocesstokens
LivecodingPLEASEstopmeandaskquestionsifyou'reconfused
PLEASEyellatmetoslowdownifIgotoofast
Implementadd
NeedastackforthecalculatorNeedtotokenizetheinputNeedtoprocesstokens
Addtestforsubtract
LivecodingPLEASEstopmeandaskquestionsifyou'reconfused
PLEASEyellatmetoslowdownifIgotoofast
Implementadd
NeedastackforthecalculatorNeedtotokenizetheinputNeedtoprocesstokens
Addtestforsubtract
Implementsubtract
LivecodingPLEASEstopmeandaskquestionsifyou'reconfused
PLEASEyellatmetoslowdownifIgotoofast
Implementadd
NeedastackforthecalculatorNeedtotokenizetheinputNeedtoprocesstokens
Addtestforsubtract
Implementsubtract
Testscanexpectfailure:malformedinput
LivecodingPLEASEstopmeandaskquestionsifyou'reconfused
PLEASEyellatmetoslowdownifIgotoofast
Implementadd
NeedastackforthecalculatorNeedtotokenizetheinputNeedtoprocesstokens
Addtestforsubtract
Implementsubtract
Testscanexpectfailure:malformedinput
Onyourown:Testsandimplementationformultiply,divide
SomefancyPythonandthebigrefactorMotivation:Unwieldyif-elsechaingoing
GetsworseasmoreoperandsareaddedAmodulardesignwillallowflexibility
SomefancyPythonandthebigrefactorMotivation:Unwieldyif-elsechaingoing
GetsworseasmoreoperandsareaddedAmodulardesignwillallowflexibility
Goal:SimplifyparsercodeIsitanumber?ThenaddtostackElselookupoperatorandexecute
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