Why you should write unit tests

One of the questions I have to answer frequently (mostly question myself) is “why should I write unit tests?”. One of the simplest answers it “They help you discover bugs”.

When you are writing unit test, you should not think about the internals of your methods, but rather about results you would like to get from that method. Kind of black-box testing. If you are not doing TDD, it also can help if you write tests the next day, when implementation details of your methods are not so fresh in your head.

Today I discovered yet another problem in the implementation of RavenDBMembershipProvider: most of the code is not mine there, but I’m writing tests and aim close to 100% coverage. Now in the main class only a few lines of code are not covered. When I started writing last few tests for these lines, I have discovered a problem. Without tests that would possibly be never discovered.. and possibly could lead to a security vulnerability.

WRITE YOUR TESTS!