Some of it is in relation to having to work late at night and during weekends. Tests can help you with your definition of done, thus alleviating the need for late night coding sessions.
Effective coding means to use the tools available when they are appropriate.
Sometimes it's about doing something simple:
- Is the bug to complex to find using tests? The flow of the application is wrong...
You have written a test that recreates the problem, but you cant figure out why it doesn't work.
- Maybe you could find it easier if you use a logger to trace the behavior?
- Maybe putting breakpoints and debugging your test will help you find what's wrong.
If you're a beginner. How do you start writing tests?
- If you're writing c# then you want to read "the art of Unit Testing" by Roy Osherove.
- If you're unsure about how to write tests. Try using test lint.
- Search posts done by Martin Fowler, Kent Beck, Robert C. Martin (Uncle Bob), Michael Feathers .
- How do you do when you test your code manually?
If you are structured in your testing of your app you may know some of the domain of automatic testing. You can read about this in the above book by Roy Osherove.
- You set up the state of the program relevant before the method/procedure to test. In Nunit it's called SetUp. You use this attribute to mark the method that sets up the state.
- You fill in a form and click on a button, browse to a page with the right parameters in the url, et.c. When testing this is the actual test. You mark a method that does this by the Test attribute.
- You look at the result from the action. In Nunit you use the static helper class Assert that contains methods to help you write the expected values.
- After you're finished testing, you may need to clean up. In Nunit this is accomplished by marking the a method with the attribute CleanUp.
Also, writing tests is not a reason not to test what you've written manually. It is a way to make sure that you don't have to do as much manually testing as before.