State Street Gang
.NET, straight up

A macro for naming your test methods

January 15, 2008 09:50 by Will

I just read a great article over at mattcalla.com which describes a macro that automatically names a test method by transforming naturally written sentences that describe the test.  Its a great idea; very BDD (which I need to look into one of these days), and reduces the amount of typing you have to do for testing (always good).  I did have some issues with the macro, so I made some changes which I present here.

While the macro works as provided, I did tweak it a little so that the resulting output is formatted the way I like it and the cursor is at the ready position for you to start typing your test magic.  I also changed it from creating an NUnit test to creating a VS test. 

My biggest issue with the macro was that you had to type your test description in before running the macro.  Doing this in the IDE, of course, kicks off stuff like intellisense and error checking, which can wreak havoc with what you're typing.  I changed the macro so that you can write your description in a comment, which keeps the IDE's nose out of your business.  It also allows you to sit down and write a number of different test descriptions all at once, which can be helpful when determining if you have covered everything before starting your test coding.

Anyhow, here's the macro as I envision it:

Imports System

Imports EnvDTE

Imports EnvDTE80

Imports System.Diagnostics

Imports System.Text

 

Public Module BDD

    Sub FormatTestCaseBDDStyle()

        If DTE.ActiveDocument Is Nothing Then Return

 

        Dim selection As TextSelection = _

            CType(DTE.ActiveDocument.Selection(), _

            EnvDTE.TextSelection)

        selection.SelectLine()

 

        If selection.Text.Trim() = "" Then Return

        Dim text As String

        text = selection.Text.Replace("//", "")

        text = text.Trim()

 

        Dim result As New StringBuilder()

        result.AppendLine("[TestMethod()]")

        result.Append("public void ")

        Dim parts As String() = text.Split( _

            New Char() {" "c}, _

            StringSplitOptions.RemoveEmptyEntries)

 

        For Each part As String In parts

            result.Append(part.Substring(0, 1).ToUpper)

            result.Append(part.Substring(1, part.Length - 1))

        Next

        result.AppendLine("()")

        result.AppendLine("{")

        result.AppendLine("}")

        selection.Text = result.ToString()

        DTE.ExecuteCommand("Edit.FormatDocument")

        selection.LineUp()

        selection.NewLine()

        selection.LineUp()

    End Sub

End Module

To add the module to Visual Studio, open up Macro Explorer and add a new module (ugh, VB):

Paste the code in and save it.  Next, you have to assign a keyboard shortcut to the macro.  I chose ctrl-b, ctrl-d as I don't often use the breakpoint explorer thing (ctrl-b by default), and I've already got this macro stuck in my head as BDD (tho its not).  To assign a keyboard shortcut, follow the numbers:

You can set the shortcut by clicking in the textbox and then hitting the shortcut keys as you would during actual use.  You don't have to type in "c" "t" "r" "l" etc.

Thanks to Matt Calla for the great macro!

"I suck at VB script" update:

Here's an updated version.  You need to write your test descriptions, one per line, in comments.  Select all the lines you wish to convert and run this macro.

    Sub FormatTestCaseBDDStyle()

        If DTE.ActiveDocument Is Nothing Then Return

 

        Dim selection As TextSelection = _

            CType(DTE.ActiveDocument.Selection(), _

            EnvDTE.TextSelection)

 

        If selection.Text.Trim() = "" Then Return

 

        Dim text As String

        text = selection.Text.Replace("//", "")

        selection.Text = ""

 

        Dim lines = text.Split(Environment.NewLine.ToCharArray(), _

            StringSplitOptions.RemoveEmptyEntries)

        Dim result As New StringBuilder()

        For Each line As String In lines

            line = line.Trim()

            selection.Text += "[Description(""" + line + """)]"

            selection.NewLine()

            selection.Text += "[TestMethod()]"

            selection.NewLine()

            selection.Text += "public void " + StrConv(line, _

                VbStrConv.ProperCase).Replace(" ", "") + "()"

            selection.NewLine()

            selection.Text += "{"

            selection.NewLine()

            selection.NewLine()

            selection.Text += "}"

            selection.NewLine()

        Next

        selection.LineUp()

        selection.LineUp()

    End Sub


Tags:
Categories: Tips
Actions: E-mail | Permalink | Comments (0) | Comment RSSRSS comment feed

Related posts

Comments are closed