开发者

Visual studio 2010 template for new UnitTest

开发者 https://www.devze.com 2022-12-31 06:48 出处:网络
What ways do I have for creating a unit test template like this? I\'m using visual studio 2010 and Resharper 5.

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()
        {

        }
    }
} 


Yes it's possible. This answer requires Reshaper to be installed.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消