Thursday, 07 December 2006

In my recent article on Immediate Window, I have showed how you can use it to debug your application, but also how to run arbitrary code. Working with Immediate Window has one major drawback: it requires a lot of typing without any kind of support from the IDE. There is however an alternative way to call your methods using Visual Studio: Object Test Bench.

Object Test Bench as the name suggests is used to test objects. It has a simple visual interface that makes it easier to work with than with Immediate Window. For example if we have a class:

public class SomeType
  public string SomeMethod()
    return "return value"

Then we can add an instance of this class to the Object Test Bench, and execute any of its methods using context menu as on the picture below:


If method that is to be invoked requires parameters, Visual Studio will display a special dialog box where you can enter appropriate values - or select them from the list of other objects if necessary. Also, if the method has a return value, the result will be added to the Object Test Bench as a new object on which you can also invoke methods.

To use the Object Test Bench, go to the Class View (ctrl+shift+c), navigate to a class you want to test and from the context menu, select "Create Instance" or "Invoke Static Method" depending on what you are trying to test. You can also access the same functionality through the Class Designer (Class Diagram).

As good as the Object Test Bench may appear to be, there are also some major problems with it that in practice make it useless - at least for me.

The first and most serious problem that disqualifies the bench is the fact that any time you make a change in the code, the whole object structure is deleted from the bench. Just imagine spending couple of minutes setting objects up just to find out that one of the method requires changes. After you make the change, you have to set the whole bench from the beginning. Immediate Window does suffer from the same problem, but it is a lot easier to just run the same command sequence by pressing UP or DOWN arrows. You cannot do this in the Object Test Bench.

The second problem is that the Object Test Bench is only accessible through the Class Diagram - which I don't use and through the Class View - which I also don't use - I only work with Solution Explorer. It may not be big problem, but it requires an additional step to use the feature. Compare this to almost immediate access to Immediate Window and multiply by the number of times you use the feature.

Third problem is that you may not see the Object Test Bench at all! Some times it happens, that when you try to "Create Instance" of an object either through Class Diagram or Class View, there is no such function in the context menu! (BTW: Visual Studio is known to hide its features randomly). In my case the problem was that I had a Web Project selected as a startup project. Changing it to an class library solved the problem. Once again there is no such problem with Immediate Window.

So basically you can use both tools: the Immediate Window and the Object Test Bench. Both have their advantages and disadvantages, but I have found the Immediate Window to be just better, faster and always there and that is why I'm not using the bench any more.

kick it on