GoToTest macro for VisualStudio

4

When you are doing Test Driven Development (TDD) you are constantly switching back and forth between production code and test classes.

As it’s good idea to have those files in separate projects, it’s sometimes very hard to find test code for a class and vice-versa.

Some time ago I decided to write a simple Visual Studio macro, that solves this problem.
It’s based on the convention that all your test classes have Test or Tests suffix (e.g. CSVReader.cs and CSVReaderTests.cs)

I’m far from being VB expert, so the code is not perfect (Why does VS use Visual Basic for Macros?):

Imports System
Imports EnvDTE
Imports EnvDTE80
Imports System.Diagnostics
Imports System.IO

Public Module MainModule
    Dim _patterns As String() = {"{0}Test", "{0}Tests"}
    Dim _reversePatterns As String() = {"Tests", "Test"}

    Sub GoToTest()
        If DTE.SelectedItems.Count = 0 Then
            Return
        End If

        Dim fullFileName As String = DTE.ActiveDocument.Name
        Dim fileName As String = Path.GetFileNameWithoutExtension(fullFileName)
        Dim extension As String = Path.GetExtension(fullFileName)

        If IsTestFile(fileName) Then
            For Each reversePattern As String In _reversePatterns
                If fileName.Contains(reversePattern) Then
                    TryOpen(fileName.Replace(reversePattern, "") + extension)
                End If
            Next
        Else
            For Each pattern As String In _patterns
                TryOpen(String.Format(pattern, fileName) + extension)
            Next pattern
        End If
    End Sub

    Function IsTestFile(ByVal fileName As String)
        For Each reversePattern As String In _reversePatterns
            If fileName.Contains(reversePattern) Then Return True
        Next
        Return False
    End Function

    Function TryOpen(ByVal fileName As String) As Boolean
        Dim item As EnvDTE.ProjectItem
        item = FindItem(FileName)
        If Not (item Is Nothing) Then
            OpenItem(item)
            Return True
        End If
        Return False
    End Function

    Function FindItem(ByVal fileName As String) As EnvDTE.ProjectItem
        If String.IsNullOrEmpty(fileName) Then
            Return Nothing
        End If
        Dim item As EnvDTE.ProjectItem = DTE.Solution.FindProjectItem(fileName)
        Return item
    End Function

    Sub OpenItem(ByVal item As EnvDTE.ProjectItem)
        item.Open()
        item.Document.Activate()
    End Sub

End Module

How to install:

Start Visual Studio and go to Tools/Macros/Load Macro Project…:
1

Right click on the Visual Studio toolbar, and select customize:
2

Select ‘Macros’ category and find ‘GoToTest’ Macro:
3

Of course you can change the name of the button.
4

And finally the macro itself:
GotoTestMacro

Tags: , ,

2 Responses to “GoToTest macro for VisualStudio”

  1. Blog » Blog Archive » NavigateToTest VS extension Says:

    […] time ago I blogged about the GoToTest macro I use in my daily […]

  2. Özgür Says:

    Thank you very much. You saved my time 🙂
    That was what I have been looking for.

    Greetings and best regards from Germany

    Özgür

Questions?

Consider using our Q&A forum for asking any questions.