Hello Fellow Monks,

I have inherited some code at work and have decided to use the project as an excuse to make my foray into TDD. I have made some progress by writing a test, then making sure the code passes the test. After that I move on to the next test. I am impressed at how this process easily catches when I break something unintentionally.

However, I read about people writing all of their tests first and then writing the code to fit the tests after. Is this the preferred method? Is writing tests then writing code a valid way of doing things?

Also, one more thing. I had a method that used time(). But that's not testable because if I call time in the test case, it will be different than when it is called within the method; making a correct comparison impossible. so I moved the time() call outside of the method and passed the result as an argument so I could easily compare my test with the output. Is this kind of change helping me by making my code more testable or is there a better way to write the test without modifying the method. I guess I want to know is, how much of my code should I be changing to accommodate tests?


In reply to Test Driven Development Workflow by actualize

Title:
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, details, 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, summary, 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.