Saturday, October 26, 2013

Test Method Snippet (Visual Studio)

If you practice Test-driven Development (TDD) you know there are many standards and patterns that you can use.  One pattern that myself and others on my team like is the Arrange, Act, and Assert pattern when writing our tests.  This pattern is included in the existing tests for you when you create a new ASP.NET MVC 4 Web Application and then select the option to Create a unit test project to it.  What I noticed is that when I use a snippet to create a new test method, the Arrange, Act, and Assert comments are not there.  This is not a big deal to most as they can remember to Arrange, Act, and Assert, but I would like the comments added to my new test methods when I use a snippet.  Along with not liking that my test methods do not have the comments, I also realized that the name of the new test method of MyTestMethod is dumb and should be changed to our naming standards which came from Roy Osherove.  The naming standard we agreed on is [UnitOfWork_StateUnderTest_ExpectedBehavior] and an explanation of this can be found at Roy's site here.  Making these changes in the snippet is simple and the steps to accomplish it are below.

1.  Open Visual Studio.

2.  Go to Tools -> Code Snippets Manager.


3.  Change the Language to Visual C# and expand the Test folder.

4.  Copy the path located in the Location.

5.  Open Windows Explorer and browse to the location copied in the previous step.

6.  Make a Copy of the testmethod.snippet. 

7.  If you want to still use the original snippet in Visual Studio, rename it by adding a prefix.  If you do not want to use the original snippet, you need to make sure it does not end in .snippet and append something to it or change it.  I prefer to keep it so others that are not aware of my new snippet can still use the old one, so I added a prefix of "Orig_".


8.  Open testmethod.snippet in notepad, Visual Studio or any other program that will allow you to edit text.

9.  Change the text within the Shortcut element from testm to testmm.  The reason for the second 'm' is that there are other methods that may show up from IntelliSense and adding the second m will eliminate them from showing up and the snippet will be invoked by typing testmm followed by a tab.

      old: <Shortcut>testm</Shortcut>
      new: <Shortcut>testmm</Shortcut>

10.  Change the text within the Default element from MyTestMethod to to UnitOfWork_StateUnderTest_ExpectedBehavior.

      old: <Default>MyTestMethod</Default> 
      new: <Default>UnitOfWork_StateUnderTest_ExpectedBehavior</Default>

11.  Add the following three lines of comments to the method just before the text  $end$ within public void $name$()
         
          // Arrange          
          var x = false;
          var y = new y();
          
          // Act          
          x = y.DoStuff();
          
          // Assert          
          Assert.IsTrue(x); 


12.  Save the file.

13.  Close all instances Visual Studio if there are any open.

14.  Open a Unit Test Project within Visual Studio and set your cursor within a test class.

15.  Type testmm followed by a [tab].  As you can see, the snippet will now display the following which has our new method name and the comments to help us create good tests.





Hopefully you learned something from this post and it will help you modify or add more snippets to help you code faster and better.

peace yo!

1 comment:

  1. Perfect. I need to create more of these and also some Class templates. This was just what I was looking for, tired of manually creating method stubs and I do not like all the ones built into VS necessarily. Good stuff here, especially for teams doing Unit Testing (TDD or not, this convention is good).

    ReplyDelete