views:

224

answers:

6

How do you effectively test code that you wrote? I find that it is very hard to tiger test code that I have written for my site because part of me feels that I don't want to find bugs in my code because that shows I'm not perfect. (Even writing that I might not be perfect bother me a little bit.)

I believe in unit testing, but lately I've become a bit of a gunslinger and have been deploying lots of code to production from the hip (which isn't always bad for a new web startup, but often is.)

I've been bitten in the last few weeks by serious bugs that have gotten by my own testing. I have a partner who I push to black box test my own code, but due to my knowledge of the implementation details I really should be the best person to test the weak points of the code using white box testing.

So what methods and tools are useful to help you test code you have written yourself?

+4  A: 

Writing the test before the code helps since I can think (without the bias set by the implementation in the mind) about what the behavior of the code should be in various conditions.

Amit Kumar
+1  A: 

I've started to write unit tests before the code for all my personal projects bigger than 20 lines of code. It helps a lot.

Now I'm always surprised when people say that they hate unittests and TDD.

Eugene Morozov
I hate unit tests and TDD
Joe Philllips
+4  A: 

OK, here goes.

  1. You are not perfect. I am not perfect. Nobody is perfect. So don't worry about exposing errors. The more you find yourself, rather than letting someone else find them for you, the closer to perfect you become.

  2. Get into the habit of writing the tests before the code where you can. Define what the code should do with a meaningful set of tests, then code to get the tests passing. Then refactor - classic TDD.

  3. If there are bugs, then immediately write a test that exposes the bug (i.e. that should pass, but fails because of the bug). Then get the test to pass, and you've fixed the bug.

David M
A: 

First you should note all the possibilities of inputs, Then you have to test with all the possibilities. You can find our bugs and it will clear out lot of bugs. I am using this way only, so far i have no idea about the Tools

sivakumar
+2  A: 

To sum up:

TDD:

  1. Before writing code, write a unit test to take in inputs and check for outputs.
  2. Write your code only to make the unit test pass -- nothing more, nothing less.
  3. Repeat for every method you're working on.

REGRESSION:

  1. Every time you commit a change/feature/fix, run all your unit tests.
  2. For each unit test which fails, go to BUG FIX

BUG FIX:

  1. Every time you find a bug, write a unit test to reveal the bug.
  2. Implement a fix which causes the unit test to pass.

FUNCTIONAL TESTING:

  1. Find a decent functional testing framework (e.g. selenium for web tests)
  2. Create a number of tests which help exercise the end-to-end functionality of your app
  3. Find and fix tests using the REGRESSION and BUG FIX processes.
bedwyr
I would add to this, write the interface and its documentation before all of that...
Varkhan
Good point, though I would hesitate to write _too_much_ documentation prior. Changes happen to the interface, tests are updated accordingly, but keeping documentation updated is often one of the first tasks to slip.
bedwyr
You seem to imply every test failing in REGRESSION leads to a new test case in BUG FIX.
philippe
If a regression test has failed, it _is_ the test case created either in the BUG FIX process or the TDD process.
bedwyr
A: 
  1. If you do TDD, you may write the test code, forget it for one week and then write the productive code. So you will rethink if your approach is the right one.
  2. Avoid embarrassment. If you find bugs in your code and you are able to fix it, no one can accuse you to write bad code.
furtelwart