views:

648

answers:

11

I was wondering when most people wrote their unit tests, if at all. I usually write tests after writing my initial code to make sure it works like its supposed to. I then fix what is broken. I have been pretty successful with this method but have been wondering if maybe switching to writing the test first would be advantageous.

+1  A: 

I dont use unit tests, unfortunately, so my answer is not at all

( I wasnt around for the design of the system and the system is not built with unit tests in mind )

+1  A: 

We try and write them before hand, but I will fully admit, that our environment is chaotic at times, and sometimes this step is initially passed over and written later.

Kevin
A: 

I usually write a list of the unit tests I will be writing before I write the code, and actually code the unit tests after, but that's because I use a program to generate the unit test stubs and then modify them to test the appropriate cases.

Elie
+11  A: 

I really want to write the code first, and often do. But the more I do real TDD, where I refuse to write any code with out a test the more I find I write more testable code and better code.

So, yes, write the test first. It takes willpower and determination, but it really produces better results.

As an added bonus, TDD has really helped me keep focused in an environment with distractions.

csexton
+23  A: 

whenever possible i try to follow a pure TDD approach:

  1. write the unit tests for the feature being developed; this forces me to decide on the public interface(s)
  2. code the feature ASAP (as simple as possible, but not simpler)
  3. correct/refactor/retest
  4. additional tests if required for better coverage, exceptional paths, etc. [rare but worth consideration]
  5. repeat with next feature

it is easy to get excited and start coding the feature first, but this often means that you will not think through all of the public interfaces in advance.

EDIT: note that if you write the code first, it is easy to unintentionally write the test to conform to the code, instead of the other way 'round!

Steven A. Lowe
@Steven -- this is easily the best answer here.
tvanfosson
Kent Beck's book, Test-Driven Development, is about the best overview of TDD out there.
Todd
You're not supposed to write all the unit tests first. Only write enough of a unit test to get a failure, then enough production code to get it to pass, then (if needed) refactor both. It's supposed to be a MUCH tighter loop than you specify here.
Tim Ottinger
@[Tim Ottinger]: i think i disagree with your assertion, but it would be hard to discuss without an example. Arguably you must write all of the unit tests for a feature first in order to fully define the interface of the feature. In practice many features only have 1 test (at least to start)...
Steven A. Lowe
A: 

I tend to use unit tests to test the code as I'm writing it. That way I'm using what I'm writing at the same time, as I use the unit test to test code while I'm writing it. Much like a console app would.

Sometimes I've even written tests without asserts, just debug outputs, just to test that how I use something is working without exceptions.

So my answer is, after a little bit of coding.

Cameron MacFarland
+4  A: 

This has been discussed in several questions:

lillq
Sorry I'm newish here and the results that thing generated didn't seem related
faceless1_14
If everyone stopped posting new questions because a month ago someone already posted something similar, this site would cease to exist. At the least, it would lose an enormous amount of traffic...Don't worry about it faceless
Bloudermilk
A: 

I use Test-Driven Development (TDD) to produce most of my production code, so I write my unit tests first unless I'm working on untestable legacy code (when the bar to write the tests is too high).

Otherwise, I write functional-level acceptance tests that the code shall satisfice.

Writing the unit tests first allow me to know exactly "where I am" : I know that what has been coded so far is operational and can be integrated or sent to the test team ; it's not bug free but it works.

philippe
+3  A: 

I follow a TDD approach, but I'm not as much of a purist as some. Typically, I will rough in the class/method with a stub that simply throws a NotImplementedException. Then I will start writing the test(s). One feature at a time, one test at a time. Frequently, I'll find that I've missed a test -- perhaps when writing other tests or when I find a bug -- then I'll go back and write a test (and the bug fix, if necessary).

Using TDD helps keep your code in line with YAGNI (you aren't gonna need it) as long as you only write tests for features that you need to develop AND only write the simplest code that will satisfy your tests.

tvanfosson
A: 

I'll offer that I'm fairly new to writing unit tests, and that I wish I had written them before I wrote my code. Or, at least had a better understanding of how to write more testable code as well as a better understanding of concepts like dependency injection which seems to be critical to writing testable code.

JasonS
A: 

Normally I write unit tests before the code. They are very beneficial and writing them before the code just makes sense. Dependency injection lends itself well to unit testing. Dependency injection allows for parts of your code to be mocked out so you only test a particular unit i.e. your code is loosely coupled and it’s therefore easy to swap out for a different (in this case mocked) implementation.

Peanut