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

Ee9f9502f703ce754aa7feda9eefc579?s=128

Greg Malcolm

July 31, 2010
Tweet

Transcript

  1. @gregmalcolm @gregmalcolm Python Koans Primer

  2. RED GREEN REFACTOR Test Driven Development (TDD) Write a failing

    test Make it pass Clean up!
  3. REDWrite a failing test What do I want to do?

    Sir Lancelot should have a favorite color
  4. 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' ...
  5. REDWrite a failing test

  6. GREENMake it pass What is the simplest thing I can

    do to make it pass?
  7. 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
  8. REFACTOR Clean up class Lancelot(object): def favorite_color(self): return 'Blue' class

    Robin(object): def favorite_color(self): return 'Blue. No yel-- Auuuuuuuugh!'
  9. 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
  10. 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
  11. None
  12. REFACTORClean up Applying TDD principals to Python Koans GREENMake it

    pass REDWrite a failing test See a failing test Make it pass Reflect
  13. 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
  14. http://github.com/gregmalcolm/python_koans