[TIP] running away from RSpec

Gary Bernhardt gary.bernhardt at gmail.com
Sun Feb 13 12:42:09 PST 2011


On Sun, Feb 13, 2011 at 5:33 AM, Michael Foord
<fuzzyman at voidspace.org.uk> wrote:
> You could do the following in Python.
>
> with describe(Dog) as it:
>    it.should.have(4).feet
>    it.should.be_thirsty
>
> Where "it" collects tests which a runner executes when collection completes
> (or in response to __exit__). You could inject "it" using "with hacks" style
> magic too.
>
> The Integer example *could* become:
>
> with describe(int) as it:
>    with it ("adds numbers"):
>        assert 1 + 1 == 2
>
> Not that I'm particularly fond of this or going to implement it myself...

I briefly considered building such a thing. A couple people have built
toy implementations [1].

Many Python programmers, including me, have wasted a lot of energy on
stuff like this in the last few years—trying to cram test syntax
beyond "SUnit except 13 years later" into Python. The results are
usually frustrating. In this case, you marry yourself to a specific
Python implementation (withhacks requires byte code magic). In return
you get hundreds of lines of Python implementation, including manually
reimplementing variable scoping (!), plus inevitable pointy bits
absent from the eight lines of Ruby I showed earlier.

It sure is a pickle!

[1] http://blog.lost-theory.org/2010/3/13/spec-runner-using-withhacks

-- 
Gary
http://blog.extracheese.org



More information about the testing-in-python mailing list