最近在 marketplace,看到報表的畫面決定來研究看看
https://marketplace.visualstudio.com/items?itemName=Palmmedia.reportgenerator
開發環境
- VS 2019
- .NET Framework 4.7.2
- vstest
vstest的取得方式除了 VS IDE之外,還有 Build Tools、Agent
https://visualstudio.microsoft.com/downloads/
vstest.console.exe 可能會出現的路徑,實作之前要先確定一下路徑,要選擇已經存在的
%ProgramFiles(x86)%\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\CommonExtensions\Microsoft\TestWindow\vstest.console.exe
%ProgramFiles(x86)%\Microsoft Visual Studio\2019\Enterprise\Common7\IDE\CommonExtensions\Microsoft\TestWindow\vstest.console.exe
%ProgramFiles(x86)%\Microsoft Visual Studio\2017\BuildTools\Common7\IDE\Extensions\TestPlatform\vstest.console.exe
%ProgramFiles(x86)%\Microsoft Visual Studio\2017\TestAgent\Common7\IDE\CommonExtensions\Microsoft\TestWindow\vstest.console.exe
%ProgramFiles(x86)%\Microsoft Visual Studio\2019\BuildTools\Common7\IDE\Extensions\TestPlatform\vstest.console.exe
%ProgramFiles(x86)%\Microsoft Visual Studio\2019\TestAgent\Common7\IDE\CommonExtensions\Microsoft\TestWindow\vstest.console.exe
ReportGenerator
安裝方式
VS IDE:Install-Package ReportGenerator
Windows:https://github.com/danielpalme/ReportGenerator/releases
我把它解壓縮到 C:\Tools\ReportGenerator.4.1.2
官方文件
https://github.com/danielpalme/ReportGenerator
https://danielpalme.github.io/ReportGenerator/
ReportGenerator 的輸入格式是 Xml,Input formats 這些工具可以產生 Xml 讓 ReportGenerator 產生報表
下圖出自:https://github.com/danielpalme/ReportGenerator#file-formats
常用參數
-reports:涵蓋率的Xml檔
-targetdir:輸出報表的路徑
-reporttypes:輸出格式,預設是HTML
-sourcedirs:原始碼路徑,
-historydir:歷史紀錄資料夾,有它才能繪製趨勢圖、比對涵蓋率
-tag:可以用 CI 的 Build Number,這樣就可以跟 CI Build 產生關聯
-skipautoprops:屬性不納入涵蓋率計算
更多參數請參考:https://github.com/danielpalme/ReportGenerator#usage
OpenCover
安裝
VS IDE:Install-Package OpenOver
Windows:https://github.com/OpenCover/opencover/releases
我把它解壓縮到 C:\Tools\OpenCover.4.7.922
官方文件
https://github.com/OpenCover/opencover
https://github.com/opencover/opencover/wiki/Usage
常用參數
-register:user
-target:執行測試的執行檔路徑
-targetargs:測試所需要的參數
-targetdir:pdb 路徑
-output:涵蓋率輸出位置(Xml)
更多參數請參考:https://github.com/OpenCover/opencover/wiki/Usage
CI 自動化茶包
上面的指令在本機觸發都沒有問題,但是放到 CI 跑自動化測試時,測試有執行,但是 coverage.xml 內容是 0,錯誤訊息如下
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.
最後,使用 -register 參數指向 build agent 的服務帳號 -register:"domain\TfsDeployService"
dotCover
安裝
VS IDE:Install-Package JetBrains.dotCover.CommandLineTools
不知道為什麼,這個版本我在 VS IDE 裝不起來
Windows:https://www.jetbrains.com/dotcover/download/#section=commandline
我解壓縮到 C:\Tools\JetBrains.dotCover.CommandLineTools.2018.3.4 目錄
官方文件
https://www.jetbrains.com/help/dotcover/Running_Coverage_Analysis_from_the_Command_LIne.html
常用參數
/TargetExecutable=執行測試的執行檔路徑
/TargetArguments=測試所需要的參數
/ReportType=涵蓋率格式,ReportGenerator 需要這種格式
/Output=涵蓋率輸出位置(Xml)
/TargetWorkingDir=工作路徑
/SymbolSearchPaths=pdb 路徑
更多參數請參考:https://www.jetbrains.com/help/dotcover/dotCover__Console_Runner_Commands.html#cover
之前介紹過 dotCover 直接整合 CI Server
https://dotblogs.com.tw/yc421206/2016/05/04/tfs2015_build_vnext_dotcover_deploy_code_coverage_report
實作
先用 VS 2019 開一個測試專案,寫一個簡單的測試案例
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
var calculation = new Calculation();
var actual = calculation.Add(1, 1);
Assert.AreEqual(2,actual);
}
}
OpenCover + ReportGenerator
echo off
SET vsTest="%ProgramFiles(x86)%\Microsoft Visual Studio\2019\BuildTools\Common7\IDE\Extensions\TestPlatform\vstest.console.exe"
set batchFolder=%~dp0
SET openCover="c:\tools\OpenCover.4.7.922\tools\OpenCover.Console.exe"
SET report="c:\tools\ReportGenerator.4.1.2\tools\net47\ReportGenerator.exe"
SET reportOutput=%batchFolder%report
SET historyOutput=%batchFolder%history
SET testResultFolder=%batchFolder%TestResults
SET coverFile="%reportOutput%\coverage.xml"
SET testAssembly=%batchFolder%UnitTestProject1\bin\debug\UnitTestProject1.dll ^
%batchFolder%UnitTestProject2\bin\debug\UnitTestProject2.dll
echo batchFolder=%batchFolder%
echo vsTest =%vsTest%
echo openCover =%openCover%
echo report =%report%
if not exist %historyOutput% (mkdir %historyOutput%)
if exist %reportOutput% (rmdir %reportOutput% /s /q)
mkdir %reportOutput%
if exist %testResultFolder% (rmdir %testResultFolder% /s /q)
mkdir %testResultFolder%
::%openCover% -register:user ^
::-target:%vsTest% ^
::-targetargs:"%testAssembly% /logger:trx /InIsolation /EnableCodeCoverage" ^
::-output:%coverFile% ^
::-filter:"+[UnitTestProject3*]*
%openCover% -register:user ^
-target:%vsTest% ^
-targetargs:"%testAssembly% /logger:trx" ^
-targetdir:%batchFolder% ^
-output:%coverFile% ^
-skipautoprops
%report% -reports:%coverFile% ^
-targetdir:%reportOutput% ^
-reporttypes:Cobertura;HTML;Badges ^
-historydir:%historyOutput% ^
-tag:"Build.2019.11.11" ^
-sourcedirs:%batchFolder%
pause
dotCover + ReportGenerator
echo off
SET vsTest="%ProgramFiles(x86)%\Microsoft Visual Studio\2019\BuildTools\Common7\IDE\Extensions\TestPlatform\vstest.console.exe"
set batchFolder=%~dp0
set dotCover="C:\Tools\JetBrains.dotCover.CommandLineTools.2018.3.4\dotCover.exe"
SET report="c:\tools\ReportGenerator.4.1.2\tools\net47\ReportGenerator.exe"
SET reportOutput=%batchFolder%report
SET historyOutput=%batchFolder%history
SET testResultFolder=%batchFolder%TestResults
SET coverFile="%reportOutput%\coverage.xml"
SET testAssembly=%batchFolder%UnitTestProject1\bin\debug\UnitTestProject1.dll ^
%batchFolder%UnitTestProject2\bin\debug\UnitTestProject2.dll
echo batchFolder=%batchFolder%
echo vsTest =%vsTest%
echo openCover =%openCover%
echo dotCover =%dotCover%
if not exist %historyOutput% (mkdir %historyOutput%)
if exist %reportOutput% (rmdir %reportOutput% /s /q)
mkdir %reportOutput%
if exist %testResultFolder% (rmdir %testResultFolder% /s /q)
mkdir %testResultFolder%
%dotCover% cover ^
/TargetExecutable=%vsTest% ^
/TargetArguments="%testAssembly% /logger:trx" ^
/Output=%coverFile% ^
/ReportType=DetailedXML ^
/TargetWorkingDir=%batchFolder%
%report% -reports:%coverFile% ^
-targetdir:%reportOutput% ^
-reporttypes:Cobertura;HTML;Badges ^
-historydir:%historyOutput% ^
-tag:"Build.2019.11.11" ^
-sourcedirs:%batchFolder%\
pause
執行結果
專案路徑
https://github.com/yaochangyu/sample.dotblog/tree/master/CodeCoverage/OpenCover/Lab.OpenCoverDemo
腳本沒有跑 Build,運行腳本之前,請先開VS IDE Build
若有謬誤,煩請告知,新手發帖請多包涵
Microsoft MVP Award 2010~2017 C# 第四季
Microsoft MVP Award 2018~2022 .NET