# python has a built in unit test module "unittest" # # Using python's unittest module is similar to NUnit # or JUnit (if you are familiar with those). # import unittest # my example function # Normally you would have the class in its own # file and then just import ExampleFunction into # the unit test file. class ExampleFunction(object): def __init__(self): self.example = 0 self.example2 = 1 self.example3 = 2 def processVariable(self, item): item *= self.example2 * self.example3 return item def setExampleVariables(self, example): self.example = example self.example2 = self.example + 1 self.example3 = self.example2 + 1 # The test class inherits from unittest's TestCase. # Inheriting ensures that the tests will run when you # call unittest.Main() class TestExampleFunction(unittest.TestCase): def setUp(self): self.ef = ExampleFunction() def test_defaults(self): # There are a lot types of asserts available # I only demo the assertEqual here. self.assertEqual(self.ef.example, 0, "example is not set to 0") self.assertEqual(self.ef.example2, 1, "example2 is not set to 1") self.assertEqual(self.ef.example3, 2, "example3 is not set to 2") def test_processVariable(self): self.assertEqual(self.ef.processVariable(1), 2, "example error message") def test_setExampleVariables(self): self.ef.setExampleVariables(5) self.assertEqual(self.ef.example, 5, "example is not set to 0") self.assertEqual(self.ef.example2, 6, "example2 is not set to 1") self.assertEqual(self.ef.example3, 7, "example3 is not set to 2") if __name__ == '__main__': unittest.main() # My Output: # ... # ------------------------------------- # Ran 3 tests in 0.000s # # OK
A python example based blog that shows how to accomplish python goals and how to correct python errors.
Showing posts with label inheritance. Show all posts
Showing posts with label inheritance. Show all posts
Saturday, March 27, 2010
Python - unit tests with the unittest module
Tuesday, March 23, 2010
Python - calling super class methods from derived classes
# An inheritance, abstract classes, and # calling super class methods from derived # classes example class AbstractBot(object): def __init__(self): self.size = 0 self.speed = 1 self.pos = [0,0] def applyPos(self, xy): self.pos[0] += xy[0] * self.speed self.pos[1] += xy[1] * self.speed def getCurrentPos(self): return self.pos class WalkingBot(AbstractBot): def applyPos(self, xy): super(WalkingBot, self).applyPos(xy) print "step, step" class DrivingBot(AbstractBot): def applyPos(self, xy): super(DrivingBot, self).applyPos(xy) print "vroom, vroom" class RunningBot(AbstractBot): def __init__(self): super(RunningBot, self).__init__() self.speed = 5 def applyPos(self, xy): super(RunningBot, self).applyPos(xy) print "trot, trot" bot1 = WalkingBot() bot2 = DrivingBot() bot3 = RunningBot() bots = [bot1, bot2, bot3] distance = (1,1) for step in xrange(3): for bot in bots: bot.applyPos(distance) print 'position: ' + str(bot.getCurrentPos())
Labels:
abstract,
inheritance,
object,
python,
super
Thursday, February 25, 2010
Python - a simple polymorphism example
# A quick example of polymorphism at work in python class Food(object): def __init__(self, name, calories): self.name = name self.calories = calories def tastesLike(self): raise NotImplementedException("Subclasses are responsible for creating this method") class HotDog(Food): def tastesLike(self): return "Extremely processed meat" class Hamburger(Food): def tastesLike(self): return "grilled goodness" class ChickenPatty(Food): def tastesLike(self): return "tastes like chicken" dinner = [] dinner.append(HotDog('Beef/Turkey BallPark', 230)) dinner.append(Hamburger('Lowfat Beef Patty', 260)) dinner.append(ChickenPatty('Micky Mouse shaped Chicken Tenders', 170)) # even though each course of the dinner is a differnet type # we can process them all in the same loop for course in dinner: print course.name + " is type " + str(type(course)) print " has " + str(course.calories) + " calories " print " and tastes like " + course.tastesLike() # my output: # #Beef/Turkey BallPark is type <class '__main__.HotDog'> # has 230 calories # and tastes like Extremely processed meat #Lowfat Beef Patty is type <class '__main__.Hamburger'> # has 260 calories # and tastes like grilled goodness #Micky Mouse shaped Chicken Tenders is type <class '__main__.ChickenPatty'> # has 170 calories # and tastes like tastes like chicken
Labels:
inheritance,
object,
polymorphism,
string,
type
Tuesday, September 15, 2009
Python - multiple inheritance
<pre> <span class="comment"># multiple inheritance with python....its easy!span> <span class="comment"># in addition to multiple inheritance you shouldspan>
<span class="comment"># check out the creating static methods for a classspan> <span class="ANY_KEYWORD">classspan> Arm(object): <span class="ANY_KEYWORD">defspan> flipBird(self): <span class="ANY_KEYWORD">printspan> <span class="STRING_LITERAL">"span><span class="STRING_LITERAL">extend middle fingerspan><span class="STRING_LITERAL">"span> <span class="ANY_KEYWORD">classspan> Weapon(object): <span class="ANY_KEYWORD">defspan> fireWeapon(self): <span class="ANY_KEYWORD">printspan> <span class="STRING_LITERAL">"span><span class="STRING_LITERAL">rat tat tatspan><span class="STRING_LITERAL">"span>
<span class="comment"># with your span><span class="comment">powers combined I am Hero Arm (part arm part weapon!)span> <span class="ANY_KEYWORD">classspan> HeroArm(Arm, Weapon): <span class="ANY_KEYWORD">passspan> a = HeroArm() a.fireWeapon() <span class="comment">#output:span> <span class="comment"># rat tat tatspan>
a.flipBird() <span class="comment">#outputspan> <span class="comment"># extend middle fingerspan> pre>
Subscribe to:
Posts (Atom)