MikeN
MikeN

Reputation: 46287

How do you test code you have written yourself?

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?

Upvotes: 3

Views: 629

Answers (6)

guerda
guerda

Reputation: 24049

  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.

Upvotes: 0

David M
David M

Reputation: 72870

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.

Upvotes: 4

bedwyr
bedwyr

Reputation: 5874

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.

Upvotes: 3

sivakumar
sivakumar

Reputation: 1577

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

Upvotes: 0

Eugene Morozov
Eugene Morozov

Reputation: 15816

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.

Upvotes: 1

amit kumar
amit kumar

Reputation: 21022

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.

Upvotes: 4

Related Questions