Visual Studio Templates for NUnit

Written by Jeff Paulson, Sr.
jeff.paulson@mevyap.com
October 11, 2003

Abstract

Microsoft Development Environment, better known as Microsoft Visual Studio, is an extensible software development tool.  Through a feature called Automation and Environment Extensibility, it is a relatively simple task to add your own project and file types to the standard elements shipped by Microsoft.  This article demonstrates the implementation of custom templates by creating a New Project template for NUnit projects and a New File template for NUnit test fixtures.

To save a bit of typing, download the files.

Caveat Lector

A few points need to be made up front.  First, although Visual Studio .NET 2003 has been released, the author is still working with the 2002 version.  Some details provided below may vary slightly bewtween releases.  Adapt.

Second, although the samples provided with this article use C#, the principle for VB.NET is the same.  The only substantial differences are the wizard directory paths and the content of the class template files.  Making the adjustments is a simple exercise.

Finally, a full description of the workings of NUnit is beyond the scope of this article.  For a better understanding of NUnit and the contents of the test fixture template, please see the references below.

Introduction

Visual Studio .NET Automation allows you to control and extend the Visual Studio IDE.  Most extensions require programmatic manipulation of the Automation Model.  The templates described in this article are perhaps the only kind of Visual Studio extension that require no programming.

Microsoft, as well as other authors writing on this topic, throw around the term "wizard" with reckless abandon.  Within a single document, you may find it applied to a DLL with a sequential form interface, a DLL controlled by JavaScripts (no UI), the parameter file used to launch such a DLL, and finally the folders containing templates and scripts.

In an attempt to minimize confusion, this document uses "wizard" only in reference to the DLL files.  Wizard parameter files are either "parameter files" or "VSZ files", and folders containing templates and scripts are "templates".

Overview of Component Templates

Templates for adding projects to a solution and for adding files to a project are implemented using a handful of text files: VSDIR files, which determine what options appear in the Add Project and Add File dialog boxes; VSZ files, which tell Visual Studio which wizard to execute in order to generate files from a template; and a template folder, containing template files and customization scripts.

A VSDIR file is essentially a list of VSZ file references.  Although VSDIR files can be found throughout the Visual Studio installation, the ones we are concerned with at the moment are in VC#\CSharpProjects and in VC#\CSharpProjectItems within the Visual Studio installation.  These files inform the Add Project and Add File dialogs of the available templates.&nsp; Information in VSDIR files is loaded only when Visual Studio is launched, so if any are added or modified, the effects will be realized only after Visual Studio is restarted.

Each template has a VSZ file.  Within this file is the name of a Visual Studio wizard (a DLL file) and a short list of parameters.  Most VSZ files differ only in the value of the WIZARD_NAME parameter.  This parameter seems poorly named, considering it immediately follows an argument called "wizard", whose value is the name of a wizard DLL.  The value of WIZARD_NAME is actually the folder name where the template resides.

The last piece of this convoluted puzzle is the template folder.  Within it are two subfolders, Scripts and Templates.  Each of these has a subfolder called 1033, or the numeric code (LCID) that represents your locale.

Creating the Class and Project Templates

Follow these steps to create a New Project template for NUnit test suites and a New File template for NUnit test fixture classes.&nsp; If you installed Visual Studio to the default folder, directory references in the following text are relative to "C:\Program Files\Microsoft Visual Studio .NET\VC#".

The easiest way to create new project and file template directories is to copy similar entities.  Because NUnit test suites are libraries and NUnit test fixtures are classes, the New DLL template and the New Class template will serve as starting points.

Make a copy of VC#Wizards\CSharpDLLWiz and name it CSharpNUnitProjectWiz.

Make a copy of VC#Wizards\CSharpAddClassWiz and name it CSharpAddNUnitClassWiz.

Within the Templates\1033 folder of both NUnit templates is a file named file1.cs.  Both files represent the same thing — an NUnit test fixture class.  Replace the contents of both with this code:

C# NUnit Test Fixture Template
using System;
using NUnit.Framework;

namespace [!output SAFE_NAMESPACE_NAME]
{
  /// <summary>
  /// Summary description for [!output SAFE_CLASS_NAME].
  /// </summary>
  [TestFixture]
  public class [!output SAFE_CLASS_NAME]
  {
    /// <summary>
    /// Perform generic test initialization. Because this method is
    /// marked with the <code>SetUp</code> attribute, it is called
    /// prior to the invocation of each test.
    /// </summary>
    [SetUp]
    public void Init()
    {
    }

    /// <summary>
    /// Perform generic post-test cleanup. Because this method is
    /// marked with the <code>tearDown</code> attribute, it is called
    /// following the completion of each test method.
    /// </summary>
    [TearDown]
    public void Dispose()
    {
    }

    /// <summary>
    /// Description of Test1.
    /// </summary>
    [Test]
    public void Test1()
    {
    }
  }
}

You now have two new templates — one for adding a new NUnit project to a solution, and one for adding a new NUnit test fixture class to a project.  In the next step, these templates will be made available to Visual Studio.

Creating the VSZ Files

The first step in implementing a new template is to create for it a VSZ file.  Make a copy of CSharpProjects\CSharpDLL.vsz and save it in the same folder as CSharpNUnit.vsz.  Edit this file in Notepad and change the value of "WIZARD_NAME" to the name given to the template folder: CSharpNUnitProjWiz.

Make a copy of CSharpProjectItems\CSharpAddClassWiz.vsz and save it in the same folder as CSharpAddNUnitClassWiz.vsz.  Edit this file in Notepad and change the value of "WIZARD_NAME" to the name given to the template folder: CSharpAddNUnitClassWiz.

Now you have two complete wizards, comprised of a wizard engine (provided by Microsoft), parameter (VSZ) files, and the templates used by the wizard engine to add NUnit elements to your Visual Studio solution.  In the final step, both templates will be made available to the Add Project and Add Class dialogs.

Creating the VSDIR Files

VSDIR files are text files containing one record per template. Fields within each record are delimited by a vertical bar ("|"), and any optional fields that are omitted should have a value of zero.

Create a text file in the CSharpProjects folder and name it CSharpUserDefined.vsdir.  Open CSharpEx.vsdir (in the same folder), copy the line beginning with "CSharpDLL", and paste it into the new file.  Update the record using these values:

CSharpProjects\CSharpUserDefined.vsdir
VSZ fileCSharpNUnit.vsz
Resource file GUID(do not change)
Template nameNUnit Test Project
Sort priority22
DescriptionA regression testing framework for NUnit.
Icon file GUID(do not change)
Icon resource ID(do not change)
Flags0 (zero)
Suggested base nameNUnitTestProject

Create a text file in the CSharpProjectItems\LocalProjectItems folder and name it UserProjectItems.vsdir.  Open LocalProjectItems.vsdir and copy the "CSharpAddClassWiz" line to the new file.  Update the record using these values:

CSharpProjectItems\LocalProjectItems\UserProjectItems.vsdir
VSZ file..\CSharpAddNUnitClassWiz.vsz
Resource file GUID(do not change)
Template nameTest Fixture
Sort priority50
DescriptionA test fixture class for NUnit
Icon file GUID(do not change)
Icon resource ID(do not change)
Flags0 (zero)
Suggested base nameTestFixture.cs

Conclusion

You have now created and implemented two new templates — one for adding an NUnit test suite to a solution, and one for adding an NUnit test fixture class to a project.  To access them, you must restart Visual Studio.

The procedures outlined above are easily adapted to creating any kind of project or file template.  In a future article, we will see how to create your own template groupings, and how to embellish the template with custom scripting.

Additional Reference