最近在替既有專案補上單元測試,除了在 SonarQube 可以看到測試代碼覆蓋率之外,這幾天同事推薦一套可以在 Visual Studio 執行的程式碼覆蓋率分析工具 AxoCover,可運行在 .NET 環境且開源免費,簡單玩了一下覺得十分不錯推薦給大家,今天就來分享 AxoCover 在 Visual Studio 2017 的安裝及基本操作說明。
安裝 AxoCover
Step 1 : 選擇 Tools > Extension and UpdatesStep 2 : 搜尋 AxoCover > 點擊 AxoCover > 安裝
發現 AxoCover 已安裝完成
在安裝完畢之後,開啟之前上課的測試專案 AxoCover 是如何進行分析跟介紹,預設是不會開啟 AxoCover 視窗,要到 Visual Studio > Tools > AxoCover 開啟,開啟後位置會在右邊視窗看到其 Logo
在開啟後會去讀取該專案是否有 UnitTest 單元測試項目
如果發現該專案沒有代碼時會跳出以下畫面,這畫面也會列出支援的 Framework,如 MSTest、NUnit、xUnit
接著會列出目前專案中所有的單元測試
TEST
呈現目前既有的單元測試代碼,以及 UnitTest 相關的一些數字資訊,例如成功/失敗的測試案例數量,包含多少個類別、涵蓋專案中 Namespace 數字等資訊
RUN
根據所選擇的區域執行單元測試,執行時 output 視窗畫面如下
Test execution started. ___ / \ | | \ / / \ / \ ___/ \___ / \ | _______ | \___/ \___/ AxoCover Test Runner Console (c) Peter Major 2017 > Executing tests... | Runner version is 1.1.389.0. | We are on x86 platform. | File redirection is enabled for the following files: | c:\users\marcustung\appdata\local\microsoft\visualstudio\15.0_3d91008b\extensions\wfz4mp4u.i0u\MSTestAdapter\Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.dll | c:\users\marcustung\appdata\local\microsoft\visualstudio\15.0_3d91008b\extensions\wfz4mp4u.i0u\MSTestAdapter\Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.dll | c:\users\marcustung\appdata\local\microsoft\visualstudio\15.0_3d91008b\extensions\wfz4mp4u.i0u\MSTestAdapter\Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.Interface.dll | c:\users\marcustung\appdata\local\microsoft\visualstudio\15.0_3d91008b\extensions\wfz4mp4u.i0u\MSTestAdapter\Microsoft.VisualStudio.TestPlatform.TestFramework.dll | c:\users\marcustung\appdata\local\microsoft\visualstudio\15.0_3d91008b\extensions\wfz4mp4u.i0u\MSTestAdapter\Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions.dll | Setting up file redirection hooks... | File redirection hooks are enabled. >> Loading assembly from c:\users\marcustung\appdata\local\microsoft\visualstudio\15.0_3d91008b\extensions\wfz4mp4u.i0u\MSTestAdapter\Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.dll... || Loaded executor: Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.MSTestExecutor << Assembly loaded. >> Running executor: Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.MSTestExecutor... || Passed AssertionSamples.AssertExceptionSample.Divide_positive Failed AssertionSamples.AssertExceptionSample.Divide_Zero Failed AssertionSamples.AssertionSamples.CompareComposedCustomer Failed AssertionSamples.AssertionSamples.CompareCustomer Failed AssertionSamples.AssertionSamples.CompareCustomerList Failed AssertionSamples.AssertionSamples.PartialCompare_Customer_Birthday_And_Order_Price << Executor finished. | File redirection rules are cleared. < Test execution finished. Shutting down... Exiting... SerializableException: 因為執行緒結束或應用程式要求,所以已中止 I/O 操作。 於 System.ServiceModel.Channels.SocketConnection.HandleReceiveAsyncCompleted() Test execution finished.
COVER
會先執行單元測試,跑完單元測試數據後會分析其覆蓋率資訊,Output 資訊前面與 run 相同省略僅列出後面不同的部分,可以看到分別進行測試代碼與相對應 class、Method、Point與 Branch 數字資料,並計算其總數與有單元測試代碼的百分比數字
< Test execution finished. Shutting down... Exiting... Generating coverage report... Committing... Visited Classes 4 of 4 (100) Visited Methods 11 of 13 (84.62) Visited Points 37 of 47 (78.72) Visited Branches 13 of 15 (86.67) ==== Alternative Results (includes all methods including those without corresponding source) ==== Alternative Visited Classes 4 of 10 (40) Alternative Visited Methods 11 of 22 (50) Test execution finished.
REPORT
光看簡單數字相信一定無法滿足,這時分析完 COVER 之後可以點選 REPORT,會更清楚的將剛剛 Output 顯示文字資訊顯示出來,說明剛剛所點選的單元測試其涵蓋率是多少、多少沒有涵蓋、總數與其占比各自的數字為多少,可以說是相當的清楚讓你知道單元測試的資訊與透明度。
SETTING
設定項目則是提供想要設定的功能(廢話?),舉例來說在 Visualization 就可以選擇是否要看 line coverage、exception、Branch coverage、Partial coverager 或是 Test ahchors 等資訊;或是當跑測試時平台是 x86 或是 x64,都可以在設定區域找到,其設定詳細說明都可以參考官網介紹。
心得
一開始分析時在自己的電腦環境不管怎麼分析都是 100 %,但同事怎麼跑都是正常的,覺得非常不可思議(沒寫 code 就 100%),後來經過追根究柢後發現自己電腦的 output 訊息與別人都不同,異常時訊息如下
< Test execution finished. Shutting down... Exiting... Generating coverage report... Committing... No results, this could be for a number of reasons. The most common reasons are: 1) missing PDBs for the assemblies that match the filter please review the output file and refer to the Usage guide (Usage.rtf) about filters. 2) the profiler may not be registered correctly, please refer to the Usage guide and the -register switch. Test execution finished.訊息中提到有可能是未產生 PDB 檔案因此無法進行分析,恍然大悟將 VS 執行模式要選擇 DEBUG 立馬正常,終於解決別人都可以我不行的窘境,提供給有跟我一樣困擾的人思考方向,希望可以省下解決這簡單問題的時間 XD。
參考
AxoCover
0 意見:
張貼留言