2

I've been always thinking "Testing? I don't need no freakin' testing! I'm doing without it just fine!"

And then my code became long.

Now I realize why it is important to write tests. I'm always afraid a little change will result in breaking something. I really want to start writing tests. But the code base has become so large that I'm really overwhelmed. I don't know where to start. And much of it I don't even remember why I coded it that way so if I begin to go back and write tests, it will take forever.

Can someone provide advice on how to start writing tests if I already have a large code base?

Vlad
  • 8,038
  • 14
  • 60
  • 92
  • You're in the legacy code camp. Refer to the WELC book, make small safe changes boxed in by tests, stay committed and hope for the best. But it won't come anywhere close to the ease of the scenario where you followed TDD from the starting line. – Gishu Apr 13 '12 at 06:43
  • Duplicate? http://stackoverflow.com/questions/6713645/building-a-test-suite-in-a-large-existing-java-code-base – Mathias Apr 13 '12 at 17:49

2 Answers2

5

First, I would recommend reading the WELC book, which should really come in handy in your situation.

Start the next time you touch the code, the very next time you need to change something in the code, go write a test for it first and then continue to write tests surrounding everything that you have to change, update, fix, or add. That way, over time you will have added tests to all the areas that are changing and will get to a point when writing tests for the rest of the code doesn't seem so overwhelming.

However, I would reiterate that the book I linked to how legacy code would be very helpful as it goes into lots of detail about ways to approach this issue.

Gishu
  • 134,492
  • 47
  • 225
  • 308
Justin Herrick
  • 2,981
  • 17
  • 18
1

Writing tests, especially good takes a lot of time, sometimes it can take much as 50% of the development time/cost, sometimes more. I would first work on writing unit tests for all your functions and attempting to get as much statement coverage as possible. If you don't know how a method works, it's probably poorly documented and/or written, and you'll be better off rewriting it anyway.

This isn't a direct answer to the question, but is a little piece of advice: You should really spend the time now to fix and clean up your code, otherwise it's going to continue to spiral out of control. Code doesn't get better by writing more code, it will only get worse, if you want it to get better you have to go back and rewrite it.

drew212
  • 506
  • 3
  • 16
  • +1 for that line 'Code doesn't get better by writing more code'. I hope you don't mind, I'm gonna borrow it. – Gishu Apr 13 '12 at 06:53
  • Sure, I probably got it from a book I read anyways. Feel free to quote it, with or without attribution =). – drew212 Apr 13 '12 at 07:25