Friday, January 27, 2012

Integrate Resharper Unit Test Runner after a successful build

Whilst trying to use TDD properly, I noticed that I'd have to build my solution and then run my unit tests. Now, most of the unit test runners will build your project at the same time, but it still was a little less convenient than I'd like.

I scraped together a few snippets from teh interwebs and came up with the following to be added to the VS Macros: Pop open the Macros IDE (Tools - Macros - Macros IDE) and in the Project Explorer choose the Environment Events Module. Then Add the following code:

Private SolutionBuildSuccess As Boolean

Private Sub BuildEvents_OnBuildBegin( _
   ByVal Scope As vsBuildScope, _
   ByVal Action As vsBuildAction _
) Handles BuildEvents.OnBuildBegin
  SolutionBuildSuccess = True
End Sub

Private Sub BuildEvents_OnBuildProjConfigDone( _
  ByVal Project As String, _
  ByVal ProjectConfig As String, _
  ByVal Platform As String, _
  ByVal SolutionConfig As String, _
  ByVal Success As Boolean _
) Handles BuildEvents.OnBuildProjConfigDone
  If Success = False Then
    SolutionBuildSuccess = False
  End If
End Sub

Private Sub BuildEvents_OnBuildDone( _
  ByVal Scope As EnvDTE.vsBuildScope, _
  ByVal Action As EnvDTE.vsBuildAction _
) Handles BuildEvents.OnBuildDone
  If SolutionBuildSuccess Then
    Dim win As Window
    win = DTE.ActiveWindow
  End If
End Sub

Going through each of these:
SolutionBuildSuccess: This is just a boolean that will be true if every project builds.  No sense in even running tests if you can't even compile

BuildEvent_OnBuildBegin: This just resets the SolutionBuildSuccess at the begin of each build

BuildEvents_OnProjConfigDone: So, there are 2 events when a build finishes.  One fires when the complete build process is done (OnBuildDone).  This one, however, does not carry the result of the project build(s).  So, there is OnProjConfigDone which fires for each project that builds and has the result.  This will just set the SolutionBuildSuccess to false if any of the projects fail to build.

BuildEvents_OnBuildDone: This one fires when the build is all done.  So, I need to check to see if all the projects built successfully.  Then I save the currently active window.  Finally we run the tests and make that previously saved active window active again.  The reason for that is that after I'm done building and running the tests, generally I'm ready to code more.  So, instead of remembering that I have to smash ctrl-tab (I think) to jump back, I took the lazy approach and put it into the step.

Now, I remember back in the old days when F6 was the command to build stuff.  I'm not sure what one of my tomfooleries changed it, but it did.  So, I re-assigned it back to being my keyboard shortcut for build.

Any Comments or suggestions will probably be ignored, but feel free to leave them anyway.. 

No comments:

Post a Comment