By and large when I want to test a method in my code, I end up creating a dummy console application in my solution and writing a couple lines of code to execute in the Main method. But this involves a not-so-insignificant amount of work. You know – the boring, repetitive and frankly annoying kind of work. Apart from the effort involved, when you are working with source controlled solutions, you can just add a new project without Visual Studio adding it to the source control system. So while I may still fall back on this method out of habit, I have found a much better way – the Object Test Bench tool window.
For the purpose of this article, I created a C# class library with a simple class.
namespace ClassLibrary1
{
public
class
Class1
{
public
int Add(int num1, int num2)
{
return num1 + num2;
}
public
int Subtract(int num1, int num2)
{
return num1 - num2;
}
}
}
The first rule of the Object Test Bench is that the project must be built, so Ctrl+Shift+B. Next, go to the Class View tool window (Ctrl+Shif+C). Then expand the ClassLibrary1 project node and the ClassLibrary1 namespace node. Right-click on the Class1 class node, and choose Create Instance | Class1 (). A window will pop up asking you to name the instance. I usually accept the default, and click OK.
If it isn't visible already, the Object Test Bench tool window will open, and display a diagramming-type image of your class. The only way to interact with the items, it appears, is to use the right-click context menu. The context menu gives you the option to invoke non-static methods, or remove the item from the Object Test Bench. I wouldn't worry too much about remove though. One of the little nuances of the Object Test Bench is that your objects disappear whenever the project gets built, or when running in debug mode and you kill the debugger.
So go ahead and choose the Add method. A window will appear asking for our two parameters. Enter 32 for both and click the OK button. Another window will appear showing the result of the method call with your parameters, in our case 64. You now have a choice of clicking the OK button to close the window, clicking the Retry button to try it with different parameters, or checking the 'Save return value' box to save the result in the Object Test Bench tool window. It may seem rather odd to save the result since it is just going to be wiped out the next time you build the project, but it can be rather useful. Let's say you want to test a couple methods, and use the output of one as the input for another. That's where saving the result comes in.
Click the box to save the result, accept the default name, and now go to invoke the Subtract method. In the parameter dropdown box for either item, you will see the saved result, int321, as an option. Select int321 and the first parameter and enter 16 for the second parameter. The result dialog will show 48 as the result.
Lastly for results, if you want to tweak your result using some of the built-in class methods, you can right-click on the result and see a list of methods and inherited methods for the result object type.
It is also possible to use debugging with the Object Test Bench. By setting a breakpoint in our method and invoking a member, Visual Studio will automatically enter debug mode and break on our breakpoint when the method is invoked.
One of the other interesting things I noted about the Object Test Bench is that it doesn't always throw the expected exception. For instance, the major flaw in my above test code is that if you were to pass int.MaxValue (2,147,483,647) for both parameters of the Add method, an overflow would occur trying to cast the result into an int. Object Test Bench however, returns a result of -2.
One thing you may have noticed back in the Code View tool window, is that there is also an option to invoke a static method. This works pretty much the same as invoking a non-static method, but no class instance is added to the Object Test Bench.
The Object Test Bench provides some pretty interesting, albeit quirky, on-the-fly unit testing capabilities.
Happy test benching!