Ask HN: Do you write tests before the implementation?
Test Driven Development is not actually synonymous with Test First Development.  Test First is a method that you can use to do TDD.  It&#x27;s quite a good method, but it&#x27;s not the only one.<p>To answer your question properly, you need to back up a bit.  What is the benefit of TDD?  If you answer is &quot;To have a series of regression tests for my code&quot;, then I think the conclusion you will come to is that Test First is almost never the right way to go.  The reason is that it&#x27;s very, very hard to imagine the tests that you need to have for your black box code when you haven&#x27;t already written it.<p>You might be wondering why on earth you would want to do TDD if not so that you can have a series of regression tests for your code.  Remember that in XP there are <i>two</i> kinds of testing: &quot;unit testing&quot; and &quot;acceptance testing&quot;.  An acceptance test is a test that the code meets your requirements.  In other words, it&#x27;s a black box test regression test.  You are very likely to do acceptance testing after the fact, because it is easier (caveat: if you are doing &quot;outside-in&quot;, usually you will write an acceptance test to get you started, but after you have fleshed in your requirements, you normally go back and write more acceptance tests).<p>If acceptance tests are regression tests, why do we need unit tests.  A common view of &quot;unit tests&quot; is to say that you want to test a &quot;unit&quot; in isolation.  Often you take a class (or the equivalent) and test the interface making sure it works.  Frequently you will fake&#x2F;mock the collaborators.  It makes sense that this is what you should do because of the words &quot;unit&quot; and &quot;test&quot;.<p>However, <i>originally</i> this was not really the case as far as I can tell (I was around at the time, though not directly interacting with the principle XP guys -- mostly trying to replicate what they were doing in my own XP projects.  This all to say that I feel confident about what I&#x27;m saying, but you shouldn&#x27;t take it as gospel).  Really right from the beginning there were a lot of people who disliked both the words &quot;unit&quot; and &quot;test&quot; because it didn&#x27;t match what they were doing.<p>Let&#x27;s start with &quot;test&quot;.  Instead of testing that functionality worked, what you were actually doing is running the code and documenting what it did -- without any regard for whether or not it fit the overall requirements.  One of the reasons for this is that you don&#x27;t want to start with all of the data that you need and then start to write code that produces that data.  Instead you start with a very small piece of that data and write code that produces that data.  Then you modify the data and update the code to match that data.  It is less about &quot;test first&quot; as it is about decomposing the problem into small pieces and observing the results of your development.  It does not matter if you write the test first or second, but it&#x27;s convenient to write the test first because before you can write the code, you need to know what change you want the code to enact.<p>One of the reasons why the term &quot;BDD&quot; was invented was because many people (myself included) thought that the phrase &quot;Test Driven Development&quot; was misleading.  We weren&#x27;t writing tests.  We were demonstrating <i>behaviour</i> of the code.  The &quot;tests&quot; we were writing were not &quot;testing&quot; anything.  They were simply expectations of the behaviour of the code.  You can see this terminology in tools like RSpec.  For people like me, it was incredibly disheartening that the Cucumber-like developers adopted the term BDD and used it to describe something <i>completely different</i>.  Even more disheartening was that they were so successful in getting people to adopt that terminology ;-)<p>Getting back to the term &quot;unit&quot;, it was never meant to refer to isolation of a piece of code.  It was meant to simply describe the code you happened to be working with.  If we wanted to write tests for a class we would have called it &quot;class tests&quot;.  If we wanted to write tests for an API we would have called it &quot;API tests&quot;.  The reason it was called &quot;unit test&quot; (again, as far as I can tell) is because we wanted to indicate that you could be testing at <i>any</i> level of abstraction.  It&#x27;s just intended to be a placeholder name to indicate &quot;the piece of code I&#x27;m interested in&quot;.<p>I think Michael Feathers best described the situation by comparing a unit to a part in a woodworking project.  When you are working on a piece, you don&#x27;t want any of the other pieces to move.  You put a clamp on the other pieces and then you go to work on the piece that you want to develop.  The tests are like an alarm that sounds whenever a piece that is clamped moves.  It&#x27;s not so much that you are &quot;testing&quot; what it should do as you are documenting its behaviour in a situation.  When you touch a different part of the code, you want to be alerted when it ends up moving something that is &quot;clamped&quot; (i.e. something you aren&#x27;t currently working on).  That&#x27;s all.  The &quot;unit&quot; you want to clamp depends a <i>lot</i> on how you want to describe the movement.  It might be a big chunk, or it might be something incredibly tiny.  You decide based on the utility of being alerted when it moves.<p>So having said all that, what is the benefit of TDD?  Not to test the code, but rather to document the behaviour.  I&#x27;ve thought long and hard about what that means in practical terms and I&#x27;ve come to the conclusion that it means <i>exposing state</i>.  In order to document the behaviour, we need to observe it.  We have &quot;tests&quot;, but they are actually more like &quot;probes&quot;.  Instead of &quot;black box&quot; interactions (which are fantastic for <i>acceptance tests</i>) we want to <i>open up</i> our code so that we can inspect the state in various situations.  By doing that we can sound the alarm when the state moves outside of the bounds that are expected.  The reason to do that is so that we can <i>modify code in other places</i> safe in the knowledge that we did not move something on the other end of our project.<p>Anything you do to expose state and to document it in various situations is, in my definition anyway, TDD.  Test First is extremely useful because it allows you to do this in an iterated fashion.  It&#x27;s not so much that you wrote the test first (that&#x27;s irrelevant).  It&#x27;s that you have broken down the task into tiny pieces that are easy to implement and that expose state.  It just happens to be the case that it&#x27;s extremely convenient to write the test first because you have to know what you want before you can write it.  If you are breaking it down in that kind of detail, then you might as well write the test first.  And, let&#x27;s face it, it kind of forces you to break it down into that detail to begin with.  That&#x27;s the whole point of the exercise.<p>There are times when I don&#x27;t do test first and there are times when I don&#x27;t do TDD.  I&#x27;ll delve into both separately.  First, I frequently don&#x27;t do Test First even when I&#x27;m doing TDD if I&#x27;m working with code that has already got a good TDD shape (exposed state with documented expectations).  That&#x27;s because the &quot;test&quot; code and the production code are 2 sides of the same coin.  I can make a change in the production behaviour, witness that it breaks some tests and then update the tests.  I often do this to stress tests my tests.  Have I really documented the behaviours?  If so, changing the behaviour should cause a test to fail.  If it doesn&#x27;t, maybe I need to take a closer look at those tests.<p>Additionally, I don&#x27;t always do TDD.  First, there are classes of problems which don&#x27;t suit a TDD breakdown (insert infamous Sudoku solver failure here -- google it).  Essentially anything that is a system of constraints or anything that represents an infinite serious is just exceptionally difficult to break down in this fashion (woe be unto those who have to do Fizz Buzz using TDD).  You need to use different techniques.<p>Jonathon Blow also recently made an excellent Twitter post about the other main place where you should avoid TDD: when you don&#x27;t know how to solve your problem.  It is often the case that you need to experiment with your code to figure out how to do what you need to do.  You don&#x27;t want to TDD that code necessarily because it can become too entrenched.  Once you figure out what you want to do, you can come back and rewrite it TDD style.  This is the original intent for XP &quot;spikes&quot;... but then some people said, &quot;Hey we should TDD the spikes because then we don&#x27;t need to rewrite the code&quot;... and much hilarity ensued.<p>I hope you found this mountain of text entertaining.  I&#x27;ve spent 20 years or more thinking about this and I feel quite comfortable with my style these days.  Other people will do things differently and will be similarly comfortable.  If my style illuminates some concepts, I will be very happy.