只有累積,沒有奇蹟

2019年3月20日 星期三

[VisualStudio] .NET 分析測試代碼覆蓋率 AxoCover

前言 
最近在替既有專案補上單元測試,除了在 SonarQube 可以看到測試代碼覆蓋率之外,這幾天同事推薦一套可以在 Visual Studio 執行的程式碼覆蓋率分析工具 AxoCover,可運行在 .NET 環境且開源免費,簡單玩了一下覺得十分不錯推薦給大家,今天就來分享 AxoCover 在 Visual Studio 2017 的安裝及基本操作說明。

安裝 AxoCover 
Step 1 : 選擇 Tools > Extension and Updates
Step 2 : 搜尋 AxoCover > 點擊 AxoCover > 安裝
Step 3 : 安裝完畢之後,重新啟動 Visual Studio > Tools > Extension and Updates 
發現 AxoCover 已安裝完成

AxoCover 使用說明 
在安裝完畢之後,開啟之前上課的測試專案 AxoCover 是如何進行分析跟介紹,預設是不會開啟 AxoCover 視窗,要到 Visual Studio > Tools > AxoCover 開啟,開啟後位置會在右邊視窗看到其 Logo
在開啟後會去讀取該專案是否有 UnitTest 單元測試項目
如果發現該專案沒有代碼時會跳出以下畫面,這畫面也會列出支援的 Framework,如 MSTest、NUnit、xUnit
接著會列出目前專案中所有的單元測試
上面區塊為 AxoCover 功能列,主要功能有以下

TEST
呈現目前既有的單元測試代碼,以及 UnitTest 相關的一些數字資訊,例如成功/失敗的測試案例數量,包含多少個類別、涵蓋專案中 Namespace 數字等資訊

RUN 
根據所選擇的區域執行單元測試,執行時 output 視窗畫面如下
  1. Test execution started.
  2.  
  3. ___
  4. / \
  5. | |
  6. \ /
  7. / \
  8. / \
  9. ___/ \___
  10. / \
  11. | _______ |
  12. \___/ \___/
  13.  
  14. AxoCover Test Runner Console
  15. (c) Peter Major 2017
  16.  
  17. > Executing tests...
  18. | Runner version is 1.1.389.0.
  19. | We are on x86 platform.
  20. | File redirection is enabled for the following files:
  21. | c:\users\marcustung\appdata\local\microsoft\visualstudio\15.0_3d91008b\extensions\wfz4mp4u.i0u\MSTestAdapter\Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.dll
  22. | c:\users\marcustung\appdata\local\microsoft\visualstudio\15.0_3d91008b\extensions\wfz4mp4u.i0u\MSTestAdapter\Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.dll
  23. | c:\users\marcustung\appdata\local\microsoft\visualstudio\15.0_3d91008b\extensions\wfz4mp4u.i0u\MSTestAdapter\Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.Interface.dll
  24. | c:\users\marcustung\appdata\local\microsoft\visualstudio\15.0_3d91008b\extensions\wfz4mp4u.i0u\MSTestAdapter\Microsoft.VisualStudio.TestPlatform.TestFramework.dll
  25. | c:\users\marcustung\appdata\local\microsoft\visualstudio\15.0_3d91008b\extensions\wfz4mp4u.i0u\MSTestAdapter\Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions.dll
  26. | Setting up file redirection hooks...
  27. | File redirection hooks are enabled.
  28. >> Loading assembly from c:\users\marcustung\appdata\local\microsoft\visualstudio\15.0_3d91008b\extensions\wfz4mp4u.i0u\MSTestAdapter\Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.dll...
  29. || Loaded executor: Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.MSTestExecutor
  30. << Assembly loaded.
  31. >> Running executor: Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.MSTestExecutor...
  32. || Passed AssertionSamples.AssertExceptionSample.Divide_positive
  33. Failed AssertionSamples.AssertExceptionSample.Divide_Zero
  34. Failed AssertionSamples.AssertionSamples.CompareComposedCustomer
  35. Failed AssertionSamples.AssertionSamples.CompareCustomer
  36. Failed AssertionSamples.AssertionSamples.CompareCustomerList
  37. Failed AssertionSamples.AssertionSamples.PartialCompare_Customer_Birthday_And_Order_Price
  38. << Executor finished.
  39. | File redirection rules are cleared.
  40. < Test execution finished.
  41. Shutting down...
  42. Exiting...
  43. SerializableException: 因為執行緒結束或應用程式要求,所以已中止 I/O 操作。
  44. System.ServiceModel.Channels.SocketConnection.HandleReceiveAsyncCompleted()
  45. Test execution finished.

COVER
會先執行單元測試,跑完單元測試數據後會分析其覆蓋率資訊,Output 資訊前面與 run 相同省略僅列出後面不同的部分,可以看到分別進行測試代碼與相對應 class、Method、Point與 Branch 數字資料,並計算其總數與有單元測試代碼的百分比數字
  1. < Test execution finished.
  2. Shutting down...
  3. Exiting...
  4. Generating coverage report...
  5.  
  6. Committing...
  7. Visited Classes 4 of 4 (100)
  8. Visited Methods 11 of 13 (84.62)
  9. Visited Points 37 of 47 (78.72)
  10. Visited Branches 13 of 15 (86.67)
  11.  
  12. ==== Alternative Results (includes all methods including those without corresponding source) ====
  13. Alternative Visited Classes 4 of 10 (40)
  14. Alternative Visited Methods 11 of 22 (50)
  15. Test execution finished.

