Jenkins實現(xiàn)iOS自動化測試及覆蓋率報告輸出

一、Jenkins安裝,Xcode插件安裝
  略
二、Xcode工程準(zhǔn)備
Xcode工程(這里我用的是workspace)包含兩個Target,一個是UnitTest項目


image.png

打開Xcode左上角Manage Schemes,將Shared打鉤


image.png

選中項目的Scheme點擊左下角Edit,打開Gather coverage data,打開覆蓋率收集,在Debug模式下會收集覆蓋率報告。


image.png

寫好UnitTestCase,command+u跑一下,在Xcode里可以看到用例結(jié)果報告和覆蓋率報告。


image.png

三、集成到Jenkins實現(xiàn)自動化測試

新建Job,設(shè)置源碼branch,這里最好新建一個專門用于測試的branch,這里取名HuaXiaFinance-iOS-test.

image.png

設(shè)置構(gòu)建觸發(fā)器*/5 * * * *,每5分鐘檢查一次源碼變化。


image.png

增加構(gòu)建步驟,選擇Execute shell


image.png

!/bin/sh -l

source /etc/profile

cd ./huaxia-ios/HuaXiaFinance2.0

xcodebuild test -workspace HuaXiaFinance.xcworkspace -scheme HuaXiaFinanceTests -destination 'platform=iOS Simulator,name=iPhone 6,OS=11.2' -enableCodeCoverage YES 2>&1 | ocunit2junit

slather coverage --html --input-format profdata --binary-file /Users/huaxiajinrong/Library/Developer/Xcode/DerivedData/HuaXiaFinance-dyxskvcwlazyufhahtutvjhorkwr/Build/Products/Debug-iphonesimulator/HuaXiaFinance.app/HuaXiaFinance --scheme HuaXiaFinanceTests --workspace HuaXiaFinance.xcworkspace --configuration Debug --output-directory reports HuaXiaFinance.xcodeproj

這里用到兩個工具, ocunit2junit 以及slather.

1、由于Jenkins只接收J(rèn)unit的單元測試報告,這里要安裝一個將腳本執(zhí)行結(jié)果的ocunit格式的測試報告轉(zhuǎn)化為JUnit報告格式的腳本,該項目名叫OCUnit2JUnit,項目地址點這里。安裝非常簡單,命令行下執(zhí)行gem install ocunit2junit(可能需要sudo權(quán)限)。

2、代碼覆蓋率
xcode test完成后生成的代碼覆蓋率文件為Coverage.profdata,存放路徑/Users/huaxiajinrong/Library/Developer/Xcode/DerivedData/HuaXiaFinance-dyxskvcwlazyufhahtutvjhorkwr/Build/ProfileData/C054A844-6A3C-4CF5-9ED0-D1165EF6C46C/Coverage.profdata

覆蓋率文件用llvm-cov解析,命令如下:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/llvm-cov report -instr-profile /Users/huaxiajinrong/Library/Developer/Xcode/DerivedData/HuaXiaFinance-dyxskvcwlazyufhahtutvjhorkwr/Build/ProfileData/C054A844-6A3C-4CF5-9ED0-D1165EF6C46C/Coverage.profdata /Users/huaxiajinrong/Library/Developer/Xcode/DerivedData/HuaXiaFinance-dyxskvcwlazyufhahtutvjhorkwr/Build/Products/Debug-iphonesimulator/HuaXiaFinance.app/HuaXiaFinance

這里采用slather去做覆蓋文件可視化報告的生成,你可以把slather當(dāng)作llvm-cov的一個前端生成工具。
//https://github.com/SlatherOrg/slather
安裝如下:
install : sodu gem install slather

這里用slather的時候一直報錯
命令行如下:
slather coverage -s --scheme HuaXiaFinanceTests --workspace HuaXiaFinance.xcworkspace HuaXiaFinance.xcodeproj
報錯如下:
No product binary found in /Users/liaodan/Library/Developer/Xcode/DerivedData/HuaXiaFinance-avwgmcmxqiypmmdhvmglmtwnnzcx/Build/Intermediates.noindex/ProfileData/ED52BCDB-9142-4D42-A7AB-7E37B480F653.
被這個問題困擾了兩天,最后再github上找到了解決辦法,參照:
https://github.com/SlatherOrg/slather/issues/192

slather coverage --html --input-format profdata --binary-file /Users/huaxiajinrong/Library/Developer/Xcode/DerivedData/HuaXiaFinance-dyxskvcwlazyufhahtutvjhorkwr/Build/Products/Debug-iphonesimulator/HuaXiaFinance.app/HuaXiaFinance --scheme HuaXiaFinanceTests --workspace HuaXiaFinance.xcworkspace --configuration Debug --output-directory reports HuaXiaFinance.xcodeproj

當(dāng)然--binary-file 用這種絕對路徑的方式指出不太合理,后續(xù)有待優(yōu)化。

四、讀取顯示junit和覆蓋率html報告
  這里用到兩個jenkins插件,jenkins->系統(tǒng)管理-> 管理插件,找到JUnit Plugin和HTML Publisher plugin,安裝重啟jenkins。
  增加構(gòu)建后操作,選擇Publish Junit test result report,配置xml文件路勁為第三步配置的test-reports/*.xml。

image.png

點擊立即構(gòu)建,等待構(gòu)建完成,返回job主頁,可以看到j(luò)unit測試結(jié)果報告和覆蓋率的圖表了。

image.png
image.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容