Leo's Technical Blog

Doctests for Ruby

Introduction

user

Leo Soto


python, ruby, testing

Doctests for Ruby

Posted by Leo Soto on .
Featured

python, ruby, testing

Doctests for Ruby

Posted by Leo Soto on .

I'm a big fan of Python doctests. They are easy to write (copy and paste from your interpreter), very clean (no subclassing, no assert*, etc.) and serve very well as documentation.

Compare this test:

def testFactorial(): 
    assertEquals(120, fac(5)) 

With this other:

>>> fac(5)
120

[If you don't understand the later, you should learn a language that provides a REPL. Really.]

So, someone working in Ruby saw the light and implemented it. Nice.

Doctest simplicity is a really poweful concept. The more impressive example is minimock, a mock library built on top of it. Aside from keeping the tests very understandable, the implementation is just 25 lines of python code. How about porting that to Ruby too?