[LRUG] TDD (was: Ruby Contracting)

Vahagn Hayrapetyan vahagnh at gmail.com
Thu Feb 19 05:02:55 PST 2009


>
> Tests are code, and are so difficult to write or maintain as
> development code.


There are certain strategies and programming mannerisms to avoid this
problem exactly, more commonly known as "testing patterns". One of them is
"explicit testing" - making your test code as simple, atomic, and
transparent as possible. Code line-by-line. Replace for-loops with
line-by-line statements, etc, generally replace complexity with simplicity.

Intuitively, the test code shouldn't be as complicated as production code -
which means every tactic one has developed to write compact and effective
code (in production mode) has to be reversed for testing code.

An example: I'm still fairly new to Ruby so at first I didn't like:

    login_as(:non_admin)
    assert_no_difference('Gym.count') do
      delete :destroy, :id => gyms(:crossfit_san_diego).id
    end

(The Ruby "do" still seems a bit quirky to me.) So I replaced the above, *to
be completely transparent*, with this:

    login_as(:non_admin)
    gym_count_before_delete = Gym.count
    delete :destroy, :id => gyms(:crossfit_san_diego).id
    assert_equal gym_count_before_delete, Gym.count

Just an example, but I hope it goes a long way to show that really the power
is in the developer's hands to write something that is both clean, useful,
and comprehensive.

Having said this, I personally do think writing testing code alongside
production code is a huge boost in overview, productivity, and confidence.
It's less about restricting code than about rhythm and focus. And if it is
about restricting code, all the better - I like restricting my code to
something that *works*.

My 50 cents!

/ Vahagn

On Thu, Feb 19, 2009 at 11:51 AM, Francisco Trindade (Frank) <
frank.trindade at gmail.com> wrote:

> Tests are code, and are so difficult to write or maintain as
> development code. If you want to write tests (and you should, IMO),
> you have to be prepared to spend time writing it carefully, spend time
> refactoring it, spend time erasing tests that are no long necessary,
> etc...
>
> Not mentioning the benefits for design that were already mentioned
> before in this thread, I wouldn't like to get near of maintaining or
> modifying non-tested ruby code. Regression tests give you the
> confidence to change and know that everything is still working ,
> specially in a dynamic language, and that is impossible to do using
> manual testing.
>
> Just my two cents.
>
> Francisco Trindade
> Software Consultant
> ThoughtWorks UK
>
> http://franktrindade.com
>
> On Thu, Feb 19, 2009 at 10:42 AM, Chris Parsons
> <chris at edendevelopment.co.uk> wrote:
> > On 19 Feb 2009, at 10:22, Tom Stuart wrote:
> >
> >> Without taking sides, what surprises me is how few people say anything
> >> about just how hard it is to write useful, robust, intelligible,
> non-brittle
> >> tests (especially for e.g. Rails applications) and how much time it
> takes to
> >> create and maintain them. Common wisdom is that this investment of time
> and
> >> effort more than pays off in quality and stability but, well, is that
> >> appealing idea necessarily always true? Is there really nobody in the
> world
> >> who actually wastes time writing tests?
> >
> > I've wasted many hours writing tests for features for which the
> investment
> > just wasn't justified, and kicked myself for not writing tests for things
> > that really needed them. It's all part of the learning curve. Test where
> the
> > risk is[1], and read blogs by people like Jay Fields and Pat Maddox -
> they
> > have some very interesting discussion on testing techniques.
> >
> > I think the biggest mistake people make is thinking that starting to
> write
> > tests is going to yield a huge instant payoff with no effort. There is a
> > small instant payoff, a lot more learning, and a real danger of
> > overconfidence.
> >
> > We need to write about testing on our blog more. It's not easy at all,
> but
> > if you're disciplined about it does work for in your favour[2].
> >
> > Oh, and there are no sides here ... this is positive open-minded debate,
> > right? :)
> >
> > Chris
> >
> > [1] Someone famous said this; no idea who.
> > [2] Well, not when we're doing stuff for fun which doesn't matter much
> > (http://ykyat.com :)
> >
> > --
> > Chris Parsons
> > Managing Director
> > Eden Development (UK) Ltd
> >
> > chris at edendevelopment.co.uk
> > www.edendevelopment.co.uk
> >
> > 0845 0097 094
> > po box 425, winchester, so23 0wy, uk
> > Map: http://pininthemap.com/edendevelopment
> >
> >
> > Tried Pin in the Map? Mark a map location, add text, send to friends.
> > http://pininthemap.com
> >
> > This electronic message and all contents contain information from the
> sender
> > which may be privileged, confidential or otherwise protected from
> > disclosure. The information is intended to be for the addressee(s) only.
> If
> > you are not an addressee, any copying, disclosure, distribution or use of
> > the contents of this message is prohibited. If you have received this
> > electronic message in error, please notify the sender by reply e-mail and
> > destroy the original message and all copies. The sender does not accept
> > liability for any errors or omissions.
> > _______________________________________________
> > Chat mailing list
> > Chat at lists.lrug.org
> > http://lists.lrug.org/listinfo.cgi/chat-lrug.org
> >
>
>
>
> --
> --
> Francisco Trindade
> _______________________________________________
> Chat mailing list
> Chat at lists.lrug.org
> http://lists.lrug.org/listinfo.cgi/chat-lrug.org
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lrug.org/pipermail/chat-lrug.org/attachments/20090219/977b13d6/attachment-0003.html>


More information about the Chat mailing list