Visual Studio Object Test Bench

The Object Test Bench is a pretty cool feature of Visual Studio that I always seem to forget about.  It allows you to test code in classes you develop within Visual Studio without having to actually run an executable.  It’s a quick way to run an informal test on some code.

So, if you have a class named Foo, you can create an instance of this class from the Class View by right-clicking the class name and selecting the Create Instance menu item and then selecting Foo() from the submenu.  If you had multiple constructors, you could have selected the one of interest from the submenu.

OTBClassView

A dialog will display, allowing you to provide a name for the instance being created.  If your constructor had arguments then you would specify values here as well.

image

The Object Test Bench view then appears with a shape representing the instantiated Foo object.  You can right-click this shape and select the menu option for Invoke Method, and select a method from the list (such as Bar).

OTBObjectTestBenchView

Again, a dialog will display, allowing you to provide the values you want to send to the method invocation.  The Bar method takes two integer arguments, and returns the summation as an integer.

image

After providing some values (e.g.,5 and 6), and clicking OK, another dialog appears with the result.  This dialog also provides an option to persist the returned value back into the Object Test Workbench.  This allows you to use this object/value for subsequent method invocation, or serve as input into another method.

image

The Object Test Workbench is certainly not perfect.  For one thing, it does not support generics.  You may notice in the Foo example above there is an overloaded Bar method with nullable int arguments.  This overloaded method doesn’t even appear in the method invocation submenu, due to the generics.  This is true even with Visual Studio 2008.

The Object Test Workbench certainly is not intended to replace a more rigorous unit testing approach, such as that provided with the Team System functionality.  However it is a nice lightweight way to quickly test some code.

Leave a Reply