REPORT
光看簡單數字相信一定無法滿足,這時分析完 COVER 之後可以點選 REPORT,會更清楚的將剛剛 Output 顯示文字資訊顯示出來,說明剛剛所點選的單元測試其涵蓋率是多少、多少沒有涵蓋、總數與其占比各自的數字為多少,可以說是相當的清楚讓你知道單元測試的資訊與透明度。

SETTING
設定項目則是提供想要設定的功能(廢話?),舉例來說在 Visualization 就可以選擇是否要看 line coverage、exception、Branch coverage、Partial coverager 或是 Test ahchors 等資訊;或是當跑測試時平台是 x86 或是 x64,都可以在設定區域找到,其設定詳細說明都可以參考官網介紹。

心得
一開始分析時在自己的電腦環境不管怎麼分析都是 100 %,但同事怎麼跑都是正常的,覺得非常不可思議(沒寫 code 就 100%),後來經過追根究柢後發現自己電腦的 output 訊息與別人都不同,異常時訊息如下
  1. < Test execution finished.
  2. Shutting down...
  3. Exiting...
  4. Generating coverage report...
  5. Committing...
  6. No results, this could be for a number of reasons. The most common reasons are:
  7. 1) missing PDBs for the assemblies that match the filter please review the
  8. output file and refer to the Usage guide (Usage.rtf) about filters.
  9. 2) the profiler may not be registered correctly, please refer to the Usage
  10. guide and the -register switch.
  11. Test execution finished.
訊息中提到有可能是未產生 PDB 檔案因此無法進行分析,恍然大悟將 VS 執行模式要選擇 DEBUG 立馬正常,終於解決別人都可以我不行的窘境,提供給有跟我一樣困擾的人思考方向,希望可以省下解決這簡單問題的時間 XD

參考
AxoCover

Related Posts:

  • [NETCore] 初探 ASP.NET Core 3.0 新朋友 - System.Text.Json前言 相信大家都發現 JSON 格式已經取代過去的 XML 成為資料交換的首選,在 ASP.NET Core 3.0 專案範本設定檔也是使用 JSON 作為設定檔的格式,並內建廣受好評的 Json.NET 做為處理 JSON 格式的函式庫,依存放在 Microsoft.AspNetCore.App 無須在透過 Nuget 另外下載,但老牌的 Json.NET 雖然好用但也相對的存在過去一些沉重的包袱,為了提高性能&n… Read More
  • [VisualStudio] ASP.NET Core 非同步代碼分析工具 - Threading.Analyzers前言 在上一篇介紹了 ASP.NET Core Task block 檢測器 - Ben.BlockingDetector ,可以在 ASP.NET Runtime 時偵測 task block 的情境並透過 log 輸出,但預防的工作總是希望越早進行越好,越快發現就更早的預防不好的寫法及早修改,本篇要介紹的是非同步代碼分析套件,可以分析靜態代碼中可能用法"不正確"的代碼,可以若有問題或是錯誤的地方歡迎高手大大給予指導或討論。 … Read More
  • [NETCore] ASP.NET Core Task block 檢測器 - Ben.BlockingDetector前言 微軟從 ASP.NET Framework 4.5 開始支援 async / await 語法,提供開發者在撰寫非同步作業時更輕鬆與容易上手,如果對於 async / await 不夠熟悉可以看黑暗大之前的好文 ASP.NET async 基本心法,但有時在一知半解寫出來的代碼有時殺傷力才會是最可怕的,如果沒有正確使用 async / await 會造成 Thread block 的問題發生,因此在撰寫&nbs… Read More
  • [Free] ASP.NET Core eBook - ASP.NET Core 的兩三事前言 去年正式踏入 ASP.NET Core 的世界,在學習 .NET Core 的過程中自己也持續地將所學到相關知識記錄在這裡,除了希望可以加深對於 ASP.NET Core 的了解之外也希望能幫助自己健忘的金魚腦,另外也有個想法是累積到一定文章數量之後,可以將所學到的文章有系統與結構性整理出來成電子書,來幫助對 .NET Core 有興趣或是正在學習的的開發者朋友,經過一個月的努力之後於是誕生了 &nbs… Read More
  • [CheatSheets] ASP.NET MVC 5 Pipeline 前言  在 ASP.NET 開發時生命週期 Life cycle 一直是個很重要的議題,在過去自己為了加深這方面的印象分別針對 ASP.NET Application 寫了篇文章 : [.NET] ASP.NET Application 概述,以及古老時代在開發 WebForm 時寫的 : [.NET] ASP.NET 網頁生命週期 Page Life-Cycle,在撰寫文章的過程中也加深了自己對於 … Read More

0 意見:

張貼留言

Copyright © 2025 m@rcus 學習筆記 | Powered by Blogger

Design by Anders Noren | Blogger Theme by NewBloggerThemes.com