January 19, 2021
Hot Topics:

Creating an NUnit Project Template for .NET, Part 1

  • By Paul Kimmel
  • Send Email »
  • More Articles »

Creating the Project Template Folder

Now that we have a prototype that we'd like to put into production, we need to start working on the assembly line. (Sorry, living in Michigan makes it hard not to use mass production metaphors.)

Several project and item templates already exist. The easiest way to create a new template is to start with an existing template. The closest relative to our template is the Visual Basic .NET Class Library template. To stub our project template, navigate to the ClassLibrary folder—which should be located approximately here: C:\Program Files\Microsoft Visual Studio .NET 2003\Vb7\VBWizards\ClassLibrary—and make a copy of this folder. Pasting the copy into the same parent folder—VBWizards—renames the copy TestClassLibrary.

Our new TestClassLibrary contains many of the elements we will need to create the new project item template.

Implementing the Template Class File

The ClassLibrary template contains a stubbed-out class module. To implement the NUnit class module, we need to replace the default class.vb module's code with templatized code from our class library prototype.

Note: Our new TestClassLibrary project folder contains two child folders. The first is scripts and the second is templates. Templates contains a sub-folder indicating the installed language version. English versions will be .\Templates\1033. It is the 1033 folder that contains the templatized class.vb module. The language version folder will vary depending on which language version of .NET you have installed.

To convert the prototype to a template, we can use the original class module as a guide. The vsWizard.dll uses substitution to replace specific tokens in the template file with values provided by VS.NET when we create a new project or add a new element. Listing 2 contains the original .\Templates\1033\class.vb module and Listing 3 contains the new code with replaceable template values for the elements replaced by VS.NET.

Listing 2: The ClassLibrary template class module.

Public Class [!output SAFE_ITEM_NAME]

End Class

Listing 3: The TestClassLibrary template class module.

Imports NUnit.Framework
Imports System.Diagnostics

Friend Class MyListener
  Inherits TraceListener

  Public Overloads Overrides Sub Write(ByVal message As String)
  End Sub

  Public Overloads Overrides Sub WriteLine(ByVal message As String)
  End Sub
End Class

<TestFixture()> _
Public Class [!output SAFE_ITEM_NAME]
  Private Shared listener As MyListener = New MyListener

  <SetUp()> _
  Public Sub Init()
    If (Not Trace.Listeners.Contains(listener)) Then
    End If
  End Sub

  <TearDown()> _
  Public Sub Deinit()
    If (Trace.Listeners.Contains(listener)) Then
    End If
  End Sub

  <Test()> _
  Public Sub Test1()
    Assertion.Assert("Add a boolean test here", False)
  End Sub

  <Test()> _
  Public Sub Test2()
    Assertion.Fail("Intentionally fail if this code is run")
  End Sub

  <Ignore("Not ready yet!")> _
  Public Sub Test3()
    ' Change the attribute from Ignore to Test
    ' when this test is finished
  End Sub

End Class

Notice that the only part we needed to change was the name of the test class. Shown underlined, the class name needs to be a replaceable parameter because VS.NET uses a naming scheme classx.vb, where x is a number guaranteeing that the new file name is unique. Typically, the class module's name is class1.vb in a new project.


In Part 1 of this article, you learned how to combine NUnit and TraceListeners to create great test libraries. In addition, you learned a bit about how VS.NET creates new projects: This is accomplished with stubbed sample files, and you can use the same approach to create project or item templates.

The benefit of using this project template is that new developers can start creating NUnit tests right away and for every test we write we save 50 lines of code and the steps required to import the correct assemblies. It is the accumulative effect of short cuts like these that turn good programmers into hyper-productive programmers.

In Part 2 of this article, I will complete the project template by demonstrating how to modify the supporting script to import the NUnit namespaces, how to define the wizard launching file, add the VSDir file entries, and we will take our new test library, created by the template, for a spin around the block.


Paul Kimmel is the VB Today columnist for codeguru.com and developer.com and has written several books on object-oriented programming, including the recently released Visual Basic .NET Power Coding from Addison-Wesley and the upcoming Excel VBA 2003: Programmer's Reference from Wrox Press. He is the chief architect for Software Conceptions and is available to help design and build your next application.

The Lansing, Michigan area has started a new .NET Users Group. A well-run group, it offers great learning and networking opportunities and occasionally some free pizza and door prizes. Contact me at kimmel@softconcepts.com if you live in mid-Michigan and are interested in participating.

Page 2 of 2

This article was originally published on April 12, 2004

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date