unit testing and pythonc4cs.github.io/lectures/f16/week5.pdfimport unittest import rpn class...

19
Unit Testing and Python Pat Pannuto / Marcus Darden

Upload: others

Post on 14-Jul-2020

11 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Unit Testing and Pythonc4cs.github.io/lectures/f16/week5.pdfimport unittest import rpn class TestBasics(unittest.TestCase): def test_add(self): result = rpn.calculate("1 1 +") self.assertEqual(2,

UnitTestingandPython

PatPannuto/MarcusDarden

Page 2: Unit Testing and Pythonc4cs.github.io/lectures/f16/week5.pdfimport unittest import rpn class TestBasics(unittest.TestCase): def test_add(self): result = rpn.calculate("1 1 +") self.assertEqual(2,

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

Mediantimetofinishwas90minutes

Thetarget,butonthelongendSomefolkssaidashighas3hours,askforhelpwhenyougetstuck!

Page 3: Unit Testing and Pythonc4cs.github.io/lectures/f16/week5.pdfimport unittest import rpn class TestBasics(unittest.TestCase): def test_add(self): result = rpn.calculate("1 1 +") self.assertEqual(2,

TestDrivenDevelopment

Page 4: Unit Testing and Pythonc4cs.github.io/lectures/f16/week5.pdfimport unittest import rpn class TestBasics(unittest.TestCase): def test_add(self): result = rpn.calculate("1 1 +") self.assertEqual(2,

"Strictlyspeaking"

1. Addatest2. Runthetestsuite

Note:Thisshouldfail!3. Writetheminimumcodetopass

tests4. Runtestsuite5. Refactor&repeat

Thepragmatist'sview:

AddtestsRuntestsWrite/fixcode

TestDrivenDevelopment

Page 5: Unit Testing and Pythonc4cs.github.io/lectures/f16/week5.pdfimport unittest import rpn class TestBasics(unittest.TestCase): def test_add(self): result = rpn.calculate("1 1 +") self.assertEqual(2,

"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 Pythonc4cs.github.io/lectures/f16/week5.pdfimport unittest import rpn class TestBasics(unittest.TestCase): def test_add(self): result = rpn.calculate("1 1 +") self.assertEqual(2,

WritingunittestsinPythonPython??

Page 7: Unit Testing and Pythonc4cs.github.io/lectures/f16/week5.pdfimport unittest import rpn class TestBasics(unittest.TestCase): def test_add(self): result = rpn.calculate("1 1 +") self.assertEqual(2,

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 Pythonc4cs.github.io/lectures/f16/week5.pdfimport unittest import rpn class TestBasics(unittest.TestCase): def test_add(self): result = rpn.calculate("1 1 +") self.assertEqual(2,

QuickrefresheronRPNcalculatorsAlsoa"stack-based"calculatorrpncalc>11+

2.0

rpncalc>11+2*

4.0

rpncalc>123+

Error:Malformedexpression

Page 9: Unit Testing and Pythonc4cs.github.io/lectures/f16/week5.pdfimport unittest import rpn class TestBasics(unittest.TestCase): def test_add(self): result = rpn.calculate("1 1 +") self.assertEqual(2,

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 Pythonc4cs.github.io/lectures/f16/week5.pdfimport unittest import rpn class TestBasics(unittest.TestCase): def test_add(self): result = rpn.calculate("1 1 +") self.assertEqual(2,

Don'tforgetgit !$wc-l*py

11rpn.py

8test_rpn.py

19total

#Thisis19linesofqualitycodehere!

Yes,we'recommittingbeforeanythingworks

ThestructureisgoodThetestharnessworks

Page 11: Unit Testing and Pythonc4cs.github.io/lectures/f16/week5.pdfimport unittest import rpn class TestBasics(unittest.TestCase): def test_add(self): result = rpn.calculate("1 1 +") self.assertEqual(2,

Andlet'snotforgetmake whilewe'reatitBecausewhytype19letterswhenyoucouldtype4?

test:

python3-munittest

.PHONY:test

Page 12: Unit Testing and Pythonc4cs.github.io/lectures/f16/week5.pdfimport unittest import rpn class TestBasics(unittest.TestCase): def test_add(self): result = rpn.calculate("1 1 +") self.assertEqual(2,

LivecodingPLEASEstopmeandaskquestionsifyou'reconfused

PLEASEyellatmetoslowdownifIgotoofast

ImplementaddNeedastackforthecalculatorNeedtotokenizetheinputNeedtoprocesstokens

Page 13: Unit Testing and Pythonc4cs.github.io/lectures/f16/week5.pdfimport unittest import rpn class TestBasics(unittest.TestCase): def test_add(self): result = rpn.calculate("1 1 +") self.assertEqual(2,

LivecodingPLEASEstopmeandaskquestionsifyou'reconfused

PLEASEyellatmetoslowdownifIgotoofast

Implementadd

NeedastackforthecalculatorNeedtotokenizetheinputNeedtoprocesstokens

Addtestforsubtract

Page 14: Unit Testing and Pythonc4cs.github.io/lectures/f16/week5.pdfimport unittest import rpn class TestBasics(unittest.TestCase): def test_add(self): result = rpn.calculate("1 1 +") self.assertEqual(2,

LivecodingPLEASEstopmeandaskquestionsifyou'reconfused

PLEASEyellatmetoslowdownifIgotoofast

Implementadd

NeedastackforthecalculatorNeedtotokenizetheinputNeedtoprocesstokens

Addtestforsubtract

Implementsubtract

Page 15: Unit Testing and Pythonc4cs.github.io/lectures/f16/week5.pdfimport unittest import rpn class TestBasics(unittest.TestCase): def test_add(self): result = rpn.calculate("1 1 +") self.assertEqual(2,

LivecodingPLEASEstopmeandaskquestionsifyou'reconfused

PLEASEyellatmetoslowdownifIgotoofast

Implementadd

NeedastackforthecalculatorNeedtotokenizetheinputNeedtoprocesstokens

Addtestforsubtract

Implementsubtract

Testscanexpectfailure:malformedinput

Page 16: Unit Testing and Pythonc4cs.github.io/lectures/f16/week5.pdfimport unittest import rpn class TestBasics(unittest.TestCase): def test_add(self): result = rpn.calculate("1 1 +") self.assertEqual(2,

LivecodingPLEASEstopmeandaskquestionsifyou'reconfused

PLEASEyellatmetoslowdownifIgotoofast

Implementadd

NeedastackforthecalculatorNeedtotokenizetheinputNeedtoprocesstokens

Addtestforsubtract

Implementsubtract

Testscanexpectfailure:malformedinput

Onyourown:Testsandimplementationformultiply,divide

Page 17: Unit Testing and Pythonc4cs.github.io/lectures/f16/week5.pdfimport unittest import rpn class TestBasics(unittest.TestCase): def test_add(self): result = rpn.calculate("1 1 +") self.assertEqual(2,

SomefancyPythonandthebigrefactorMotivation:Unwieldyif-elsechaingoing

GetsworseasmoreoperandsareaddedAmodulardesignwillallowflexibility

Page 18: Unit Testing and Pythonc4cs.github.io/lectures/f16/week5.pdfimport unittest import rpn class TestBasics(unittest.TestCase): def test_add(self): result = rpn.calculate("1 1 +") self.assertEqual(2,

SomefancyPythonandthebigrefactorMotivation:Unwieldyif-elsechaingoing

GetsworseasmoreoperandsareaddedAmodulardesignwillallowflexibility

Goal:SimplifyparsercodeIsitanumber?ThenaddtostackElselookupoperatorandexecute

Page 19: Unit Testing and Pythonc4cs.github.io/lectures/f16/week5.pdfimport unittest import rpn class TestBasics(unittest.TestCase): def test_add(self): result = rpn.calculate("1 1 +") self.assertEqual(2,

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