WEBINAR: On-demand webcast
How to Boost Database Development Productivity on Linux, Docker, and Kubernetes with Microsoft SQL Server 2017 REGISTER >
A couple of years ago, I was writing some code generators using the CodeDOM namespace. To test the code my generator was writing—to make sure it compiled and ran as expected—I wrote a tool that generated a dynamic assembly and a startup method that invoked the generated code. It was a useful tool. I didn't think to integrate it into .NET at the time, but someone at Microsoft has developed something similar and integrated it into .NET. It's called the Object Test Bench.
This article is about this cool new feature, which you will see in Visual Studio 2005. The Object Test Bench permits you to select any single class from the class view or the new diagram view, invoke static methods, create instances, and invoke instance operations. This excellent tool will make a nice adjunct to any development process, especially one that follows on with some good NUnit tests.
Disclaimer: The content in this article is still theoretical. Beta 1 of Visual Studio 2005 already supports this behavior for C#, but it is not functioning in Beta 1 for VB 2005. In addition, Object Test Bench may ultimately be available only in the Professional and Enterprise versions of Visual Studio 2005.
Create Some Code
To demonstrate the Object Test Bench, I have defined some testing code: a wrapper class for the System.Reflection.AssemblyInfo class. The AssemblyInfo class contains metadata that includes the kind of information you might include in a Windows Forms About dialog.
Start by creating the wrapper class. Use the My feature for Visual Basic:
Listing 1: An MyAssemblyInfo for Windows Forms Using the My Feature in VS 2005
Imports System.Reflection Public Class MyAssemblyInfo Public ReadOnly Property Title() As String Get Return My.Application.AssemblyInfo.Title End Get End Property Public ReadOnly Property Copyright() As String Get Return My.Application.AssemblyInfo.LegalCopyright End Get End Property Public ReadOnly Property Version() As String Get Return my.Application.AssemblyInfo.Version.ToString() End Get End Property Public Sub Dump() Debug.WriteLine(Title) Debug.WriteLine(Version) Debug.WriteLine(Copyright) End Sub End Class
Although you don't have to have the wrapper class (you could just use My directly), adding the wrapper insulates your code from .NET code if elements such as My evolve in the future.
Although very simple, the MyAssemblyInfo in the sample is sufficient for working with the Object Test Bench. More importantly, it reflects Microsoft's commitment to make simple tasks simpler to program in VS 2005.
If you aren't sure how to set AssemblyInfo information, refer to the previous VB Today column, "Your Introduction to the My Object in VS 2005". You can still set the AssemblyInfo attributes in the AssemblyInfo.vb file or use the IDE's AssemblyInfo dialog to do so much more easily.
Create Objects from the Class View
You can open the Object Test Bench explicitly by selecting View|Other Windows|Object Test Bench or implicitly in one of the three following ways, depending on you familiarity with any of these features:
- Select View|Class View and create an instance of the MyAssemblyInfo from the Class View's context menu (accessed by clicking with the right mouse button).
- Select a class in the Solution Explorer and click the new View in Diagram button at the top of the Solution Explorer. This step creates a UML class diagram. Context menu options in the Diagram View permit you to create instances of classes.
- Create objects in the Immediate window using lines of code. (If you are still using VB6, this method may be the most recognizable way to create objects.)
Whether you opened the Object Test Bench first or created instances of the MyAssemblyInfo, you should see an instance of the MyAssemblyInfo in the Object Test Bench window. From the context menus for objects in the test bench, you can invoke operations.
Note: To show you what the Object Test Bench looks like after creating an instance of the MyAssemblyInfo, I had to use C# in Beta 1. Visual Basic has a lot of new features, and test bench apparently hasn't been wired up yet. To experiment with Object Test Bench right away, simply create a C# project, inherit from your VB class (such as MyAssemblyInfo), and invoke the objects from the C# wrapper. The C3 project has to be the startup project for this trick to work in Beta 1. Although forcing test bench to work with VB as described does cause Beta 1 to be a tad shaky, Beta 2 is reportedly on its way.
The following steps create an instance of MyAssemblyInfo in the Object Test Bench:
- Open the Class View from the View|Other Windows|Class View menu.
- Expand the project containing the MyAssemblyInfo wrapper and right-click to display the context menu.
- Select Create Instance|MyAssemblyInfo class. An Object Test bench dialog will be displayed.
- Click OK. The Create Instance dialog will be displayed.
- Click OK to use the default object name, which will be something like myAssemblyInf1—a variation of the class name.
After Step 5, the Object Test Bench will contain an instance of the designated class (see Figure 1). The context menu for this icon will contain items for invoking operations. (Your implementation of MyAssemblyInfo contains only properties. Even though properties are implemented in MSIL as methods, the Object Test Bench presently does not support invoking properties.)
Figure 1: An Instance of MyAssemblyInfo in the Object Test Bench
If you want to experiment with invoking operations, invoke the method named Dump, which displays the properties to the Output Window using Debug.WriteLine.
To invoke Dump, create an instance of MyAssemblyInfo in the Object Test Bench. Right-click on the object in the test bench and select Invoke Method|MyAssemblyInfo.Dump. If you have shared methods, you don't need to create an instance of the class. If the method you invoke has a return value, an instance of that return value will be created in the test bench view. If your method needs parameters, the confirmation dialog displayed when you invoke a method will display input fields for adding those parameters.
At present, it doesn't appear that you can debug from the Object Test Bench. I don't know yet whether the test bench will support debugging or not, but I hope it does.
Just the Tip of the Iceberg
Visual Basic and Visual Studio 2005 have many new features. Some like the Object Test Bench are available to all VS 2005 users, and others like the My feature will be available for just VB developers.
The Object Test Bench is just the tip of the iceberg. For more information on all the cool new VS 2005 features, refer to my upcoming book Expert One-on-One Visual Studio 2005 (available Fall 2005).
About the Author
Paul Kimmel is the VB Today columnist for www.codeguru.com and has written several books on object-oriented programming and .NET. Check out his book Visual Basic .NET Power Coding from Addison-Wesley and his upcoming book UML DeMystified from McGraw-Hill/Osborne (Spring 2005). Paul is also the founder and chief architect for Software Conceptions, Inc, founded 1990. He is available to help design and build software worldwide. You may contact him for consulting opportunities or technology questions at firstname.lastname@example.org.
If you are interested in joining, sponsoring a meeting, or posting a job, check out www.glugnet.org, the Web page of the Greater Lansing area Users Group for .NET.
Copyright © 2005 by Paul Kimmel. All Rights Reserved.