一、Jenkins安裝,Xcode插件安裝
略
二、Xcode工程準備
Xcode工程(這里我用的是workspace)包含兩個Target,一個是UnitTest項目
打開Xcode左上角Manage Schemes,將Shared打鉤
選中項目的Scheme點擊左下角Edit,打開Gather coverage data,打開覆蓋率收集,在Debug模式下會收集覆蓋率報告。
寫好UnitTestCase,command+u跑一下,在Xcode里可以看到用例結果報告和覆蓋率報告。
三、集成到Jenkins實現自動化測試
新建Job,設置源碼branch,這里最好新建一個專門用于測試的branch,這里取名HuaXiaFinance-iOS-test.
設置構建觸發器*/5 * * * *,每5分鐘檢查一次源碼變化。
增加構建步驟,選擇Execute shell
!/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只接收Junit的單元測試報告,這里要安裝一個將腳本執行結果的ocunit格式的測試報告轉化為JUnit報告格式的腳本,該項目名叫OCUnit2JUnit,項目地址點這里。安裝非常簡單,命令行下執行gem install ocunit2junit
(可能需要sudo權限)。
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當作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
當然--binary-file 用這種絕對路徑的方式指出不太合理,后續有待優化。
四、讀取顯示junit和覆蓋率html報告
這里用到兩個jenkins插件,jenkins->系統管理-> 管理插件,找到JUnit Plugin和HTML Publisher plugin,安裝重啟jenkins。
增加構建后操作,選擇Publish Junit test result report,配置xml文件路勁為第三步配置的test-reports/*.xml。
點擊立即構建,等待構建完成,返回job主頁,可以看到junit測試結果報告和覆蓋率的圖表了。