{"id":73,"date":"2009-09-23T10:32:35","date_gmt":"2009-09-23T08:32:35","guid":{"rendered":"http:\/\/www.limilabs.com\/blog\/?p=73"},"modified":"2018-01-24T18:20:16","modified_gmt":"2018-01-24T16:20:16","slug":"gototest-macro-for-visualstudio","status":"publish","type":"post","link":"https:\/\/www.limilabs.com\/blog\/gototest-macro-for-visualstudio","title":{"rendered":"GoToTest macro for VisualStudio"},"content":{"rendered":"<div class=\"well\">\nExtension is available for other Visual Studio versions:<\/p>\n<ul>\n<li>GoToTest macro for VisualStudio 2008<\/li>\n<li><a href=\"\/blog\/navigate-to-test-visual-studio-extension\">NavigateToTest VS2010 extension<\/a><\/li>\n<li><a href=\"\/blog\/navigate-to-test-vs2012-extension\">NavigateToTest VS2012 extension<\/a><\/li>\n<li><a href=\"\/blog\/navigate-to-test-vs2013-extension\">NavigateToTest VS2013 extension<\/a><\/li>\n<li><a href=\"\/blog\/navigate-to-test-vs2015-extension\">NavigateToTest VS2015 extension<\/a><\/li>\n<li><a href=\"\/blog\/navigate-to-test-vs2017-extension\">NavigateToTest VS2017 extension<\/a><\/li>\n<\/ul>\n<\/div>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/blog\/wp-content\/uploads\/2009\/09\/4.png\" alt=\"4\" title=\"4\" width=\"392\" height=\"77\" class=\"alignnone size-full wp-image-77\" srcset=\"https:\/\/www.limilabs.com\/blog\/wp-content\/uploads\/2009\/09\/4.png 392w, https:\/\/www.limilabs.com\/blog\/wp-content\/uploads\/2009\/09\/4-300x58.png 300w\" sizes=\"(max-width: 392px) 100vw, 392px\" \/><\/p>\n<p>When you are doing Test Driven Development (TDD) you are <strong>constantly switching back and forth<\/strong> between production code and test classes.<\/p>\n<p>As it&#8217;s good idea to have those files in separate projects, it&#8217;s sometimes very hard to find test code for a class and vice-versa.<\/p>\n<p>Some time ago I decided to write a simple <strong>Visual Studio macro<\/strong>, that solves this problem.<br \/>\nIt&#8217;s based on the convention that all your test classes have Test or Tests suffix (e.g. CSVReader.cs and CSVReaderTests.cs)<\/p>\n<p>I&#8217;m far from being VB expert, so the code is not perfect (Why does VS use Visual Basic for Macros?):<\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\nImports System\r\nImports EnvDTE\r\nImports EnvDTE80\r\nImports System.Diagnostics\r\nImports System.IO\r\n\r\nPublic Module MainModule\r\n    Dim _patterns As String() = {&quot;{0}Test&quot;, &quot;{0}Tests&quot;}\r\n    Dim _reversePatterns As String() = {&quot;Tests&quot;, &quot;Test&quot;}\r\n\r\n    Sub GoToTest()\r\n        If DTE.SelectedItems.Count = 0 Then\r\n            Return\r\n        End If\r\n\r\n        Dim fullFileName As String = DTE.ActiveDocument.Name\r\n        Dim fileName As String = Path.GetFileNameWithoutExtension(fullFileName)\r\n        Dim extension As String = Path.GetExtension(fullFileName)\r\n\r\n        If IsTestFile(fileName) Then\r\n            For Each reversePattern As String In _reversePatterns\r\n                If fileName.Contains(reversePattern) Then\r\n                    TryOpen(fileName.Replace(reversePattern, &quot;&quot;) + extension)\r\n                End If\r\n            Next\r\n        Else\r\n            For Each pattern As String In _patterns\r\n                TryOpen(String.Format(pattern, fileName) + extension)\r\n            Next pattern\r\n        End If\r\n    End Sub\r\n\r\n    Function IsTestFile(ByVal fileName As String)\r\n        For Each reversePattern As String In _reversePatterns\r\n            If fileName.Contains(reversePattern) Then Return True\r\n        Next\r\n        Return False\r\n    End Function\r\n\r\n    Function TryOpen(ByVal fileName As String) As Boolean\r\n        Dim item As EnvDTE.ProjectItem\r\n        item = FindItem(FileName)\r\n        If Not (item Is Nothing) Then\r\n            OpenItem(item)\r\n            Return True\r\n        End If\r\n        Return False\r\n    End Function\r\n\r\n    Function FindItem(ByVal fileName As String) As EnvDTE.ProjectItem\r\n        If String.IsNullOrEmpty(fileName) Then\r\n            Return Nothing\r\n        End If\r\n        Dim item As EnvDTE.ProjectItem = DTE.Solution.FindProjectItem(fileName)\r\n        Return item\r\n    End Function\r\n\r\n    Sub OpenItem(ByVal item As EnvDTE.ProjectItem)\r\n        item.Open()\r\n        item.Document.Activate()\r\n    End Sub\r\n\r\nEnd Module\r\n<\/pre>\n<p><strong>How to install:<\/strong><\/p>\n<p>Start Visual Studio and go to Tools\/Macros\/Load Macro Project&#8230;:<br \/>\n<a href=\"\/blog\/wp-content\/uploads\/2009\/09\/1.png\"><img loading=\"lazy\" decoding=\"async\" src=\"\/blog\/wp-content\/uploads\/2009\/09\/1-150x150.png\" alt=\"1\" title=\"1\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-74\" \/><\/a><\/p>\n<p>Right click on the Visual Studio toolbar, and select customize:<br \/>\n<a href=\"\/blog\/wp-content\/uploads\/2009\/09\/2.png\"><img loading=\"lazy\" decoding=\"async\" src=\"\/blog\/wp-content\/uploads\/2009\/09\/2-150x150.png\" alt=\"2\" title=\"2\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-74\" \/><\/a><\/p>\n<p>Select &#8216;Macros&#8217; category and find &#8216;GoToTest&#8217; Macro:<br \/>\n<a href=\"\/blog\/wp-content\/uploads\/2009\/09\/3.png\"><img loading=\"lazy\" decoding=\"async\" src=\"\/blog\/wp-content\/uploads\/2009\/09\/3-150x150.png\" alt=\"3\" title=\"3\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-74\" \/><\/a><\/p>\n<p>Of course you can change the name of the button.<br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"\/blog\/wp-content\/uploads\/2009\/09\/4.png\" alt=\"4\" title=\"4\" width=\"392\" height=\"77\" class=\"alignnone size-full wp-image-77\" srcset=\"https:\/\/www.limilabs.com\/blog\/wp-content\/uploads\/2009\/09\/4.png 392w, https:\/\/www.limilabs.com\/blog\/wp-content\/uploads\/2009\/09\/4-300x58.png 300w\" sizes=\"(max-width: 392px) 100vw, 392px\" \/><\/p>\n<p>And finally the macro itself:<br \/>\n<a href='\/blog\/wp-content\/uploads\/2009\/09\/GotoTestMacro.vsmacros'>GotoTestMacro<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Extension is available for other Visual Studio versions: 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&#8217;s good idea to have [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,8],"tags":[104,56,58],"class_list":["post-73","post","type-post","status-publish","format-standard","hentry","category-programming","category-tools","tag-navigatetotest","tag-unit-testing","tag-vs"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/73"}],"collection":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/comments?post=73"}],"version-history":[{"count":10,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/73\/revisions"}],"predecessor-version":[{"id":5424,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/73\/revisions\/5424"}],"wp:attachment":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/media?parent=73"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/categories?post=73"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/tags?post=73"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}