Automatically run unit-tests when compiling in Visual Studio 2008

I’ve been getting into test-driven development lately and am absolutely loving it. However, it’s a pain to have to remember to run your tests each time you build. I’d prefer it that whenever I build my solution my tests are automatically run, and the results displayed to me.

In Visual Basic 2008, simply hit Alt-F11, and open your EnvironmentsEvents module. If you don’t have this module, simply create one, and paste the following code into it.

There is an extra feature here as well, cribbed from some smart guy. I’m building a DTF project as part of my solution which takes forever to compile, and locks Visual Studio up while it does. Therefore, I want to start compiling as soon as a project fails so I get a much faster turnaround.

Option Strict Off
Option Explicit Off

Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90

Imports System.Diagnostics

Public Module EnvironmentEvents

#Region "Automatically generated code, do not modify"
'Automatically generated code, do not modify
'Event Sources Begin

      <System.ContextStaticAttribute()> Public WithEvents DTEEvents As EnvDTE.DTEEvents

      <System.ContextStaticAttribute()> Public WithEvents DocumentEvents As EnvDTE.DocumentEvents

      <System.ContextStaticAttribute()> Public WithEvents WindowEvents As EnvDTE.WindowEvents

      <System.ContextStaticAttribute()> Public WithEvents TaskListEvents As EnvDTE.TaskListEvents

      <System.ContextStaticAttribute()> Public WithEvents FindEvents As EnvDTE.FindEvents

      <System.ContextStaticAttribute()> Public WithEvents OutputWindowEvents As EnvDTE.OutputWindowEvents

      <System.ContextStaticAttribute()> Public WithEvents SelectionEvents As EnvDTE.SelectionEvents

      <System.ContextStaticAttribute()> Public WithEvents BuildEvents As EnvDTE.BuildEvents

      <System.ContextStaticAttribute()> Public WithEvents SolutionEvents As EnvDTE.SolutionEvents

      <System.ContextStaticAttribute()> Public WithEvents SolutionItemsEvents As EnvDTE.ProjectItemsEvents

      <System.ContextStaticAttribute()> Public WithEvents MiscFilesEvents As EnvDTE.ProjectItemsEvents

      <System.ContextStaticAttribute()> Public WithEvents DebuggerEvents As EnvDTE.DebuggerEvents

      <System.ContextStaticAttribute()> Public WithEvents ProjectsEvents As EnvDTE.ProjectsEvents

      <System.ContextStaticAttribute()> Public WithEvents TextDocumentKeyPressEvents As EnvDTE80.TextDocumentKeyPressEvents

      <System.ContextStaticAttribute()> Public WithEvents CodeModelEvents As EnvDTE80.CodeModelEvents

      <System.ContextStaticAttribute()> Public WithEvents DebuggerProcessEvents As EnvDTE80.DebuggerProcessEvents

      <System.ContextStaticAttribute()> Public WithEvents DebuggerExpressionEvaluationEvents As EnvDTE80.DebuggerExpressionEvaluationEvents

'Event Sources End

'End of automatically generated code

#End Region
    'The following code will run all tests following a successful solution build.
    '
    'Hooking into build events is discussed at
    'http://visualstudiohacks.com/general/customize-your-project-build-process/
    '
    'The macro code to run the tests was taken by recording a macro
    'and inspecting the generated code.

    'Flag to prevent tests running if a project fails to compile.
    Private runTests As Boolean

    Private Sub BuildEvents_OnBuildBegin(ByVal Scope As EnvDTE.vsBuildScope, ByVal Action As EnvDTE.vsBuildAction) Handles BuildEvents.OnBuildBegin
        runTests = 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 Not Success Then
            runTests = False
            DTE.ExecuteCommand("Build.Cancel")
        End If
    End Sub

    Private Sub BuildEvents_OnBuildDone( _
        ByVal Scope As EnvDTE.vsBuildScope, _
        ByVal Action As EnvDTE.vsBuildAction) Handles BuildEvents.OnBuildDone

         If (Action = vsBuildAction.vsBuildActionBuild Or Action = vsBuildAction.vsBuildActionRebuildAll) And _
            Scope = vsBuildScope.vsBuildScopeSolution And _
            runTests Then

            DTE.ExecuteCommand("Test.RunAllTestsInSolution")

        End If
    End Sub
End Module
Advertisements

One thought on “Automatically run unit-tests when compiling in Visual Studio 2008

  1. Pingback: Automatically run unit-tests when saving in Eclipse | Coding IT

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s