<br><br><div class="gmail_quote">On Sat, Feb 12, 2011 at 2:58 PM, Gary Bernhardt <span dir="ltr"><<a href="mailto:gary.bernhardt@gmail.com">gary.bernhardt@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div class="im">On Sat, Feb 12, 2011 at 10:34 AM, Alfredo Deza <<a href="mailto:alfredodeza@gmail.com">alfredodeza@gmail.com</a>> wrote:<br>
> This more of an open question I guess, but why there isn't a project in<br>
> Python that grabs the best features from RSpec<br>
> and implements them?<br>
<br>
</div>As you suggest, it's impossible. Mote and PySpec are just fancy ways<br>
to name your tests: weak implementations of one tiny corner of RSpec.<br>
Mote uses horrible settrace magic; PySpec adds a bunch of<br>
domain-irrelevant noise. Neither even supports arbitrary context<br>
strings. RSpec is more terse, more expressive, removes the noise, and<br>
is an entirely reasonable thing to build in Ruby.<br>
<br>
That last point is important: it's not just that RSpec is possible in<br>
Ruby; it's actually idiomatic. I think that people don't realize just<br>
how easy it is to build its syntax. RSpec lets you say things like:<br>
<br>
describe Integer do<br>
it "adds numbers" do<br>
(1 + 1).should == 2<br>
end<br>
end<br>
<br>
Looks complicated and magical, right? Nope. Here's an implementation<br>
of describe and it:<br>
<br>
def describe(name)<br>
puts name<br>
yield<br>
end<br>
<br>
def it(name)<br>
puts "- #{name}"<br>
yield<br>
end<br>
<br>
This will output the spec output that's become so familiar:<br>
<br>
Integer<br>
- adds numbers<br>
<br>
I spent a couple days of my life trying to get Python to do this when<br>
writing Mote and failed completely.<br>
<br>
The `2.should == 2` syntax is also simple, but here you can get close<br>
in Python. Expecter Gadget [1] lets you say `expect(1 + 1) == 2`. I<br>
still prefer RSpec because the statement reads as "subject,<br>
relationship, expected value".<br>
<br>
There's much more terseness to be had than this, though; the above is<br>
just the most trivial example. You can say things like:<br>
<br>
describe Dog do<br>
it { should have(4).feet }<br>
it { should be_thirsty }<br>
end<br>
<br>
This is totally stock RSpec (and totally self-contained).<br>
`have(4).feet` asserts that `Dog.new.feet.size == 4`; `be_thirsty`<br>
asserts that `Dog.new.thirsty?` returns true.<br>
<br>
The most equivalent Python is:<br>
<br>
class DogTest:<br>
def setup(self):<br>
self.dog = Dog()<br>
<br>
def test_that_it_has_4_feet(self):<br>
assert len(self.dog.feet) == 4<br>
<br>
def test_that_it_is_thirsty(self):<br>
assert self.dog.is_thirsty()<br>
<br>
(Inline the dog construction if you like; that just moves the problem around.)<br>
<br>
Some Python programmers will probably turn their noses up at that<br>
RSpec example because it's fancy (I know I would've). I think that's<br>
unreasonable. It's significantly more terse *and* more readable than<br>
the Python: a combination we don't often get.<br></blockquote><div><br></div><div>It is *very* difficult to talk about things that exist in other languages that are great</div><div>and that they do not exist in Python.</div>
<div><br></div><div>It is also not a case of "if you like it so much just use BLEH". I just think we can</div><div>come up with different and better things and should be talking about that too. </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<br>
"More readable" does depend on learning RSpec, but that's easy because<br>
it's so intuitive. And I bet that you knew exactly what it meant upon<br>
reading it, even if its mapping onto the Dog class wasn't obvious.<br>
<br>
Getting back to the point: you just can't do this stuff in Python.<br>
I've spent a lot of time over the last couple years thinking about<br>
this and eventually I just gave up.</blockquote><div><br></div><div>I think that is why it is important to talk about stuff that is not necessarily tied to</div><div>UnitTesting and the way things are currently done.</div>
<div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"> It's one of the reasons that<br>
almost all of my programming is now done in Ruby.</blockquote><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<br>
[1] <a href="https://github.com/garybernhardt/expecter" target="_blank">https://github.com/garybernhardt/expecter</a><br>
<font color="#888888"><br>
--<br>
Gary<br>
<a href="http://blog.extracheese.org" target="_blank">http://blog.extracheese.org</a><br>
</font></blockquote></div><br>