Unit Testing I am going to state the obvious here. Are you ready? If the developer finds the bug, it is easier and cheaper to fix. Obviously, it's cheaper to fix because the code has not shipped, and there is no install base to upgrade. It's easier to fix than if a non-developer finds the bug and proceeds to explain his or her perception of what stars aligned to cause the erroneous situation.Some developers may argue that simply stepping through the debugger is testing enough. This has little value because it is not reuseable and available for Regression testing. (Not to mention the fact that many issues, such as timing issues and race conditions, are not always reproducible in the debugger.) This may prove the code is OK for now, but what about a year from now? What about running the code on computers other than the developer's machine that do not have development tools and special configurations? Tests must be useable and interpretable by other programmers. In three years, when the original implementer is long gone, the tests should still live on. In fact, even if the module is completely refactored, the tests should still be valuable. Every time someone changes the code to fix a bug, the potential of introducing a new bug or reintroducing a past bug arises. If these tests are run with every build, it guarantees a bug will not appear or, worse, reappear down the road. If the test fails, the build should also fail. Therefore, this testing must be tightly integrated with the build process. The overall problem is that testing is too far removed from development. The goal is to closely integrate testing with development. For this to be achieved, writing a test must be easy, lightweight, and quick. Martin Fowler puts it this way:Whenever you are tempted to type something into a print statement or debugger expression, write it as a test instead.The testing framework should be extensible and flexible and should generate standard output for reporting. Sound like a huge task? Are you ready to say, "We don't have time to test?" Well, extensive testing frameworks and tools already exist for .NET, and the best part is that they are free! |