Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
My perception is that much of the problems associated with testing is that the act of testing is an explicit and deliberate (and necessary) violation of the DRY principle ("Don't Repeat Yourself"), also known as once-and-only-once.

The rational of DRY is that if you state a fact twice, then when you change it then you need to change it twice. This doubles the inertia inherent in the code, and thus slows down progress (defintion of interia from physics: resistance to change).

The majority of unit test that I've seen (obviously an infinitesimal minory of those that exist) tend to be poorly factored. It's not uncommon to see code along the lines of:

is sqrt(4), 2; is sqrt(16), 4; is sqrt(9), 3;

Which repeats 3 times the the facts that: a function to evaluate square roots exists, that this function is named "sqrt", that it takes a single numeric argument, and that it returns a numeric value! Change any of those facts and the whole set of tests needs to be revised! Moving to a data-driven approach can mitigate this problem somewhat, but never entirely eliminates it. It can't, because the basis of testing is to violate DRY.

In this example, the facts being repeated may seem somewhat trivial, but as a general rule, the duplication acts to magnify the impact of imperfect design decisions: the added inertia may cause people to delay a needed refactoring; and the refactoring may itself need to be staged to avoid a scenario where both the tests and the implementation change simultaneously (e.g. renaming the sqrt function, above, would cause you to change both the tests and the implementation: a purist would tell you to first add the new function to delegate to the old; then change the callers; then the tests; and finally replace the delegation with the original implementation. It's not surprising that some of us give in to the temptation to take shortcuts!

Ideally, the violation of DRY that is the purpose of testing should be realized in a form that is as different as possible from the implementation being tested. Embedding the information as static type systems to be checked by a compiler; or as DBA (design by contract) to be verified by proof tools tend to form stronger whitebox tests than the simple test-by-example that, from my limited perspective, seems all too common.

Opinions my own; statements of fact may be in error.

In reply to Re: Testing methodology, best practices and a pig in a hut. by dpuu
in thread Testing methodology, best practices and a pig in a hut. by BrowserUk

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others making s'mores by the fire in the courtyard of the Monastery: (3)
    As of 2021-01-23 07:23 GMT
    Find Nodes?
      Voting Booth?