VSTS Build - Test Assemblies如何使用NUnit進行測試?

VSTS Build - Test Assemblies如何使用NUnit進行測試?

VSTS Build Test Assemblies是假的

VSTS 的 Build(CI) 建立 build definition 時,通常會選擇範本來調整修改,例如,選擇 Visual Studio 範本:

vsts build visual studio default step

在不修改範本步驟與內容的情況下,我們直接進行 Build 動作,我們可以看到所有動作都亮起綠燈:

vsts defualt step build succeeded

看到綠燈,心情就非常好。但我發現一個可怕的事實...

Build step logs

這是一個含測試的專案(使用 NUnit),想要看一下測試結果:

vsts build - code coverage
vsts build - tests

什麼都看不到?VSTS 不論是 Build 還是 Release 都有非常詳細的 Logs,不看還好,一看:

test assemblies logs

假的!假的!測試根本沒有執行呀。(哭哭)

(1)Path to Custom Test Adapters

xUnit or NUnit with Visual Studio Online Build 找到第一關鍵因素「$(Build.SourcesDirectory)\packages」,測試一段時間後,依專案架構我修改成「$(Build.SourcesDirectory)\{solution_name}\packages。這是指定測試套件的路徑,也是就 NuGet 還原套件儲存的位置。

(2)Test Adapter Path

重新 Build 一樣沒有執行測試,但其中訊息引起我的注意:

Warning: The path 'C:\a\1\s\***\packages' specified in the 'TestAdapterPath' does not contain any test adapters, provide a valid path and try again.

2016-07-18T09:27:49.9335531Z Warning: No test is available in C:\a\1\s\***\***.Tests\bin\Release\***.Tests.dll. Make sure that installed test discoverers & executors, platform & framework version settings are appropriate and try again.

Visual Studio 要能正確執行 NUnit 測試需要額外安裝 NUnit 3 Test Adapter,難到 VSTS 也要裝外掛?找了 VSTS Marketplace 也找不到什麼相關的內容。

後來,花了不少時間終於找到原因,原來 Test Adapter 有 NuGet 版。必須在測試專案裡把 NUnit3TestAdapter 給安裝進來。

nuget - nunit3testadapter

發現此原因後才發現,人家 xUnit or NUnit with Visual Studio Online Build 文章裡已經交代的很清楚:2. Install the matching Visual Studio test adapter/runner NuGet package.,只怪自己走馬看花才浪費了很多時間。

vsts build - tests result

除了看 Tests 的結果,還能把 Code coverage 給下載回來用 Visual Studio 開啟涵蓋率等報告。

小結

一開始的綠燈是"假的",原因是我改用 NUnit,後來的紅燈是"暫時的",雖然花費了不少時間,最後可以看到測試報告與涵蓋率才確認,那是"真的"。

Run Tests using Visual Studio task

VSTest.Console.exe command-line options

沒有留言:

張貼留言

感謝您的留言,如果我的文章你喜歡或對你有幫助,按個「讚」或「分享」它,我會很高興的。