Because writing class libraries is a big part of any developer's job, it follows that testing class libraries is a big part of your job, too. Testing an application, whether it's a Windows application or a Web application, is relatively straightforwardyou run it, and experiment with various paths through the application in terms of which buttons you click, or which fields you fill out or leave blank. But how do you test a class library, or a service, or even a single class that encapsulates an important business process or set of rules?
Console applications are perfect for this kind of testing. They are quick to write, and you don't need a fancy user interface. In fact, it's easy to capture the output of a console application by redirecting it to a file. This enables you to save the results of test after test and compare them. Windows programmers aren't always familiar with the techniques for interacting with users without using controls, but they're quick to learn and definitely useful.
A test harness for working with a single class is the simplest to write. Typically it should perform the following steps:
It's a good idea to have lots of output statements sprinkled throughout this code, so that output might look something like this:
Creating account1, default constructor Account # 123, BalanceCreating account1, default constructor Account # 123, Balance $0.00 Creating account2, passing 50 Account #124, Balance $50.00 Deposit 100 to account1 Account #124, Balance $100.00.00 Creating account2, passing 50 Account #124, Balance .00 Deposit 100 to account1 Account #124, Balance 0.00
This sort of output demonstrates which parts of your code are working and serves as an excellent record of the development process. Generally, all of the values are hard-coded so that you can run the whole thing without having to interact with it, but some people write test harnesses that prompt for values or even for which methods to exercise.
When your test harness is testing a component or a layer, your tests will typically interact either with a single bridging class that provides access to the functionality of the component or layer, or with each of the classes in the component or layer. This can make the harness quite long, but a systematic test of all your functionality is well worth doing, so take the time.