views:

102

answers:

1

What ways do I have for creating a unit test template like this? I'm using visual studio 2010 and Resharper 5.

using NUnit.Framework;

namespace SolutionName.Core
{
    [TestFixture]
    public class ClassNameTests
    {
        [Test]
        public void test()
        {

        }
    }
} 
+1  A: 

Yes it's possible.

First, go to the Menu and choose Resharper => Live Templates. There switch to the File-Templates tab. Add a new template there.

Give the template a description like 'UnitTest' Give it a default-name like 'Test.cs' and choose 'File name must be a valid identifier' Change the availability to 'in C# project ...'

Copy and paste your template into the text-field. Then replace the class-name with a variable. Variables are surrounded by $-characters. The $END$ marks where the cursor is when your done. Like this:

using NUnit.Framework;

namespace SolutionName.Core
{
    [TestFixture]
    public class $ClassNameTests$
    {
        [Test]
        public void test()
        { 
            $END$
        }
    }
} 

Right next to the text-field is a list of the variables (you might need to extend is, its sometimes really small). There the ClassNameTests-variable appears. Choose the macro 'Current file name without extension'. Now the template is done, so store it

Go to your solution, right-click there and choose Add -> New From Template -> More... . There select your template. Make sure that you check the 'Add to quicklist' option and confirm. Now you can enter the class-name for the test and Resharper creates the file for you.

After that the template will be under Add -> New From Template -> Your Template Name

You can probably improve my solution with additional variables & macros. Play with it. Add variables to your template by surrounding a name with $-chars. Then choose a macro for the variable on the list next the the template-text.

Gamlor

related questions