Here are some links from MSDN Magazine on automatic testing code:
Using UIAutomation Bugslayer March 2007
Using PowerShell Test Run December 2007
Tester, a utility for recording mouse clicks and keystrokes, then playing them back & program checking behaviour. Excellent for unmanaged code. Uses windows handles so may not work well for managed code. Bugslayer March 2002.