GoToTest macro for VisualStudio
- GoToTest macro for VisualStudio 2008
- NavigateToTest VS2010 extension
- NavigateToTest VS2012 extension
- NavigateToTest VS2013 extension
- NavigateToTest VS2015 extension
- NavigateToTest VS2017 extension
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…:
Right click on the Visual Studio toolbar, and select customize:
Select ‘Macros’ category and find ‘GoToTest’ Macro:
Of course you can change the name of the button.
And finally the macro itself:
GotoTestMacro
May 3rd, 2014 at 09:29
[…] time ago I blogged about the GoToTest macro I use in my daily […]
March 19th, 2017 at 08:59
Thank you very much. You saved my time 🙂
That was what I have been looking for.
Greetings and best regards from Germany
Özgür