CSE101: Introduction To Programming Lab 4 Section 1: Defining Your Own
CSE101: Introduction To Programming Lab 4 Section 1: Defining Your Own
CSE101:
Lab4
Section1:Definingyourownmodule
OnceyouhaveimplementedthemethodsinSection1,thenextstageisto‘test’if
your
implementationbehavesaccordingtoyourexpectationsornot.Ifthereareerrorsin
yourimplementation,itcanbecaughtthroughcarefultestingofthecode.
Youmaytakea lookatthedocumentationonunittest:
https://docs.python.org/3/library/unittest.html
Wehaveshareda testcode,calledtesting.py,alongwiththisdocument.Youhaveto
savelab4.pyandtesting.pyinsamedirectory.Openthetesting.pyinaneditor.Notice
thattesting.pyhasa testcodetotestthefivefunctionsdefinedinfirstmodule.The
testing.pymoduleimportslab4module.Therearevarioususefulfunctionsdefinedin
theunittestmodule.Let’sconsideranimportantonethatyouwillberequiredtousein
thislab.
assertEqual(first,second,msg=None)
Teststhatfirstandsecondareequal.Ifthevaluesdonotcompareequal,thetest
willfail.Normally,wegivefirstastheresultreturnedfromtheunitwearetesting.In
our
case,thefirstargumentwouldbea calltothefunctionwearetesting.Thesecond
argumentistheoutputexpectedbytheprogrammer.Ifthetwodiffer,itimplies,the
functionbeingtestedfailsourtest.
In testing.py module, we have imported the lab3 module,as we would need its
functions
fortesting.Ignoreunfamiliarkeywords(likeclass,self)inthemodulefornow.Wehave
alreadydefineda testmethod,calledsecond_max,totestthesecond_maxfunction.
Therearethreetestcasesdefinedinit
1.self.assertEqual(second_max(1,2,3,4),3)
2.self.assertEqual(second_max(-1,-2,-3,-4),-2)
3.self.assertEqual(second_max(-1,20,-30,100),20)
Nowhereareyourtaskstoperformonthetesting.py.Youhavetoruntesting.py
andcheckifyourlab4runsperfectly.