Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Python Koans Primer

Python Koans Primer

A quick and simple explanation of the concepts behind TDD. Used as a primer for my Python Koans workshop

NOTE: The animations didn't translate too PDF format very well, so expect wierdness

Greg Malcolm

July 31, 2010
Tweet

More Decks by Greg Malcolm

Other Decks in Programming

Transcript

  1. REDWrite a failing test What do I want to do?

    Sir Lancelot should have a favorite color
  2. REDWrite a failing test def test_that_lancelots_favorite_color_is_blue(self): lancelot = Lancelot() self.assertEquals("blue",

    lancelot.favorite_color()); ... Traceback (most recent call last): File "test_lancelot.py", line 11, in test_that_lancelots_favorite_color_is_blue self.assertEquals("blue", lancelot.favorite_color()); AttributeError: 'Lancelot' object has no attribute 'favorite_color' ...
  3. Make it pass class Lancelot(object): pass class Lancelot(object): def favorite_color(self):

    return 'Blue' ... Traceback (most recent call last): File "test_lancelot.py", line 11, in test_that_lancelots_favorite_color_is_blue self.assertEquals("blue", lancelot.favorite_color()); AttributeError: 'Lancelot' object has no attribute 'favorite_color' ... ---------------------------------------------------------------------- Ran 1 test in 0.000s OK GREEN
  4. REFACTOR Clean up class Lancelot(object): def favorite_color(self): return 'Blue' class

    Robin(object): def favorite_color(self): return 'Blue. No yel-- Auuuuuuuugh!'
  5. Clean up def test_that_lancelots_favorite_color_is_blue(self): lancelot = Lancelot() self.assertEquals("blue", lancelot.favorite_color()); class

    Lancelot(object): def favorite_color(self): return 'Blue' def test_that_lancelots_favorite_color_is_blue(self): lancelot = Knight('Lancelot', 'blue') self.assertEquals("blue", lancelot.favorite_color()); class Lancelot(object): def favorite_color(self): return 'Blue' REFACTOR
  6. Clean up def test_that_lancelots_favorite_color_is_blue(self): lancelot = Knight('Lancelot', 'blue') self.assertEquals("blue", lancelot.favorite_color());

    class Knight(object): def __init__(self, name, favorite_color): self._name = name self._favorite_color = favorite_color def favorite_color(self): return self._favorite_color REFACTOR
  7. REFACTORClean up Applying TDD principals to Python Koans GREENMake it

    pass REDWrite a failing test See a failing test Make it pass Reflect
  8. class Sources(object): http://commons.wikimedia.org/wiki/File:Ouroboros-simple.svg http://farm4.static.flickr.com/3213/2979328686_f56f213576_o.png http://ihasahotdog.files.wordpress.com/2009/08/funny-dog-pictures-red-balloons.jpg http://static.howstuffworks.com/gif/snake-anatomy.gif def images(self): def information(self):

    http://github.com/edgecase/ruby_koans/blob/master/README.rdoc http://www.slideshare.net/Siddhi/test-driven-development-with-python http://jamesshore.com/Blog/Red-Green-Refactor.html def python_koans(self): http://bitbucket.org/gregmalcolm/python_koans http://wiki.github.com/gregmalcolm/python_koans/ def other_koans(self): http://github.com/edgecase/ruby_koans http://rubyquiz.com/quiz67.html http://github.com/chicagoruby/MongoDB_Koans http://github.com/CoryFoy/DotNetKoans http://github.com/relevance/functional-koans