Sunday, February 01, 2015

Adding Template Classes to Visual Studio 2013's "Add New Item" Screen (NUnit Test Class)

When using Visual Studio,  if you follow Test Driven Development principles, then you're probably used to repeatedly following these steps when adding a new test class (using NUnit as an example):

  1. Right click on Solution Explorer
  2. Click Add  and then New Item
  3. Selecting Class from the list of items
  4. Entering your test class name
  5. Typing in or pasting similar looking lines of code:

using System;
using NUnit.Framework;
 
namespace MyProject
{
    [TestFixture]
    public class MyTestClass
    {
 
        [Test]
        public void Test()
        {
        }
 
    }
}



Admittedly it's not completely onerous, but it can be a bit of a chore.  Thankfully it's very easy to add your own templates to the Add New Item dialog that Visual Studio displays:





Using MSDN's How to: Create Item Templates as guideline, I'm going to create two Item Templates - a standard NUnit test class and one that addtionally includes Setup and Teardown methods.

Getting Started 


1. Create a new Visual Studio project (it doesn't matter which type as we won't be compiling anything). I've called my project NUnitTemplate.

2. Add a reference to NUnit or run Install-Package NUnit into Package Manager console.

3. Add a new class, NUnitTestClass.cs

4. Enter the following 'code':


using System;
using NUnit.Framework;
 
namespace $rootnamespace$
{
 [TestFixture]
 public class $safeitemname$
 {
 
  [Test]
  public void Test()
  {
  }
 
 }
}
This 'class' looks familiar, but it includes a number of template patterns which Visual Studio replaces when it creates the class.  More examples of standard template pattern items.

5. From the File menu choose Export Template.  This starts a wizard that automates the steps required to create a zip file containing your template file, icon, preview image and description for Visual Studio.  This file is placed into a specific folder which VS reads at start-up.

You'll be shown the first page of the Export Template Wizard:


Choose Item Template as we're creating a new Item, rather than a Project template.  You should see only one project listed if you created a single project - NUnitTemplate in my case.  Click Next.

6. On the next Select Item screen you can choose which template item to export - unfortunately you can't choose multiple items to export, which is quite annoying if you've got a few related template items like I have in this demo:


Select the item template that you've just added (NUnitTestClass.cs) and click Next.

7. The Item Reference screen allows you to specify which references needed to be added to the destination project.  System is generally needed, but also you'll need NUnit.Framework:


If the destination project doesn't already have a reference to NUnit.framework or Visual Studio cannot locate the registered .DLL then you'll be shown the following warning when you add the item from this template:


8. The last step of the Wizard is to enter some details about this template item:

Template Name is used to create the destination Zip file.
Template Description is shown in right hand pane.
Icon image is shown in the scrollable list of items (I'm pointing at the NUnit .ICO file)
Preview Image is shown in the right hand pane. You need to ensure this is no more 200 pixels wide otherwise VS will compress it to fit into the screen.

9.  Click Finish and the items are copied into a ZIP file using the Template name and placed into a folder within Documents\Visual Studio 2013\My Exported Templates.  In my case that's:

 C:\Users\jason_000\Documents\Visual Studio 2013\My Exported Templates

Tick Automatically import the template into Visual Studio  and VS will copy the file into another folder where VS looks for user defined templates, for me that's:

C:\Users\jason_000\Documents\Visual Studio 2013\Templates\ItemTemplates

That's all the automated steps done.

10.  There's one extra convenience step that VS doesn't do for you - you'll need to alter the DefaultName element from NUnitTestClass to something like <DefaultName>Test.cs</DefaultName>

Jump to your Documents\Visual Studio 2013\Templates\ItemTemplates folder.  Open the NUnitTestClass.zip file:


You'll see a file titled MyTemplate.vstemplate - this tells VS how to show/handle the selected item type. Open that in Notepad (or VS):



Change the DefaultName attribute and save the ZIP file (remembering to copy the Zip file to your My Exported Templates if you're using that folder as the original source).


I've repeated the above steps to additionally create a standard test class which includes Setup and Teardown methods, using this template class:

using System;
using NUnit.Framework;
 
namespace $rootnamespace$
{
 [TestFixture]
 public class $safeitemname$
 {
 
  [SetUp]
  public void BeforeEachTest()
  {
  }
 
  [TearDown]
  public void AfterEachTest()
  {
  }
 
  [Test]
  public void Test()
  {
  }
 
 }
}

If you feel like following these steps, the final .ZIP files can be downloaded from my site and saved to  your Documents\Visual Studio 2013\Templates\ItemTemplates folder.


5 comments:

  1. Great post. Keep sharing such a topic.

    php training in chennai

    ReplyDelete
  2. I really appreciate information shared above. It’s of great help. If someone want to learn Online (Virtual) instructor lead live training in Nunit, kindly contact us http://www.maxmunus.com/contact
    MaxMunus Offer World Class Virtual Instructor led training Nunit . We have industry expert trainer. We provide Training Material and Software Support. MaxMunus has successfully conducted 100000+ trainings in India, USA, UK, Australlia, Switzerland, Qatar, Saudi Arabia, Bangladesh, Bahrain and UAE etc.
    For Demo Contact us:
    Name : Arunkumar U
    Email : arun@maxmunus.com
    Skype id: training_maxmunus
    Contact No.-+91-9738507310
    Company Website –http://www.maxmunus.com



    ReplyDelete
  3. thanks for your article the information is very nice it's very useful thanks for it.web design company in velachery

    ReplyDelete
  4. Anonymous2:03 PM

    Among the strategies that should show up as choices for players from most countries are Visa, MasterCard, Maestro, MuchBetter, PaysafeCard, Skrill, Neteller, and ecoPayz. It’s more than probably that another native choices will also be obtainable, and a few players will even be able to|be succesful of|have the ability to} use PayPal. You’ll get a preview of a number of} totally different video games whenever you enter the live casino, but 바카라사이트 in actuality, the variety of tables on provide is far bigger. In the preview, you'll be able to|you probably can} merely select which sort of table sport you need to play, but by simply clicking on considered one of these, you'll be able to|you probably can} enter the video games lobby and see the whole choice. One of the roulette tables additionally be|can be} streamed immediately from the land-based casino Dragonara in Malta. This allows you to play at a real-life casino by way of the JackpotCity Live Casino.

    ReplyDelete