[SpecFlow] SpecFlow Report and Pickles via Command Line

續上篇,上篇使用了 Pickles 產生出 Feature 報表,這篇要介紹 SpecFlow 報表,SpecFlow 本身就內建報表,這個報表比較適合開發人員,請參考:

https://github.com/techtalk/SpecFlow/wiki/Reporting

這篇提供了我寫的 Batch 檔,若對指令還不熟的,可以從參考一下。

 

開發環境:

  • VS 2015 Update 2
  • SpecFlow 2.0

安裝:

  • 從 Nuget

使用方式很簡單,有兩種報表樣式,兩個要呈現的內容都不一樣,範例如下:

MsTest Test Execution Report:

specflow.exe mstestexecutionreport BookShop.AcceptanceTests.csproj /testResult:result.trx /out:MyResult.html

 

Step Definition Report:

specflow.exe stepdefinitionreport BookShop.AcceptanceTests.csproj

 

我將這篇跟上篇提到的東西,寫了一隻 bat ,把這隻 bat 放到專案內執行,他會一次產出三種跟測試整合的報表

@echo off

:envVariables
set specFlowReportName=specFlowReport.html
set specFlowStepReportName=specFlowStepReport.html
set picklesReportName=Index.html
set testResultFolderName=TestResults
set testResultName=TestResult.trx

set currentFolder=%cd%
set batchFolder=%~dp0

set debug=\bin\Debug\
set release=\bin\Release\

set mode=%debug%
for %%i in ("%~dp0..") do set previousFolder=%%~fi

for %%f in (%currentFolder%) do set projectName=%%~nxf

set projectFile="%currentFolder%\%projectName%.csproj"
set dll="%currentFolder%%mode%%projectName%.dll"

set projectFolder="%batchFolder%"
set featureFolder="%currentFolder%"
set outputRootPath=%currentFolder%%mode%%testResultFolderName%
set testResultPath="%outputRootPath%\%testResultName%"
set specFlowReportPath="%outputRootPath%\%specFlowReportName%"
set specFlowStepReportPath="%outputRootPath%\%specFlowStepReportName%"
set picklesReportPath="%outputRootPath%\%picklesReportName%"

:externalEXE

set msTestPath="%ProgramFiles(x86)%\Microsoft Visual Studio 14.0\Common7\IDE\MsTest.exe"
REM set msTestPath="%ProgramFiles(x86)%\Microsoft Visual Studio 14.0\Common7\IDE\CommonExtensions\Microsoft\TestWindow\vstest.console.exe"
set specFlowPath="%previousFolder%\packages\SpecFlow.2.0.0\tools\SpecFlow.exe"
set picklesPath="%previousFolder%\packages\Pickles.CommandLine.2.6.0\tools\pickles.exe"


echo Current Folder = %currentFolder%
echo Batch Folder   = %batchFolder%
echo Feature Folder = %featureFolder%
echo Previous Folder= %previousFolder%
echo Pickles Path   = %picklesPath%
echo SpecFlow Path  = %specFlowPath% 
echo ProjectFile    = %projectFile%

echo dll            = %dll%
echo OutputRootPath = %outputRootPath%
echo TestResultPath = %testResultPath%
echo SpecFlowReportPath=%specFlowReportPath%
echo SpecFlowStepReportPath=%specFlowStepReportPath%
echo PicklesReportPath=%picklesReportPath%

:initailFolder
if exist "%outputRootPath%" (rmdir "%outputRootPath%" /s /q )
mkdir "%outputRootPath%"

:runMsTest
echo Generate TestResult.trx....

%msTestPath% /testcontainer:%dll% /resultsfile:%testResultPath%

REM %msTestPath% %dll%  /Logger:trx
:runPicklesReport
echo Generate Pickles Report....

%picklesPath% run --feature-directory=%featureFolder% --output-directory="%outputRootPath%" --documentation-format="dhtml" --test-results-format=mstest --link-results-file=%testResultPath%

:runSpecFlowReport
@echo Generate SpecFlow Report....
%specFlowPath% mstestexecutionreport %projectFile% /testResult:%testResultPath% /out:%specFlowReportPath%
%specFlowPath% stepdefinitionreport %projectFile% /out:%specFlowStepReportPath%

