Saturday, August 29, 2009

Reducing the Friction of the Resharper Test Runner

I've seen many a demonstration where the presenter is using a lightweight test runner like TestDriven.net which allows for the execution of units tests without a lot of GUI fanfare.  A few months ago I was having trouble with the Resharper test runner in Visual Studio (for some reason it stopped building my assembly before executing the tests) and so I wrote a macro that allows you to run tests from the context of the current cursor location.  I’ve mapped this to my F12 key for quick execution.

Here is the Visual Studio macro that I wrote.  It uses Resharper's unit test context run feature.  Simply position the cursor inside a namespace, class, or test, and when you invoke this macro it will build the assembly in context, and then proceed to run all the tests found in the current cursor context.

    Private WithEvents testLaunchBuildEvents As BuildEvents
    Private OverallBuildSuccess As Boolean
    Private PayAttention As Boolean
    Sub BuildAndTestCodeContext()
        If (testLaunchBuildEvents Is Nothing) Then
            testLaunchBuildEvents = BuildEvents
        End If
        OverallBuildSuccess = True
        PayAttention = True
        DTE.ExecuteCommand("Build.BuildSelection")
    End Sub
    Sub SolutionBuildDoneForUnitTestContextRun(ByVal Scope As EnvDTE.vsBuildScope, ByVal Action As EnvDTE.vsBuildAction) Handles testLaunchBuildEvents.OnBuildDone
        If Not PayAttention Then
            Exit Sub
        End If
        PayAttention = False
        If OverallBuildSuccess Then
            DTE.ExecuteCommand("ReSharper.ReSharper_UnitTest_ContextRun")
        Else
            Beep()
        End If
    End Sub
    Sub SingleProjectBuildDoneForUnitTestContextRun( _
            ByVal Project As String, _
            ByVal ProjectConfig As String, _
            ByVal Platform As String, ByVal SolutionConfig As String, _
            ByVal Success As Boolean) Handles testLaunchBuildEvents.OnBuildProjConfigDone
        OverallBuildSuccess = OverallBuildSuccess And Success
    End Sub

0 comments: