When Do I Write Unit Tests?
Unless you're the one guy who writes bug-free code, you should test your code as you write it. Don't wait until you've developed all your code to begin writing your unit tests. Written tests give you a goal. Write the tests as soon as you realize that you will need them and develop your program to pass the test. Make it a point to try to write tests that will fail. Write multiple unit tests for each method. Test the edge cases. Challenge yourself. You'll have more fun programming. At the same time you'll remain more focused and motivated.
Writing good unit tests
Your tests should
- Have descriptive names that let you know what broke just by reading the name.
- Test one and only one thing
- Run quickly. If your tests take too long to run you won't run them.
- Be resilient to change
- Logically map to the classes you're testing