START Chrome %specFlowReportPath%
START Chrome %specFlowStepReportPath%
START Chrome %picklesReportPath%

Exit

執行方式,如下圖:

如果嫌每個專案都要放 bat 檔太麻煩,你也可以把它整合到 VS Tools,這裡的 %1,%2,會從 VS 來,完整程式碼如下

@echo off

:envVariables
set specFlowReportName=specFlowReport.html
set specFlowStepReportName=specFlowStepReport.html
set picklesReportName=Index.html
set testResultFolderName=TestResults
set testResultName=TestResult.trx

set currentFolder=%cd%

set batchFolder=%~dp0
set projectFile=%1
set dll=%2

set projectFolder=%~dp1

IF "%projectFolder:~-1%"=="\" (
	set featureFolder="%projectFolder:~0,-1%"
	) else (
	set featureFolder="%projectFolder%"
)

set outputRootPath=%currentFolder%\%testResultFolderName%
set testResultPath="%outputRootPath%\%testResultName%"
set specFlowReportPath="%outputRootPath%\%specFlowReportName%"
set specFlowStepReportPath="%outputRootPath%\%specFlowStepReportName%"
set picklesReportPath="%outputRootPath%\%picklesReportName%"

:externalEXE
set specFlowRunPath= "%batchFolder%SpecRun.SpecFlow.1.3.0\lib\net45\"

echo specFlowRunPath=%specFlowRunPath%
cd %specFlowRunPath%
pause
set msTestPath="%ProgramFiles(x86)%\Microsoft Visual Studio 14.0\Common7\IDE\MsTest.exe"
REM set msTestPath="%ProgramFiles(x86)%\Microsoft Visual Studio 14.0\Common7\IDE\CommonExtensions\Microsoft\TestWindow\vstest.console.exe"
set specFlowPath="%batchFolder%SpecFlow.2.0.0\tools\SpecFlow.exe"
set picklesPath="%batchFolder%Pickles.CommandLine.2.4.1\tools\pickles.exe"

echo Batch Folder   = %batchFolder%
echo Pickles Path   = %picklesPath%
echo SpecFlow Path  = %specFlowPath% 

echo Feature Folder = %featureFolder%
echo ProjectFile    = %projectFile%
echo Current Folder = %currentFolder%
echo dll            = %dll%

echo OutputRootPath = %outputRootPath%
echo TestResultPath = %testResultPath%

echo SpecFlowReportPath=%specFlowReportPath%
echo PicklesReportPath=%picklesReportPath%

:initailFolder
if exist "%outputRootPath%" (  rmdir "%outputRootPath%" /s /q )
mkdir "%outputRootPath%"

:runMsTest
echo Generate TestResult.trx....
%msTestPath% /testcontainer:%2 /resultsfile:%testResultPath%
REM %msTestPath% %2  /Logger:trx
:runPicklesReport
echo Generate Pickles Report....
%picklesPath% run --feature-directory=%featureFolder% --output-directory="%outputRootPath%" --documentation-format="dhtml" --test-results-format=mstest --link-results-file=%testResultPath%

:runSpecFlowReport
@echo Generate SpecFlow Report....
%specFlowPath% mstestexecutionreport %projectFile% /testResult:%testResultPath% /out:%specFlowReportPath%
%specFlowPath% stepdefinitionreport %projectFile% /out:%specFlowStepReportPath%

pause
START Chrome %specFlowReportPath%
START Chrome %specFlowStepReportPath%
START Chrome %picklesReportPath%

 

目錄結構,把相關的 SpecFlow 跟 Pickles.CommandLine 放在同一個目錄底下,如下圖:

  在 VS IDE 新增 External Tools

Title:Generate Test Report

Command:C:\VSExtendReportTools\Generate Report.bat

Aruments:$(ProjectDir)$(ProjectFileName) $(TargetName)$(TargetExt)

Initial Directory:$(BinDir)

運行時,滑鼠焦點要停在 SpecFlow 測試專案,然後再點選 Generate Report,如下圖: 

最後,報表長這樣。

若有謬誤,煩請告知,新手發帖請多包涵


Microsoft MVP Award 2010~2017 C# 第四季
Microsoft MVP Award 2018~2022 .NET

Image result for microsoft+mvp+logo