user261258
user261258

Reputation: 103

how can I automatically run my tests after compilation?

Is there any easy way to automatically run my unit tests after a successful compilation?

Upvotes: 5

Views: 1229

Answers (3)

nrjohnstone
nrjohnstone

Reputation: 818

You can also do this

  1. Create a batch file to run mstest with your desired parameters and use a fixed results file name. Use the START command on the result file to load it into the IDE. Save the batch file in the same path as your solution.

    REM DELETE OLD RESULTS FILE
    del TestResults\auto.trx
    mstest /testcontainer:MyApp\UnitTest\bin\x86\debug\MyUnitTest.dll /category:"Nightly" /resultsfile:TestResults\auto.trx
    start TestResults\auto.trx

  2. Call this batch file in a macro after every build event (I use a seperate thread so I can keep coding away in the IDE)

In your EnvironmentEvents add the following code ' Global flag to indicate if tests should be run
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

        Dim thrd As New System.Threading.Thread(AddressOf threadRunTests)  

        thrd.Start()
    End If
End Sub

Private Sub threadRunTests()

    path = System.IO.Path.GetDirectoryName(DTE.Solution.FullName)

    Environment.CurrentDirectory = path

    DTE.StatusBar.Text = "Running tests..."

    Shell(path & "\RunNightlyTests.bat", AppWinStyle.MinimizedNoFocus, True)

    DTE.StatusBar.Text = "Finished Running tests"
End Sub

Upvotes: 2

Mark Seemann
Mark Seemann

Reputation: 233347

Think about it the other way around: instead of running unit tests each time you compile, simply get into the habit of running the unit tests often.

If you are using MSTest in Visual Studio, you can run all the unit tests as easy as Ctrl+R, A. When you do that, VS automatically compiles the code before running the tests.

Upvotes: 4

Noon Silk
Noon Silk

Reputation: 55152

Yes, but probably you don't want to. This is generally done on a CI server (i.e. your build server) or on an ad-hoc basis.

But if you really want to try it out, in VS you can execute the tests as a "Post Build" task. You can just specify a command-line to run (i.e. nunit) and then direct it to the appropriate lib (there are special variables that will let you link to the just-built project dll).

Upvotes: 2

Related Questions