views:

575

answers:

2

I am increasingly annoyed by the unnecessarily verbose template that is used when I create a new unit test in Visual Studio (using the default, included unit testing framework). Instead of

public ImportModelStateTest()
{
 //
 // TODO: Add constructor logic here
 //
}

private TestContext testContextInstance;

/// <summary>
///Gets or sets the test context which provides
///information about and functionality for the current test run.
///</summary>
public TestContext TestContext
{
 get
 {
  return testContextInstance;
 }
 set
 {
  testContextInstance = value;
 }
}

I'd like to have simply

/// <summary>
///Gets or sets the test context which provides
///information about and functionality for the current test run.
///</summary>
public TestContext TestContext { get; set; }

If I need a constructor I'll add one, and the same goes for special getters and setters. I'd also like to remove the sample TestMethod that is included - I still need to rename it, so I can just as well write my own from scratch.

I have looked for the template used for creating these test files, but not been able to find it (I looked mostly around in the neighborhood of the T4 templates used for controllers and views). Where do I change this template?

+1  A: 

All of the item templates for visual studio are run from this directory:

C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\ItemTemplates

Of course c:\Program files may change depending on your installation options. I think the test template is located here:

C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\ItemTemplates\CSharp\1033

As UnitTestWizard.zip.

It looks as though the file within the zip is an xml file which when you open it points at the fact that it is running a dll to generate the test file at runtime probably using code dom.

You could take a look at T4 Templating for this or writing a VS add-in to do this minimum setup it wouldn't take long to write.

Peter
Do you have any links to info on how I do this?
Tomas Lycken
Try looking at visual studio extensibility or vsx. There is a book on visual studio extensibility by wrox publishers which is a good book.
Peter
A: 

The book you want is "Professional Visual Studio Extensibility" by Keyvan Nayyeri. It's an excellent book, and covers the entire breadth of Visual Studio Extensibility (VSX), from macros up to VSpackages.

Be sure to look at the Visual Studio Extensibility Developer Center on MSDN. Also, I recommend you download and install the Visual Studio 2008 SDK. There are some very good examples there, complete with source code.

John Saunders