2010-01-28 22 views

回答

2

反過來想一想:不是每次編譯都要運行單元測試,而是經常養成單元測試的習慣。

如果您在Visual Studio中使用MSTest,則可以像運行所有單元測試一樣簡單,如Ctrl + R,A。當你這樣做時,VS在運行測試之前自動編譯代碼。

+0

更好,謝謝 – user261258 2010-01-28 22:27:55

2

是的,但可能你不想。這通常在CI服務器(即構建服務器)上或臨時基礎上完成。

但是,如果你真的想嘗試一下,你可以在VS中執行測試作爲「後期製作」任務。你可以指定一個命令行來運行(即nunit),然後將其引導到適當的lib(有特殊的變量,可以讓你鏈接到剛建立的項目dll)。

+0

命令行是什麼? – user261258 2010-01-28 22:01:04

+0

可執行文件。右鍵單擊項目的屬性,然後單擊「構建事件」。在後期構建中,您可以編寫如下內容:'「$(ProjectDir).. \ nant \ nant」-buildfile:「$(ProjectDir).. \ local.deploy.task」-D:startingPath =「$(ProjectDir) 「(這是來自我們的一個項目)。 – 2010-01-28 22:03:07

2

你也可以做到這一點

  1. 創建一個批處理文件與您期望的參數運行MSTEST並使用一個固定的結果文件名。使用結果文件上的START命令將其加載到IDE中。將批處理文件保存在與解決方案相同的路徑中。

    REM刪除舊的結果文件
    德爾TestResults \ auto.trx
    MSTEST /testcontainer:MyApp\UnitTest\bin\x86\debug\MyUnitTest.dll /類別: 「每晚」 /resultsfile:TestResults\auto.trx
    開始TestResults \ auto.trx

  2. 調用此批(我使用一個單獨的線程,這樣我可以保持在IDE編碼的距離)文件中的宏每次構建事件之後

在你EnvironmentEvents一個DD下面的代碼 「全球標誌,表示如果測試應該運行
私人runTests由於布爾

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 
相關問題