.NN #5: Object Test bench

Jim Holmes recently posted a blog entry about an interesting use for the immediate window :"Using the Immediate Window in VS to Explore APIs".  He is basically using the immediate window to test out API calls and see how an object works.  Something I stumbled on recently is something built right into Visual Studio 2005 to accomplish this in a different way: The Object Test Bench (if you have VS Professional or higher that is, whereas the immediate window is included in all versions of Visual Studio).

The Object Test Bench is somewhat hidden, but you get started with it via the Class View window, or from the Class Designer.  If you start up Visual Studio and open an existing (or create a new) project you can bring up the Class View window via the menu (View -> Class View) or the hotkey (Ctrl-Shift-C by default).  The class view is just what is sounds like, a tree-view hierarchy of the classes within your project and or solution.  You can however, still get at any of the referenced framework classes via the references nodes. 

Let’s take the example that Jim used, the System.Uri class.  Here the steps to play around with this class using Object Test Bench:

  1. Open the class view window.
  2. Expand the class view to see the References node, then drill down until you find System.Uri.  Note that some System.x objects reside under the mscorlib node since they are located in that assembly rather than System.dll assembly.  System.Uri is actually under the system node.
  3. Select System.Uri and the Class View window will show all the members (based on how the Class View is configured) of System.Uri.  This is handy to look at the API for a class.
  4. Right-click the class in the tree-view and there are two options that start the Object Test Bench: Create Instance and Invoke Static Method.Right-click from class view
  5. Select Create Instance and choose one of the constructors.Creating an Instance
  6. Give the instance a name and enter the parameter values.  There are options in the drop downs for Null/Nothing, etc.  Note that it gives you doc comments for the constructor you selected.

When you click OK you’ll notice that a console window is brought up in the background and a new window appears inside Visual Studio.  The console is where Visual studio has generated code that will create the instance you requested and is hosting the live instance in memory.  If you close the console window you’ll leave the object in an unstable state, so it’s best to leave it alone unless you are done with the Object Test Bench window.

The new “Object Test Bench” window that is now displayed has a representation Object Test Bench visualizerof the object as a shape.  If you hover over the shape you get a visualizer just like when you’re debugging.  If you right-click on the object you can invoke an instance method, which just like the create instance, prompts you for parameters.  Note that you are actually performing that operation, so if you invoke the static Delete method on a System.IO.File class and pass it a real file name it will delete the file.

You can create multiple instances of objects in the test bench at the same time.  Also once the objects are created they can be used as input into other object constructors or method calls.  For example, if you create an instance of a System.DateTime structure it would then automatically appear in the drop down of parameter values when attempting to create an instance or invoke a method on another class that has a parameter that is of the DateTime type.

The Object Test Bench is a quick way of discovering an API, testing your objects (and framework objects) and learning what objects do.  Check out more on what the Object Test Bench can do online.  Also, there is an article on Troubleshooting Object Test Bench if you run into issues.  Of course, I’m not the only one that has blogged about the Object Test Bench, so if you are interested in this feature do a Google search for “Visual Studio Object Test Bench” and you’ll see all sorts of goodies.

There are numerous tools buried in the IDE and the Object Test Bench is just one of them.  Several are only available if you have certain SKUs of Visual Studio, while others are available in all versions.