Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Think of it as a kind of specification. You <em>start</em> by asking yourself, "what should the code I eventually want be able to do?" So, say you want to write a function that adds natural numbers. How would you know if that worked? Well, you know that 2+2=4, so you can write a test (this is basically python but leaves out a lot of details, see the <a href="http://docs.python.org/library/unittest.html" rel="nofollow noreferrer">unittest module docs</a>):</p> <pre><code>def test2plus2(self): assertEquals(addNat(2,2), 4) </code></pre> <p>So here you've defined a specification that says "For natural numbers a and b, compute a+b". <em>Now you know what you need to write the function</em>:</p> <pre><code>def addNat(a,b): return a+b </code></pre> <p>You run it and it passes the test. But then there are some other things you know; since it's for natural numbers only (for whatever reason), you need to add a guard against unnatural numbers:</p> <pre><code>def testUnnatural(self): failUnlessRaises(AssertionErrof, addNat(-1, 2)) </code></pre> <p>Now you've added a specification that says "and throw an AssertionError if the numbers are negative." that tells you the next piece of code:</p> <pre><code>def addNat(a,b): """Version two""" assert((a &gt;= 0) and (b&gt;=0)) return a+b </code></pre> <p>Run this now, and the assertion doesn't fire; success again.</p> <p>The point is that TDD is a way of defining very clear detailed specifications. For something like "addNat" they're not needed, but real code, especially in an agile world, you <em>don't</em> know the answer intuitively. TDD helps you sort that and fid the real requirements,</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload